Class: XeroGateway::OAuth

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/xero_gateway/oauth.rb

Overview

Shamelessly based on the Twitter Gem’s OAuth implementation by John Nunemaker Thanks!

twitter.rubyforge.org/ github.com/jnunemaker/twitter/

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

Instance Method Summary collapse

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, options = {})
  @ctoken, @csecret = ctoken, csecret
  @consumer_options = XERO_CONSUMER_OPTIONS.merge(options)
end

Instance Attribute Details

#authorization_expires_atObject (readonly)

Returns the value of attribute authorization_expires_at.



28
29
30
# File 'lib/xero_gateway/oauth.rb', line 28

def authorization_expires_at
  @authorization_expires_at
end

#consumer_optionsObject (readonly)

Returns the value of attribute consumer_options.



28
29
30
# File 'lib/xero_gateway/oauth.rb', line 28

def consumer_options
  @consumer_options
end

#csecretObject (readonly)

Returns the value of attribute csecret.



28
29
30
# File 'lib/xero_gateway/oauth.rb', line 28

def csecret
  @csecret
end

#ctokenObject (readonly)

Returns the value of attribute ctoken.



28
29
30
# File 'lib/xero_gateway/oauth.rb', line 28

def ctoken
  @ctoken
end

#expires_atObject (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_handleObject (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_tokenObject



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 authorize_from_access(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 authorize_from_request(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

#consumerObject



35
36
37
# File 'lib/xero_gateway/oauth.rb', line 35

def consumer
  @consumer ||= ::OAuth::Consumer.new(@ctoken, @csecret, consumer_options)
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.message)
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