Class: OAuthClient

Inherits:
HTTPClient show all
Defined in:
lib/oauthclient.rb

Overview

OAuthClient provides OAuth related methods in addition to HTTPClient.

See sample/ dir how to use OAuthClient. There are sample clients for Twitter, FriendFeed and Google Buzz.

Constant Summary

Constants inherited from HTTPClient

HTTPClient::DEFAULT_AGENT_NAME, HTTPClient::GSSAPIEnabled, HTTPClient::LIB_NAME, HTTPClient::NTLMEnabled, HTTPClient::PROPFIND_DEFAULT_EXTHEADER, HTTPClient::RUBY_VERSION_STRING, HTTPClient::SSPIEnabled, HTTPClient::VERSION

Instance Attribute Summary collapse

Attributes inherited from HTTPClient

#base_url, #cookie_manager, #default_header, #follow_redirect_count, #proxy_auth, #request_filter, #ssl_config, #test_loopback_response, #www_auth

Instance Method Summary collapse

Methods inherited from HTTPClient

#cookies, #debug_dev, #debug_dev=, #default_redirect_uri_callback, #delete, #delete_async, #force_basic_auth=, #get, #get_async, #get_content, #head, #head_async, #keep_webmock_compat, #no_proxy, #no_proxy=, #options, #options_async, #post, #post_async, #post_content, #propfind, #propfind_async, #proppatch, #proppatch_async, #proxy, #proxy=, #put, #put_async, #redirect_uri_callback=, #request, #request_async, #request_async2, #reset, #reset_all, #save_cookie_store, #set_auth, #set_basic_auth, #set_cookie_store, #set_proxy_auth, #strict_redirect_uri_callback, timeout_scheduler, #trace, #trace_async

Methods included from HTTPClient::Util

#argument_to_hash, hash_find_value, #http?, #https?, #keyword_argument, try_require, uri_dirname, uri_part_of, urify

Constructor Details

#initialize(*arg) ⇒ OAuthClient

Creates a OAuthClient instance which provides OAuth related methods in addition to HTTPClient.

Method signature is as same as HTTPClient. See HTTPClient.new


31
32
33
34
35
36
# File 'lib/oauthclient.rb', line 31

def initialize(*arg)
  super
  @oauth_config = HTTPClient::OAuth::Config.new
  self.www_auth.oauth.set_config(nil, @oauth_config)
  self.www_auth.oauth.challenge(nil)
end

Instance Attribute Details

#oauth_configObject

HTTPClient::OAuth::Config

OAuth configurator.


25
26
27
# File 'lib/oauthclient.rb', line 25

def oauth_config
  @oauth_config
end

Instance Method Details

#get_access_token(uri, request_token, request_token_secret, verifier = nil) ⇒ Object

Get access token.

uri

URI for request token.

request_token

a request token String. See get_access_token.

request_token_secret

a request secret String. See get_access_token.

verifier

a verifier tag String.

When the request succeeds and the server returns a pair of access token and secret, oauth_config.token and oauth_token.secret are updated with the access token. Then you can call OAuthClient#get, #post, #delete, etc. All requests are signed.


69
70
71
72
73
74
75
76
77
78
# File 'lib/oauthclient.rb', line 69

def get_access_token(uri, request_token, request_token_secret, verifier = nil)
  oauth_config.token = request_token
  oauth_config.secret = request_token_secret
  oauth_config.callback = nil
  oauth_config.verifier = verifier
  res = request(oauth_config.http_method, uri)
  filter_response(res)
  oauth_config.verifier = nil
  res
end

#get_oauth_response(res) ⇒ Object

Parse response and returns a Hash.


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/oauthclient.rb', line 81

def get_oauth_response(res)
  enc = res.header['content-encoding']
  body = nil
  if enc and enc[0] and enc[0].downcase == 'gzip'
    body = Zlib::GzipReader.wrap(StringIO.new(res.content)) { |gz| gz.read }
  else
    body = res.content
  end
  body.split('&').inject({}) { |r, e|
    key, value = e.split('=', 2)
    r[unescape(key)] = unescape(value)
    r
  }
end

#get_request_token(uri, callback = nil, param = nil) ⇒ Object

Get request token.

uri

URI for request token.

callback

callback String. This can be nil for OAuth 1.0a

param

Additional query parameter Hash.

It returns a HTTP::Message instance as a response. When the request is made successfully, you can retrieve a pair of request token and secret like following;

res = client.get_request_token(...)
token = res.oauth_params['oauth_token']
secret = res.oauth_params['oauth_token_secret']

49
50
51
52
53
54
55
56
57
# File 'lib/oauthclient.rb', line 49

def get_request_token(uri, callback = nil, param = nil)
  oauth_config.token = nil
  oauth_config.secret = nil
  oauth_config.callback = callback
  oauth_config.verifier = nil
  res = request(oauth_config.http_method, uri, param)
  filter_response(res)
  res
end