Created
July 30, 2011 02:56
-
-
Save charliemoseley/1115142 to your computer and use it in GitHub Desktop.
Probably a completely wrong gem
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 characters
require "gdata_request/version" | |
module GdataRequest | |
# A simple class that makes a oauth request using the oauth gem | |
class OauthRequest | |
def initialize(oauth_token, oauth_secret, params = { | |
:application_id => 'anonymous', | |
:application_secret => 'anonymous', | |
:site => 'https://www.google.com' | |
}) | |
@oauth_token = oauth_token | |
@oauth_secret = oauth_secret | |
@application_id = params[:application_id] | |
@application_secret = params[:application_secret] | |
@site = params[:site] | |
end | |
def to_sc | |
"Without Self" | |
end | |
def self.to_sc | |
"With Self" | |
end | |
def request(rest, url, data = nil, options = nil) | |
# Default get requests to json | |
unless url.include? 'alt=json' | |
if url.include? '?' | |
url = url + '&alt=json' | |
else | |
url = url + '?alt=json' | |
end | |
end | |
# Default post requests to json | |
if options.nil? | |
options = { 'Content-Type' => 'application/json' } | |
else | |
unless options.has_key? 'Content-Type' | |
options['Content-Type'] = 'application/json' | |
end | |
response = request_raw(rest, url, data, options) | |
ActiveSupport::JSON.decode response | |
end | |
def request_raw(rest, url, data = nil, options = nil) | |
response = oath_access_token.request(rest, url, data, options) | |
case response | |
when Net::HTTPSuccess then response.body | |
when Net::HTTPRedirection then oath_access_token.request(rest, response['location'], data, options).body | |
else | |
response.error! | |
end | |
end | |
private | |
def access_token | |
@oath_access_token ||= create_access_token | |
end | |
def create_access_token | |
consumer = OAuth::Consumer.new @application_id, | |
@application_secret, | |
:site => @site | |
token_hash = { :oauth_token => @oauth_token, | |
:oauth_token_secret => @oauth_secret } | |
access_token = OAuth::AccessToken.from_hash(consumer, token_hash) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment