Created
March 8, 2011 09:11
-
-
Save chromeragnarok/860067 to your computer and use it in GitHub Desktop.
An example on how to use em-http-request to access Twitter's User Stream API
This file contains 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 characters
require 'rubygems' | |
require 'eventmachine' | |
require 'em-http-request' | |
require 'oauth/helper' | |
require 'oauth/signature/hmac/sha1' | |
require 'uri' | |
module TwitterDemo | |
STREAM_URL = "https://userstream.twitter.com/2/user.json" | |
CONSUMER_KEY = "" | |
CONSUMER_SECRET = "" | |
ACCESS_TOKEN = "" | |
ACCESS_TOKEN_SECRET = "" | |
SIGNATURE_METHOD = "HMAC-SHA1" | |
OAUTH_VERSION = "1.0" | |
class OAuthMiddleware | |
def self.normalize_string(raw_string) | |
unsafe = /[^a-zA-Z0-9\-_\.~]+|[\&]+/i | |
URI.escape(raw_string, unsafe) | |
end | |
def self.generate_base_string(http_method, url, parameters) | |
safe_parameters = normalize_string parameters | |
safe_http_method = normalize_string http_method | |
safe_url = normalize_string url | |
p "#{safe_http_method}&#{safe_url}&#{safe_parameters}" | |
"#{safe_http_method}&#{safe_url}&#{safe_parameters}" | |
end | |
def self.nonce | |
rand(10 ** 30).to_s.rjust(30,'0') | |
end | |
def self.sign(key, base_string) | |
digest = OpenSSL::Digest::Digest.new("sha1") | |
hmac = OpenSSL::HMAC.digest(digest, key, base_string) | |
Base64.encode64(hmac).chomp.gsub(/\n/, '') | |
end | |
def self.request(head, body) | |
oauth_nonce = OAuth::Helper.generate_key | |
oauth_signature_method = "HMAC-SHA1" | |
oauth_timestamp = OAuth::Helper.generate_timestamp | |
raw_oauth_parameters = "oauth_consumer_key=#{CONSUMER_KEY}&" + \ | |
"oauth_nonce=#{oauth_nonce}&" + \ | |
"oauth_signature_method=#{oauth_signature_method}&" + \ | |
"oauth_timestamp=#{oauth_timestamp}&" + \ | |
"oauth_token=#{ACCESS_TOKEN}&" + \ | |
"oauth_version=#{OAUTH_VERSION}" | |
secret_key = "#{CONSUMER_SECRET}&#{ACCESS_TOKEN_SECRET}" | |
oauth_signature = self.normalize_string( self.sign(secret_key, self.generate_base_string("GET", STREAM_URL, raw_oauth_parameters)) ).gsub("=","%3D") | |
head["authorization"] = %{OAuth oauth_consumer_key="#{CONSUMER_KEY}", oauth_token="#{ACCESS_TOKEN}",oauth_signature_method="#{oauth_signature_method}", oauth_timestamp="#{oauth_timestamp}", oauth_nonce="#{oauth_nonce}", oauth_signature="#{oauth_signature}", oauth_version="#{OAUTH_VERSION}"} | |
p head | |
[head,body] | |
end | |
end | |
def twitter_oauth_consumer | |
@twitter_oauth_consumer ||= OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com") | |
end | |
def twitter_oauth_access_token | |
@twitter_oauth_access_token ||= OAuth::AccessToken.new(twitter_oauth_consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) | |
end | |
EM.run do | |
@request = EventMachine::HttpRequest.new(STREAM_URL) | |
@request.use OAuthMiddleware | |
http = @request.get :head => {"Accept" => "*/*", "User-Agent" => "oauth-example/0.1", "Keep-alive" => "true" } | |
http.stream do |chunk| | |
p chunk | |
end | |
http.errback do | |
puts "Failed retrieving user stream." | |
puts http.response | |
EM.stop_event_loop | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment