Class: Lita::Handlers::Reddit::ApiCalls
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::Reddit::ApiCalls
- Defined in:
- lib/lita/handlers/reddit/api_calls.rb
Instance Method Summary collapse
- #get_posts(subreddit, redis_key, limit = 3) ⇒ Object
-
#initialize(client_id, client_secret) ⇒ ApiCalls
constructor
A new instance of ApiCalls.
- #update_token ⇒ Object
- #user_agent ⇒ Object
Constructor Details
#initialize(client_id, client_secret) ⇒ ApiCalls
Returns a new instance of ApiCalls.
5 6 7 8 |
# File 'lib/lita/handlers/reddit/api_calls.rb', line 5 def initialize(client_id, client_secret) @client_id = client_id @client_secret = client_secret end |
Instance Method Details
#get_posts(subreddit, redis_key, limit = 3) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/lita/handlers/reddit/api_calls.rb', line 29 def get_posts(subreddit, redis_key, limit = 3) resp = http.get do |req| req.url "https://oauth.reddit.com/r/#{subreddit}/new.json" req.headers["Authorization"] = "bearer #{@access_token}" req.headers["User-Agent"] = user_agent req.headers["Content-Type"] = "Application/json" req.params["limit"] = limit req.params["before"] = redis_key end response_body = MultiJson.load(resp.body) results = response_body["data"]["children"] results.collect do |r| { id: r["data"]["id"], subreddit: r["data"]["subreddit"], title: r["data"]["title"], user: r["data"]["author"], shortlink: format("http://redd.it/%s", r["data"]["id"]) } end rescue Exception => msg log.error("lita-reddit: Exception during get_posts: #{msg}") return [] end |
#update_token ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lita/handlers/reddit/api_calls.rb', line 14 def update_token log.info("lita-reddit: updating userless auth token") request = http request.basic_auth(@client_id, @client_secret) auth_response = request.post do |req| req.url "https://www.reddit.com/api/v1/access_token" req.headers["User-Agent"] = user_agent req.body = "grant_type=client_credentials" end response = MultiJson.load(auth_response.body) @access_token = response["access_token"] rescue Exception => msg log.error("lita-reddit: Exception during token update: #{msg}") end |
#user_agent ⇒ Object
10 11 12 |
# File 'lib/lita/handlers/reddit/api_calls.rb', line 10 def user_agent "ruby:lita-reddit:v0.0.8 (by /u/dosman711)" end |