Class: OpenAuth2::Token

Inherits:
Object
  • Object
show all
Extended by:
DelegateToConfig
Includes:
Connection
Defined in:
lib/open_auth2/token.rb

Overview

Used to get Access/Refresh tokens from OAuth server.

Instance Method Summary collapse

Methods included from DelegateToConfig

delegate_to_config, extended

Methods included from Connection

#connection, included

Constructor Details

#initialize(config) ⇒ Token

Use it to set @config. Unlike Client, no error is raised since this is not part of public api. This will be called from Client#token internally only.

Accepts:

config: OpenAuth2::Config object

Returns: self.



17
18
19
20
21
22
# File 'lib/open_auth2/token.rb', line 17

def initialize(config)
  @config      = config
  @faraday_url = authorize_url

  self
end

Instance Method Details

#build_code_url(params = {}) ⇒ Object

Packages the info from config & passed in arguments into an url that user has to visit to authorize this app.

Examples:

token.build_code_url
#=> 'http://...'

# or
token.build_code_url(:scope => 'publish_stream')

Accepts:

params: (optional) Hash of additional config to be bundled into
        the url.

Returns: String (url).



40
41
42
43
44
45
46
# File 'lib/open_auth2/token.rb', line 40

def build_code_url(params={})
  url = URI::HTTPS.build(:host  => host,
                         :path  => authorize_path,
                         :query => encoded_body(params))

  url.to_s
end

#get(params = {}) ⇒ Object

Make request to OAuth server for access token & ask @config to parse it. @config delegates that to the appropriate provider.

We ask @config since the format of response differs between OAuth servers widely.

Accepts:

params: (optional) Hash of additional config to be sent with
        request.

Returns: ?.



60
61
62
63
64
65
# File 'lib/open_auth2/token.rb', line 60

def get(params={})
  body        = get_body_hash(params)
  raw_request = post(body)

  parse(raw_request)
end

#refresh(params = {}) ⇒ Object

Make request to OAuth server to refresh the access token & ask @config to parse it.

Accepts:

params: (optional) Hash of additional config to be sent with
        request.

Returns: ?.



76
77
78
79
80
81
# File 'lib/open_auth2/token.rb', line 76

def refresh(params={})
  body        = refresh_body_hash(params)
  raw_request = post(body)

  parse(raw_request)
end

#token_expired?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
# File 'lib/open_auth2/token.rb', line 83

def token_expired?
  token_expires_at > Time.now
rescue
  nil
end