Class: OAuth::Consumer
- Inherits:
-
Object
- Object
- OAuth::Consumer
- Defined in:
- lib/oauth/consumer.rb
Constant Summary collapse
- @@default_options =
{ # Signature method used by server. Defaults to HMAC-SHA1 :signature_method => 'HMAC-SHA1', # default paths on site. These are the same as the defaults set up by the generators :request_token_path=>'/oauth/request_token', :authorize_path=>'/oauth/authorize', :access_token_path=>'/oauth/access_token', # How do we send the oauth values to the server see # http://oauth.net/core/1.0/#consumer_req_param for more info # # Possible values: # # :header - via the Authorize header (Default) ( option 1. in spec) # :body - url form encoded in body of POST request ( option 2. in spec) # :query_string - via the query part of the url ( option 3. in spec) :scheme=>:header, # Default http method used for OAuth Token Requests (defaults to :post) :http_method=>:post, :oauth_version=>"1.0" }
Instance Attribute Summary collapse
-
#http ⇒ Object
The HTTP object for the site.
-
#key ⇒ Object
Returns the value of attribute key.
-
#options ⇒ Object
Returns the value of attribute options.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#site ⇒ Object
Returns the value of attribute site.
Instance Method Summary collapse
- #access_token_path ⇒ Object
- #access_token_url ⇒ Object
- #access_token_url? ⇒ Boolean
- #authorize_path ⇒ Object
- #authorize_url ⇒ Object
- #authorize_url? ⇒ Boolean
-
#create_signed_request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates and signs an http request.
-
#get_request_token(request_options = {}, *arguments) ⇒ Object
Makes a request to the service for a new OAuth::RequestToken @[email protected]_request_token.
-
#http_method ⇒ Object
The default http method.
-
#initialize(consumer_key, consumer_secret, options = {}) ⇒ Consumer
constructor
Create a new consumer instance by passing it a configuration hash:.
-
#request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates, signs and performs an http request.
- #request_token_path ⇒ Object
-
#request_token_url ⇒ Object
TODO this is ugly, rewrite.
- #request_token_url? ⇒ Boolean
- #scheme ⇒ Object
-
#sign!(request, token = nil, request_options = {}) ⇒ Object
Sign the Request object.
-
#signature_base_string(request, token = nil, request_options = {}) ⇒ Object
Return the signature_base_string.
-
#token_request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates a request and parses the result as url_encoded.
-
#uri(custom_uri = nil) ⇒ Object
Contains the root URI for this site.
Constructor Details
#initialize(consumer_key, consumer_secret, options = {}) ⇒ Consumer
Create a new consumer instance by passing it a configuration hash:
@consumer=OAuth::Consumer.new( key,secret,{
:site=>"http://term.ie",
:scheme=>:header,
:http_method=>:post,
:request_token_path=>"/oauth/example/request_token.php",
:access_token_path=>"/oauth/example/access_token.php",
:authorize_path=>"/oauth/example/authorize.php"
})
Start the process by requesting a token
@request_token=@consumer.get_request_token
session[:request_token]=@request_token
redirect_to @request_token.
When user returns create an access_token
@access_token=@request_token.get_access_token
@photos=@access_token.get('/photos.xml')
59 60 61 62 63 64 65 66 67 |
# File 'lib/oauth/consumer.rb', line 59 def initialize(consumer_key,consumer_secret,={}) # ensure that keys are symbols @options=@@default_options.merge( .inject({}) do |, (key, value)| [key.to_sym] = value end) @key = consumer_key @secret = consumer_secret end |
Instance Attribute Details
#http ⇒ Object
The HTTP object for the site. The HTTP Object is what you get when you do Net::HTTP.new
75 76 77 |
# File 'lib/oauth/consumer.rb', line 75 def http @http end |
#key ⇒ Object
Returns the value of attribute key.
32 33 34 |
# File 'lib/oauth/consumer.rb', line 32 def key @key end |
#options ⇒ Object
Returns the value of attribute options.
32 33 34 |
# File 'lib/oauth/consumer.rb', line 32 def @options end |
#secret ⇒ Object
Returns the value of attribute secret.
32 33 34 |
# File 'lib/oauth/consumer.rb', line 32 def secret @secret end |
#site ⇒ Object
Returns the value of attribute site.
32 33 34 |
# File 'lib/oauth/consumer.rb', line 32 def site @site end |
Instance Method Details
#access_token_path ⇒ Object
160 161 162 |
# File 'lib/oauth/consumer.rb', line 160 def access_token_path @options[:access_token_path] end |
#access_token_url ⇒ Object
181 182 183 |
# File 'lib/oauth/consumer.rb', line 181 def access_token_url @options[:access_token_url]||site+access_token_path end |
#access_token_url? ⇒ Boolean
185 186 187 |
# File 'lib/oauth/consumer.rb', line 185 def access_token_url? @options[:access_token_url]!=nil end |
#authorize_path ⇒ Object
156 157 158 |
# File 'lib/oauth/consumer.rb', line 156 def @options[:authorize_path] end |
#authorize_url ⇒ Object
173 174 175 |
# File 'lib/oauth/consumer.rb', line 173 def @options[:authorize_url]||site+ end |
#authorize_url? ⇒ Boolean
177 178 179 |
# File 'lib/oauth/consumer.rb', line 177 def @options[:authorize_url]!=nil end |
#create_signed_request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates and signs an http request. It’s recommended to use the Token classes to set this up correctly
118 119 120 121 122 |
# File 'lib/oauth/consumer.rb', line 118 def create_signed_request(http_method,path, token=nil,={},*arguments) request=create_http_request(http_method,path,*arguments) sign!(request,token,) request end |
#get_request_token(request_options = {}, *arguments) ⇒ Object
Makes a request to the service for a new OAuth::RequestToken
@request_token=@consumer.get_request_token
93 94 95 96 |
# File 'lib/oauth/consumer.rb', line 93 def get_request_token(={}, *arguments) response=token_request(http_method,(request_token_url? ? request_token_url : request_token_path), nil, , *arguments) OAuth::RequestToken.new(self,response[:oauth_token],response[:oauth_token_secret]) end |
#http_method ⇒ Object
The default http method
70 71 72 |
# File 'lib/oauth/consumer.rb', line 70 def http_method @http_method||=@options[:http_method]||:post end |
#request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates, signs and performs an http request. It’s recommended to use the OAuth::Token classes to set this up correctly. The arguments parameters are a hash or string encoded set of parameters if it’s a post request as well as optional http headers.
@consumer.request(:get,'/people',@token,{:scheme=>:query_string})
@consumer.request(:post,'/people',@token,{},@person.to_xml,{ 'Content-Type' => 'application/xml' })
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/oauth/consumer.rb', line 105 def request(http_method,path, token=nil,={},*arguments) if path=~/^\// _http=http else _http=create_http(path) _uri=URI.parse(path) path="#{_uri.path}#{_uri.query ? "?#{_uri.query}" : ""}" end _http.request(create_signed_request(http_method,path,token,,*arguments)) end |
#request_token_path ⇒ Object
152 153 154 |
# File 'lib/oauth/consumer.rb', line 152 def request_token_path @options[:request_token_path] end |
#request_token_url ⇒ Object
TODO this is ugly, rewrite
165 166 167 |
# File 'lib/oauth/consumer.rb', line 165 def request_token_url @options[:request_token_url]||site+request_token_path end |
#request_token_url? ⇒ Boolean
169 170 171 |
# File 'lib/oauth/consumer.rb', line 169 def request_token_url? @options[:request_token_url]!=nil end |
#scheme ⇒ Object
148 149 150 |
# File 'lib/oauth/consumer.rb', line 148 def scheme @options[:scheme] end |
#sign!(request, token = nil, request_options = {}) ⇒ Object
Sign the Request object. Use this if you have an externally generated http request object you want to sign.
135 136 137 |
# File 'lib/oauth/consumer.rb', line 135 def sign!(request,token=nil, = {}) request.oauth!(http, self, token, .merge()) end |
#signature_base_string(request, token = nil, request_options = {}) ⇒ Object
Return the signature_base_string
140 141 142 |
# File 'lib/oauth/consumer.rb', line 140 def signature_base_string(request,token=nil, = {}) request.signature_base_string(http, self, token, .merge()) end |
#token_request(http_method, path, token = nil, request_options = {}, *arguments) ⇒ Object
Creates a request and parses the result as url_encoded. This is used internally for the RequestToken and AccessToken requests.
125 126 127 128 129 130 131 132 |
# File 'lib/oauth/consumer.rb', line 125 def token_request(http_method,path,token=nil,={},*arguments) response=request(http_method,path,token,,*arguments) if response.code=="200" CGI.parse(response.body).inject({}){|h,(k,v)| h[k.to_sym]=v.first;h} else response.error! end end |
#uri(custom_uri = nil) ⇒ Object
Contains the root URI for this site
80 81 82 83 84 85 86 87 |
# File 'lib/oauth/consumer.rb', line 80 def uri(custom_uri=nil) if custom_uri @uri = custom_uri @http = create_http # yike, oh well. less intrusive this way else # if no custom passed, we use existing, which, if unset, is set to site uri @uri ||= URI.parse(site) end end |