Class: OpenAuth2::Token

Inherits:
Client
  • Object
show all
Defined in:
lib/open_auth2/token.rb

Overview

Gets Access/Refresh tokens from OAuth server.

Instance Attribute Summary

Attributes inherited from Client

#faraday_url

Instance Method Summary collapse

Methods inherited from Client

#configure, #connection, #run_request, #token

Methods included from DelegateToConfig

#delegate_to_config, extended

Constructor Details

#initialize(config) ⇒ Token

Returns a new instance of Token.



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

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



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

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.


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

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.


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

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

  parse(raw_request)
end

#token_expired?Boolean

Returns:

  • (Boolean)


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

def token_expired?
  Time.now > token_expires_at
rescue
  nil
end