Created
October 21, 2014 18:56
Revisions
-
Eric Berry created this gist
Oct 21, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ require 'oauth' require 'httpclient' require 'json' class SimpleTwitter def initialize(config) @config = config @http_client = HTTPClient.new end def encoded_token bearer_token_credentials = "#{@config[:key]}:#{@config[:secret]}" Base64.encode64(bearer_token_credentials).split(/\n/).join('') end def authorization_headers { 'Authorization' => "Basic #{encoded_token}", 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8' } end def get_user_timeline(username, max=10) access_token = get_bearer_token uri = URI.parse("https://api.twitter.com/1.1/statuses/user_timeline.json?count=#{max}&screen_name=#{username}") headers = { 'User-Agent' => 'My Twitter App v1.0.23', 'Authorization' => "Bearer #{access_token}", 'Accept-Encoding' => 'gzip' } response = @http_client.get(uri, {}, headers) gz = Zlib::GzipReader.new(StringIO.new(response.body.to_s)) return JSON.parse(gz.read) end def get_bearer_token uri = URI.parse('https://api.twitter.com/oauth2/token') response = @http_client.post(uri, { grant_type: 'client_credentials' }, authorization_headers) return JSON.parse(response.body)['access_token'] end end twitter = SimpleTwitter.new({ key: ENV['CONSUMER_KEY'], secret: ENV['CONSUMER_SECRET'] }) results = twitter.get_user_timeline('coderberry') puts results.inspect