Class: OpenAuth2::Token

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

Overview

Gets 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

Returns a new instance of Token.



8
9
10
11
# File 'lib/open_auth2/token.rb', line 8

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

Instance Method Details

#build_code_url(params = {}) ⇒ Object

Packages info from config & passed in arguments into an url that user can 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.

Returns: String (url).



28
29
30
31
32
33
# File 'lib/open_auth2/token.rb', line 28

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

Makes request to OAuth server for access token & parses it by delegating to the appropriate provider.

Accepts:

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


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

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

  parse(raw_request)
end

#refresh(params = {}) ⇒ Object

Makes request to OAuth server to refresh the access token.

Accepts:

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


55
56
57
58
59
60
# File 'lib/open_auth2/token.rb', line 55

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

  parse(raw_request)
end

#token_expired?Boolean

Returns:

  • (Boolean)


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

def token_expired?
  token_expires_at > Time.now
rescue
  nil
end