Class: OAuth2::AccessToken
- Inherits:
-
Object
- Object
- OAuth2::AccessToken
- Defined in:
- lib/oauth2/access_token.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Class Method Summary collapse
-
.from_hash(client, hash) ⇒ AccessToken
Initializes an AccessToken from a Hash.
-
.from_kvform(client, kvform) ⇒ AccessToken
Initializes an AccessToken from a key/value application/x-www-form-urlencoded string.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Indexer to additional params present in token response.
-
#delete(path, opts = {}, &block) ⇒ Object
Make a DELETE request with the Access Token.
-
#expired? ⇒ Boolean
Whether or not the token is expired.
-
#expires? ⇒ Boolean
Whether or not the token expires.
-
#get(path, opts = {}, &block) ⇒ Object
Make a GET request with the Access Token.
-
#headers ⇒ Object
Get the headers hash (includes Authorization token).
-
#initialize(client, token, opts = {}) ⇒ AccessToken
constructor
Initalize an AccessToken.
-
#post(path, opts = {}, &block) ⇒ Object
Make a POST request with the Access Token.
-
#put(path, opts = {}, &block) ⇒ Object
Make a PUT request with the Access Token.
-
#refresh!(params = {}) ⇒ AccessToken
Refreshes the current Access Token.
-
#request(verb, path, opts = {}, &block) ⇒ Object
Make a request with the Access Token.
Constructor Details
#initialize(client, token, opts = {}) ⇒ AccessToken
Initalize an AccessToken
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/oauth2/access_token.rb', line 39 def initialize(client, token, opts={}) @client = client @token = token.to_s [:refresh_token, :expires_in, :expires_at].each do |arg| instance_variable_set("@#{arg}", opts.delete(arg) || opts.delete(arg.to_s)) end @expires_in ||= opts.delete('expires') @expires_in &&= @expires_in.to_i @expires_at ||= Time.now.to_i + @expires_in if @expires_in @options = {:mode => opts.delete(:mode) || :header, :header_format => opts.delete(:header_format) || 'Bearer %s', :param_name => opts.delete(:param_name) || 'bearer_token'} @params = opts end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/oauth2/access_token.rb', line 3 def client @client end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
3 4 5 |
# File 'lib/oauth2/access_token.rb', line 3 def expires_at @expires_at end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
3 4 5 |
# File 'lib/oauth2/access_token.rb', line 3 def expires_in @expires_in end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/oauth2/access_token.rb', line 4 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
3 4 5 |
# File 'lib/oauth2/access_token.rb', line 3 def params @params end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
3 4 5 |
# File 'lib/oauth2/access_token.rb', line 3 def refresh_token @refresh_token end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
3 4 5 |
# File 'lib/oauth2/access_token.rb', line 3 def token @token end |
Class Method Details
.from_hash(client, hash) ⇒ AccessToken
Initializes an AccessToken from a Hash
12 13 14 |
# File 'lib/oauth2/access_token.rb', line 12 def from_hash(client, hash) self.new(client, hash.delete('access_token') || hash.delete(:access_token), hash) end |
.from_kvform(client, kvform) ⇒ AccessToken
Initializes an AccessToken from a key/value application/x-www-form-urlencoded string
21 22 23 |
# File 'lib/oauth2/access_token.rb', line 21 def from_kvform(client, kvform) from_hash(client, Rack::Utils.parse_query(kvform)) end |
Instance Method Details
#[](key) ⇒ Object
Indexer to additional params present in token response
57 58 59 |
# File 'lib/oauth2/access_token.rb', line 57 def [](key) @params[key] end |
#delete(path, opts = {}, &block) ⇒ Object
Make a DELETE request with the Access Token
125 126 127 |
# File 'lib/oauth2/access_token.rb', line 125 def delete(path, opts={}, &block) request(:delete, path, opts, &block) end |
#expired? ⇒ Boolean
Whether or not the token is expired
71 72 73 |
# File 'lib/oauth2/access_token.rb', line 71 def expired? expires? && (expires_at < Time.now.to_i) end |
#expires? ⇒ Boolean
Whether or not the token expires
64 65 66 |
# File 'lib/oauth2/access_token.rb', line 64 def expires? !!@expires_at end |
#get(path, opts = {}, &block) ⇒ Object
Make a GET request with the Access Token
104 105 106 |
# File 'lib/oauth2/access_token.rb', line 104 def get(path, opts={}, &block) request(:get, path, opts, &block) end |
#headers ⇒ Object
Get the headers hash (includes Authorization token)
130 131 132 |
# File 'lib/oauth2/access_token.rb', line 130 def headers { 'Authorization' => [:header_format] % token } end |
#post(path, opts = {}, &block) ⇒ Object
Make a POST request with the Access Token
111 112 113 |
# File 'lib/oauth2/access_token.rb', line 111 def post(path, opts={}, &block) request(:post, path, opts, &block) end |
#put(path, opts = {}, &block) ⇒ Object
Make a PUT request with the Access Token
118 119 120 |
# File 'lib/oauth2/access_token.rb', line 118 def put(path, opts={}, &block) request(:put, path, opts, &block) end |
#refresh!(params = {}) ⇒ AccessToken
options should be carried over to the new AccessToken
Refreshes the current Access Token
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/oauth2/access_token.rb', line 79 def refresh!(params={}) raise "A refresh_token is not available" unless refresh_token params.merge!(:client_id => @client.id, :client_secret => @client.secret, :grant_type => 'refresh_token', :refresh_token => refresh_token) new_token = @client.get_token(params) new_token. = new_token end |
#request(verb, path, opts = {}, &block) ⇒ Object
Make a request with the Access Token
96 97 98 99 |
# File 'lib/oauth2/access_token.rb', line 96 def request(verb, path, opts={}, &block) set_token(opts) @client.request(verb, path, opts, &block) end |