Class: XeroGateway::OAuth
- Inherits:
-
Object
- Object
- XeroGateway::OAuth
- Extended by:
- Forwardable
- Defined in:
- lib/xero_gateway/oauth.rb
Overview
Shamelessly based on the Twitter Gem’s OAuth implementation by John Nunemaker Thanks!
Defined Under Namespace
Classes: RateLimitExceeded, TokenExpired, TokenInvalid, UnknownError
Constant Summary collapse
- XERO_CONSUMER_OPTIONS =
{ :site => "https://api.xero.com", :request_token_path => "/oauth/RequestToken", :access_token_path => "/oauth/AccessToken", :authorize_path => "/oauth/Authorize" }.freeze
Instance Attribute Summary collapse
-
#authorization_expires_at ⇒ Object
readonly
Returns the value of attribute authorization_expires_at.
-
#consumer_options ⇒ Object
readonly
Returns the value of attribute consumer_options.
-
#csecret ⇒ Object
readonly
Returns the value of attribute csecret.
-
#ctoken ⇒ Object
readonly
Returns the value of attribute ctoken.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#session_handle ⇒ Object
readonly
Returns the value of attribute session_handle.
Instance Method Summary collapse
- #access_token ⇒ Object
- #authorize_from_access(atoken, asecret) ⇒ Object
- #authorize_from_request(rtoken, rsecret, params = {}) ⇒ Object
- #consumer ⇒ Object
-
#initialize(ctoken, csecret, options = {}) ⇒ OAuth
constructor
A new instance of OAuth.
-
#renew_access_token(access_token = nil, access_secret = nil, session_handle = nil) ⇒ Object
Renewing access tokens only works for Partner applications.
- #request_token(params = {}) ⇒ Object
Constructor Details
#initialize(ctoken, csecret, options = {}) ⇒ OAuth
Returns a new instance of OAuth.
30 31 32 33 |
# File 'lib/xero_gateway/oauth.rb', line 30 def initialize(ctoken, csecret, = {}) @ctoken, @csecret = ctoken, csecret @consumer_options = XERO_CONSUMER_OPTIONS.merge() end |
Instance Attribute Details
#authorization_expires_at ⇒ Object (readonly)
Returns the value of attribute authorization_expires_at.
28 29 30 |
# File 'lib/xero_gateway/oauth.rb', line 28 def @authorization_expires_at end |
#consumer_options ⇒ Object (readonly)
Returns the value of attribute consumer_options.
28 29 30 |
# File 'lib/xero_gateway/oauth.rb', line 28 def @consumer_options end |
#csecret ⇒ Object (readonly)
Returns the value of attribute csecret.
28 29 30 |
# File 'lib/xero_gateway/oauth.rb', line 28 def csecret @csecret end |
#ctoken ⇒ Object (readonly)
Returns the value of attribute ctoken.
28 29 30 |
# File 'lib/xero_gateway/oauth.rb', line 28 def ctoken @ctoken end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
28 29 30 |
# File 'lib/xero_gateway/oauth.rb', line 28 def expires_at @expires_at end |
#session_handle ⇒ Object (readonly)
Returns the value of attribute session_handle.
28 29 30 |
# File 'lib/xero_gateway/oauth.rb', line 28 def session_handle @session_handle end |
Instance Method Details
#access_token ⇒ Object
51 52 53 |
# File 'lib/xero_gateway/oauth.rb', line 51 def access_token @access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret) end |
#authorize_from_access(atoken, asecret) ⇒ Object
55 56 57 |
# File 'lib/xero_gateway/oauth.rb', line 55 def (atoken, asecret) @atoken, @asecret = atoken, asecret end |
#authorize_from_request(rtoken, rsecret, params = {}) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/xero_gateway/oauth.rb', line 43 def (rtoken, rsecret, params = {}) request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret) access_token = request_token.get_access_token(params) @atoken, @asecret = access_token.token, access_token.secret update_attributes_from_token(access_token) end |
#consumer ⇒ Object
35 36 37 |
# File 'lib/xero_gateway/oauth.rb', line 35 def consumer @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, ) end |
#renew_access_token(access_token = nil, access_secret = nil, session_handle = nil) ⇒ Object
Renewing access tokens only works for Partner applications
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/xero_gateway/oauth.rb', line 60 def renew_access_token(access_token = nil, access_secret = nil, session_handle = nil) access_token ||= @atoken access_secret ||= @asecret session_handle ||= @session_handle old_token = ::OAuth::RequestToken.new(consumer, access_token, access_secret) access_token = old_token.get_access_token({ :oauth_session_handle => session_handle, :token => old_token }) update_attributes_from_token(access_token) rescue ::OAuth::Unauthorized => e # If the original access token is for some reason invalid an OAuth::Unauthorized could be raised. # In this case raise a XeroGateway::OAuth::TokenInvalid which can be captured by the caller. In this # situation the end user will need to re-authorize the application via the request token authorization URL raise XeroGateway::OAuth::TokenInvalid.new(e.) end |
#request_token(params = {}) ⇒ Object
39 40 41 |
# File 'lib/xero_gateway/oauth.rb', line 39 def request_token(params = {}) @request_token ||= consumer.get_request_token(params) end |