Skip to content

Instantly share code, notes, and snippets.

@lukemelia
Created February 11, 2010 22:51
Show Gist options
  • Save lukemelia/302073 to your computer and use it in GitHub Desktop.
Save lukemelia/302073 to your computer and use it in GitHub Desktop.
module Steps
module OauthHelper
def oauth_get(client_application, oauth_token, url)
oauth_headers_factory = OAuthHeadersFactory.new(client_application, oauth_token)
get_via_redirect(url, nil, oauth_headers_factory.headers_for_get(url))
end
def oauth_post(client_application, oauth_token, url, params)
oauth_headers_factory = OAuthHeadersFactory.new(client_application, oauth_token)
post_via_redirect(url,
params,
oauth_headers_factory.headers_for_post(url, params))
end
class OAuthHeadersFactory
def initialize(client_application, oauth_token)
@client_application = client_application
@oauth_token = oauth_token
end
def headers_for_post(url, data)
request = Net::HTTP::Post.new(url)
request.set_form_data(data)
is_ssl = url.starts_with?('https')
request.oauth!(net_http(is_ssl), consumer, access_token)
extract_headers(request)
end
def headers_for_get(url)
request = Net::HTTP::Get.new(url)
is_ssl = url.starts_with?('https')
request.oauth!(net_http(is_ssl), consumer, access_token)
extract_headers(request)
end
def extract_headers(request)
headers = request.to_hash
headers.keys.each{ |key| headers[key] = headers[key].to_s }
headers
end
def site_uri
@site_uri ||= URI.parse(Settings.web_root)
end
def net_http(is_ssl)
http = Net::HTTP.new(site_uri.host, is_ssl ? 443 : site_uri.port)
http.use_ssl = true if is_ssl
http
end
def consumer
@consumer ||= OAuth::Consumer.new(@client_application.key,
@client_application.secret,
{:site => @client_application.url})
end
def access_token
return nil if @oauth_token.nil?
@access_token ||= OAuth::AccessToken.new(consumer,
@oauth_token.token,
@oauth_token.secret)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment