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.
-
#session_handle ⇒ Object
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.
31 32 33 34 |
# File 'lib/xero_gateway/oauth.rb', line 31 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 |
#session_handle ⇒ Object
Returns the value of attribute session_handle.
29 30 31 |
# File 'lib/xero_gateway/oauth.rb', line 29 def session_handle @session_handle end |
Instance Method Details
#access_token ⇒ Object
52 53 54 |
# File 'lib/xero_gateway/oauth.rb', line 52 def access_token @access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret) end |
#authorize_from_access(atoken, asecret) ⇒ Object
56 57 58 |
# File 'lib/xero_gateway/oauth.rb', line 56 def (atoken, asecret) @atoken, @asecret = atoken, asecret end |
#authorize_from_request(rtoken, rsecret, params = {}) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/xero_gateway/oauth.rb', line 44 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
36 37 38 |
# File 'lib/xero_gateway/oauth.rb', line 36 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/xero_gateway/oauth.rb', line 61 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
40 41 42 |
# File 'lib/xero_gateway/oauth.rb', line 40 def request_token(params = {}) @request_token ||= consumer.get_request_token(params) end |