Class: OAuth::Consumer

Inherits:
Object
  • Object
show all
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',
  :authenticate_path=>'/oauth/authenticate',
  
  # 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

Instance Method Summary collapse

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.authorize_url

When user returns create an access_token

@access_token=@request_token.get_access_token
@photos=@access_token.get('/photos.xml')


60
61
62
63
64
65
66
67
68
# File 'lib/oauth/consumer.rb', line 60

def initialize(consumer_key,consumer_secret,options={})
  # ensure that keys are symbols
  @options=@@default_options.merge( options.inject({}) do |options, (key, value)|
    options[key.to_sym] = value
    options
  end)
  @key = consumer_key
  @secret = consumer_secret
end

Instance Attribute Details

#httpObject

The HTTP object for the site. The HTTP Object is what you get when you do Net::HTTP.new



76
77
78
# File 'lib/oauth/consumer.rb', line 76

def http
  @http
end

#keyObject

Returns the value of attribute key.



33
34
35
# File 'lib/oauth/consumer.rb', line 33

def key
  @key
end

#optionsObject

Returns the value of attribute options.



33
34
35
# File 'lib/oauth/consumer.rb', line 33

def options
  @options
end

#secretObject

Returns the value of attribute secret.



33
34
35
# File 'lib/oauth/consumer.rb', line 33

def secret
  @secret
end

#siteObject

Returns the value of attribute site.



33
34
35
# File 'lib/oauth/consumer.rb', line 33

def site
  @site
end

Instance Method Details

#access_token_pathObject



165
166
167
# File 'lib/oauth/consumer.rb', line 165

def access_token_path
  @options[:access_token_path]
end

#access_token_urlObject



194
195
196
# File 'lib/oauth/consumer.rb', line 194

def access_token_url
  @options[:access_token_url]||site+access_token_path
end

#access_token_url?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/oauth/consumer.rb', line 198

def access_token_url?
  @options[:access_token_url]!=nil
end

#authenticate_pathObject



161
162
163
# File 'lib/oauth/consumer.rb', line 161

def authenticate_path
  @options[:authenticate_path]
end

#authenticate_urlObject



182
183
184
# File 'lib/oauth/consumer.rb', line 182

def authenticate_url
  @options[:authenticate_url]||site+authenticate_path
end

#authenticate_url?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/oauth/consumer.rb', line 190

def authenticate_url?
  @options[:authenticate_url]!=nil
end

#authorize_pathObject



157
158
159
# File 'lib/oauth/consumer.rb', line 157

def authorize_path
  @options[:authorize_path]
end

#authorize_urlObject



178
179
180
# File 'lib/oauth/consumer.rb', line 178

def authorize_url
  @options[:authorize_url]||site+authorize_path
end

#authorize_url?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/oauth/consumer.rb', line 186

def authorize_url?
  @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



119
120
121
122
123
# File 'lib/oauth/consumer.rb', line 119

def create_signed_request(http_method,path, token=nil,request_options={},*arguments)
  request=create_http_request(http_method,path,*arguments)
  sign!(request,token,request_options)
  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


94
95
96
97
# File 'lib/oauth/consumer.rb', line 94

def get_request_token(request_options={}, *arguments)
  response=token_request(http_method,(request_token_url? ? request_token_url : request_token_path), nil, request_options, *arguments)
  OAuth::RequestToken.new(self,response[:oauth_token],response[:oauth_token_secret])
end

#http_methodObject

The default http method



71
72
73
# File 'lib/oauth/consumer.rb', line 71

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' })


106
107
108
109
110
111
112
113
114
115
# File 'lib/oauth/consumer.rb', line 106

def request(http_method,path, token=nil,request_options={},*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,request_options,*arguments))
end

#request_token_pathObject



153
154
155
# File 'lib/oauth/consumer.rb', line 153

def request_token_path
  @options[:request_token_path]
end

#request_token_urlObject

TODO this is ugly, rewrite



170
171
172
# File 'lib/oauth/consumer.rb', line 170

def request_token_url
  @options[:request_token_url]||site+request_token_path
end

#request_token_url?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/oauth/consumer.rb', line 174

def request_token_url?
  @options[:request_token_url]!=nil
end

#schemeObject



149
150
151
# File 'lib/oauth/consumer.rb', line 149

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.



136
137
138
# File 'lib/oauth/consumer.rb', line 136

def sign!(request,token=nil, request_options = {})
  request.oauth!(http, self, token, options.merge(request_options))
end

#signature_base_string(request, token = nil, request_options = {}) ⇒ Object

Return the signature_base_string



141
142
143
# File 'lib/oauth/consumer.rb', line 141

def signature_base_string(request,token=nil, request_options = {})
  request.signature_base_string(http, self, token, options.merge(request_options))
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.



126
127
128
129
130
131
132
133
# File 'lib/oauth/consumer.rb', line 126

def token_request(http_method,path,token=nil,request_options={},*arguments)
  response=request(http_method,path,token,request_options,*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



81
82
83
84
85
86
87
88
# File 'lib/oauth/consumer.rb', line 81

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