Class: OpenAPI::AuthToken
Class Attribute Summary collapse
-
.key ⇒ Object
Returns the value of attribute key.
Instance Attribute Summary collapse
-
#expires ⇒ Object
Returns the value of attribute expires.
-
#expires_at ⇒ Object
Returns the value of attribute expires_at.
-
#expires_in ⇒ Object
Returns the value of attribute expires_in.
-
#header ⇒ Object
Returns the value of attribute header.
-
#header_format ⇒ Object
Returns the value of attribute header_format.
-
#key ⇒ Object
Returns the value of attribute key.
-
#options ⇒ Object
Returns the value of attribute options.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#token(force_renew = false) ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #headers ⇒ Object
-
#initialize(opts = {}, save = false) ⇒ AuthToken
constructor
A new instance of AuthToken.
- #new_auth_token ⇒ Object
- #renew? ⇒ Boolean
- #renew_token ⇒ Object
- #save ⇒ Object
- #to_hash ⇒ Object
- #update(opts = {}, save = false) ⇒ Object
Constructor Details
#initialize(opts = {}, save = false) ⇒ AuthToken
Returns a new instance of AuthToken.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/openapi/auth_token.rb', line 58 def initialize(opts={}, save=false) = {} opts = {"token" => nil, "refresh_token" => nil, "expires_at" => nil, "expires_in" => nil, "key" => nil, "header" => "Authorization", "header_format" => "Bearer %s"}.merge(opts) update(opts, save) end |
Class Attribute Details
.key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/openapi/auth_token.rb', line 6 def key @key end |
Instance Attribute Details
#expires ⇒ Object
Returns the value of attribute expires.
3 4 5 |
# File 'lib/openapi/auth_token.rb', line 3 def expires @expires end |
#expires_at ⇒ Object
Returns the value of attribute expires_at.
3 4 5 |
# File 'lib/openapi/auth_token.rb', line 3 def expires_at @expires_at end |
#expires_in ⇒ Object
Returns the value of attribute expires_in.
3 4 5 |
# File 'lib/openapi/auth_token.rb', line 3 def expires_in @expires_in end |
#header ⇒ Object
Returns the value of attribute header.
3 4 5 |
# File 'lib/openapi/auth_token.rb', line 3 def header @header end |
#header_format ⇒ Object
Returns the value of attribute header_format.
3 4 5 |
# File 'lib/openapi/auth_token.rb', line 3 def header_format @header_format end |
#key ⇒ Object
Returns the value of attribute key.
3 4 5 |
# File 'lib/openapi/auth_token.rb', line 3 def key @key end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/openapi/auth_token.rb', line 3 def end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
3 4 5 |
# File 'lib/openapi/auth_token.rb', line 3 def refresh_token @refresh_token end |
#token(force_renew = false) ⇒ Object
Returns the value of attribute token.
3 4 5 |
# File 'lib/openapi/auth_token.rb', line 3 def token @token end |
Class Method Details
.fetch(key) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/openapi/auth_token.rb', line 17 def fetch(key) if OpenAPI.cache begin puts "fetch new: #{OpenAPI.cache.get(key)}" return self.new(JSON.parse(OpenAPI.cache.get(key))) rescue => e puts e. end end end |
.fetch_or_create(key, opts) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/openapi/auth_token.rb', line 8 def fetch_or_create(key, opts) auth_token = fetch(key) if auth_token.nil? || auth_token.renew? self.class.new(opts) else auth_token end end |
Instance Method Details
#[](key) ⇒ Object
54 55 56 |
# File 'lib/openapi/auth_token.rb', line 54 def [](key) [key.to_s] end |
#headers ⇒ Object
70 71 72 |
# File 'lib/openapi/auth_token.rb', line 70 def headers {header => header_format % token} end |
#new_auth_token ⇒ Object
99 100 101 |
# File 'lib/openapi/auth_token.rb', line 99 def new_auth_token() raise NotImplementedError end |
#renew? ⇒ Boolean
95 96 97 |
# File 'lib/openapi/auth_token.rb', line 95 def renew? @token == nil || @expires_at.to_i < Time.now.utc.to_i end |
#renew_token ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/openapi/auth_token.rb', line 104 def renew_token new_token = self.class.fetch(self.key) if new_token.nil? || new_token.renew? self.new_auth_token self.save else update(new_token.to_hash) end end |
#save ⇒ Object
78 79 80 81 82 |
# File 'lib/openapi/auth_token.rb', line 78 def save if OpenAPI.cache OpenAPI.cache.set(key, to_hash.to_json, expires_in + 2.hours) end end |
#to_hash ⇒ Object
84 85 86 |
# File 'lib/openapi/auth_token.rb', line 84 def to_hash end |
#update(opts = {}, save = false) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/openapi/auth_token.rb', line 37 def update(opts={}, save=false) = .merge(opts) .each do |k, v| instance_variable_set("@#{k}", v) end @token ||= .delete("access_token") ["token"] = @token if opts.has_key?("expires_at") && !opts["expires_at"].nil? @expires_at = opts["expires_at"].to_i elsif opts.has_key?("expires_in") && !opts["expires_in"].nil? .delete("expires_in") @expires_at = Time.now.utc.to_i + opts["expires_in"] ["expires_at"] = @expires_at end self.save if save end |