GithubHelp home page GithubHelp logo

mtowers / redd Goto Github PK

View Code? Open in Web Editor NEW

This project forked from avinashbot/redd

0.0 1.0 0.0 279 KB

An awesome API wrapper for Ruby.

Home Page: http://git.io/redd#readme

License: MIT License

Ruby 100.00%

redd's Introduction

redd
Gem Version Build Status MIT License Chat on Gitter

redd is an API wrapper for reddit written in ruby that focuses on being consistent and extensible. Check out the latest documentation on RubyDoc.


Gemfile

gem "redd", "~> 0.7.0"

# Optional fast JSON parser.
gem "oj", platforms: :ruby

Getting Started

# Authorization (Web)
w = Redd.it(:web, "CLIENT_ID", "SECRET", "REDIRECT_URI", user_agent: "TestSite v1.0.0")
url = w.auth_url("random_state", ["identity", "read"], :temporary)
puts "Please go to #{url} and enter the code below:"
code = gets.chomp
w.authorize!(code)

# Authorization
r = Redd.it(:script, "CLIENT_ID", "SECRET", "Unidan", "hunter2", user_agent: "TestBot v1.0.0")
r.authorize!

# See documentation for more grants.
# Access
require "secure_random"
require "sinatra"

enable :sessions

get "/auth" do
  state = SecureRandom.urlsafe_base64
  session[:state] = state
  redirect w.auth_url(state, %w(identity), :permanent)
end

get "/redirect" do
  halt 500, "Your state doesn't match!" unless session[:state] == params[:state]
  access = w.authorize!(params[:code])
  session[:access] = access.to_json
  redirect to("/name")
end

get "/name" do
  if session[:access]
    session_access = Redd::Access.from_json(session[:access])
    w.with(session_access) do |client|
      client.refresh_access! if session_access.expired?
      "Your username is #{client.me.name}"
    end
  else
    redirect to("/auth")
  end
end
# Getting a model
vargas = r.user_from_name("_vargas_")
puts vargas.keys
puts vargas.over_18?

picturegame = r.subreddit_from_name("picturegame")
puts picturegame.display_name
puts picturegame.public_description
# Listings
hot = r.get_hot("all")
hot.each { |link| puts "#{link.title} by /u/#{link.author}" }
# Streaming
def stream_all!
  r.stream :get_comments, "all" do |comment|
    reddit.refresh_access! if reddit.access.expired? # for :web
    comment.reply("World!") if comment.body == "Hello?"
  end
end
# Escaping Errors
begin
  stream_all!
rescue Redd::Error::RateLimited => error
  sleep(error.time)
  retry
rescue Redd::Error => error
  # 5-something errors are usually errors on reddit's end.
  raise error unless (500...600).include?(error.code)
  retry
end

Extras

# Extending Redd.rb

module MyGildingExtension
  def gild!
    # We're using post instead of request_object, because we don't
    # expect any object from the response.
    post("/api/v1/gold/gild/#{fullname}")
  end
end

Redd::Objects::Comment.include(MyGildingExtension)
Redd::Objects::Submission.include(MyGildingExtension)
# Contributing
1. [Fork](https://github.com/avidw/redd/issues/new)
2. [Pull](https://github.com/avidw/redd/compare)
3. [Profit](https://www.reddit.com/r/requestabot)

# Request a Feature
1. [Help](https://github.com/avidw/redd/issues/new)
# Copyright.rb
#
# Copyright (c) Avinash Dwarapu under the MIT License. See LICENSE.md for more details.
# Redd::Error has been modified from RedditKit.rb. See RedditKit.LICENSE.md for its license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.