Class: XeroGateway::OAuth
- Inherits:
-
Object
- Object
- XeroGateway::OAuth
- 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
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
- #delete(path, headers = {}) ⇒ Object
- #get(path, headers = {}) ⇒ Object
-
#initialize(ctoken, csecret, options = {}) ⇒ OAuth
constructor
A new instance of OAuth.
- #post(path, body = '', headers = {}) ⇒ Object
- #put(path, body = '', headers = {}) ⇒ Object
-
#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.
28 29 30 31 32 33 34 35 36 |
# File 'lib/xero_gateway/oauth.rb', line 28 def initialize(ctoken, csecret, = {}) @ctoken, @csecret = ctoken, csecret # Allow user-agent base val for certification procedure (enforce for PartnerApp) @base_headers = {} @base_headers["User-Agent"] = .delete(:user_agent) if .has_key?(:user_agent) @consumer_options = XERO_CONSUMER_OPTIONS.merge() end |
Instance Attribute Details
#authorization_expires_at ⇒ Object (readonly)
Returns the value of attribute authorization_expires_at.
25 26 27 |
# File 'lib/xero_gateway/oauth.rb', line 25 def @authorization_expires_at end |
#consumer_options ⇒ Object (readonly)
Returns the value of attribute consumer_options.
25 26 27 |
# File 'lib/xero_gateway/oauth.rb', line 25 def @consumer_options end |
#csecret ⇒ Object (readonly)
Returns the value of attribute csecret.
25 26 27 |
# File 'lib/xero_gateway/oauth.rb', line 25 def csecret @csecret end |
#ctoken ⇒ Object (readonly)
Returns the value of attribute ctoken.
25 26 27 |
# File 'lib/xero_gateway/oauth.rb', line 25 def ctoken @ctoken end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
25 26 27 |
# File 'lib/xero_gateway/oauth.rb', line 25 def expires_at @expires_at end |
#session_handle ⇒ Object
Returns the value of attribute session_handle.
26 27 28 |
# File 'lib/xero_gateway/oauth.rb', line 26 def session_handle @session_handle end |
Instance Method Details
#access_token ⇒ Object
58 59 60 |
# File 'lib/xero_gateway/oauth.rb', line 58 def access_token @access_token ||= ::OAuth::AccessToken.new(consumer, @atoken, @asecret) end |
#authorize_from_access(atoken, asecret) ⇒ Object
62 63 64 |
# File 'lib/xero_gateway/oauth.rb', line 62 def (atoken, asecret) @atoken, @asecret = atoken, asecret end |
#authorize_from_request(rtoken, rsecret, params = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/xero_gateway/oauth.rb', line 48 def (rtoken, rsecret, params = {}) request_token = ::OAuth::RequestToken.new(consumer, rtoken, rsecret) # Underlying oauth consumer accepts body params and headers for request via positional params - explicit nilling of # body parameters allows for correct position for headers access_token = request_token.get_access_token(params, nil, @base_headers) @atoken, @asecret = access_token.token, access_token.secret update_attributes_from_token(access_token) end |
#consumer ⇒ Object
38 39 40 |
# File 'lib/xero_gateway/oauth.rb', line 38 def consumer @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, ) end |
#delete(path, headers = {}) ⇒ Object
101 102 103 |
# File 'lib/xero_gateway/oauth.rb', line 101 def delete(path, headers = {}) access_token.delete(path, headers.merge(@base_headers)) end |
#get(path, headers = {}) ⇒ Object
89 90 91 |
# File 'lib/xero_gateway/oauth.rb', line 89 def get(path, headers = {}) access_token.get(path, headers.merge(@base_headers)) end |
#post(path, body = '', headers = {}) ⇒ Object
93 94 95 |
# File 'lib/xero_gateway/oauth.rb', line 93 def post(path, body = '', headers = {}) access_token.post(path, body, headers.merge(@base_headers)) end |
#put(path, body = '', headers = {}) ⇒ Object
97 98 99 |
# File 'lib/xero_gateway/oauth.rb', line 97 def put(path, body = '', headers = {}) access_token.put(path, body, headers.merge(@base_headers)) end |
#renew_access_token(access_token = nil, access_secret = nil, session_handle = nil) ⇒ Object
Renewing access tokens only works for Partner applications
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/xero_gateway/oauth.rb', line 67 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) # Underlying oauth consumer accepts body params and headers for request via positional params - explicit nilling of # body parameters allows for correct position for headers access_token = old_token.get_access_token({ :oauth_session_handle => session_handle, :token => old_token }, nil, @base_headers) 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
42 43 44 45 46 |
# File 'lib/xero_gateway/oauth.rb', line 42 def request_token(params = {}) # Underlying oauth consumer accepts body params and headers for request via positional params - explicit nilling of # body parameters allows for correct position for headers @request_token ||= consumer.get_request_token(params, nil, @base_headers) end |