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.



31
32
33
34
# File 'lib/xero_gateway/oauth.rb', line 31

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

#session_handleObject

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_tokenObject



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



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

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



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.message)
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