Class: OpenAPI::AuthToken

Inherits:
Object show all
Defined in:
lib/openapi/auth_token.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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)
  @options = {}
  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

.keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/openapi/auth_token.rb', line 6

def key
  @key
end

Instance Attribute Details

#expiresObject

Returns the value of attribute expires.



3
4
5
# File 'lib/openapi/auth_token.rb', line 3

def expires
  @expires
end

#expires_atObject

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_inObject

Returns the value of attribute expires_in.



3
4
5
# File 'lib/openapi/auth_token.rb', line 3

def expires_in
  @expires_in
end

#headerObject

Returns the value of attribute header.



3
4
5
# File 'lib/openapi/auth_token.rb', line 3

def header
  @header
end

#header_formatObject

Returns the value of attribute header_format.



3
4
5
# File 'lib/openapi/auth_token.rb', line 3

def header_format
  @header_format
end

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/openapi/auth_token.rb', line 3

def key
  @key
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/openapi/auth_token.rb', line 3

def options
  @options
end

#refresh_tokenObject

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.message
    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)
  @options[key.to_s]
end

#headersObject



70
71
72
# File 'lib/openapi/auth_token.rb', line 70

def headers
  {header => header_format % token}
end

#new_auth_tokenObject

Raises:

  • (NotImplementedError)


99
100
101
# File 'lib/openapi/auth_token.rb', line 99

def new_auth_token()
  raise NotImplementedError
end

#renew?Boolean

Returns:

  • (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_tokenObject



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

#saveObject



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_hashObject



84
85
86
# File 'lib/openapi/auth_token.rb', line 84

def to_hash
  @options
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)
  @options = @options.merge(opts)
  @options.each do |k, v|
    instance_variable_set("@#{k}", v)
  end
  @token ||= @options.delete("access_token")
  @options["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?
    @options.delete("expires_in")
    @expires_at = Time.now.utc.to_i + opts["expires_in"]
    @options["expires_at"] = @expires_at
  end
  self.save if save
end