Class: SparkApi::Authentication::OAuthSession

Inherits:
Object
  • Object
show all
Defined in:
lib/spark_api/authentication/oauth2.rb

Overview

Representation of a session with the api using oauth2

Constant Summary collapse

SESSION_ATTRIBUTES =
[:access_token, :expires_in, :scope, :refresh_token, :refresh_timeout, :start_time]

Instance Method Summary collapse

Constructor Details

#initialize(o = {}) ⇒ OAuthSession

Returns a new instance of OAuthSession.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/spark_api/authentication/oauth2.rb', line 158

def initialize(o={})

  options = OptionsHash.new(o)

  @access_token = options["access_token"]
  @expires_in = options["expires_in"]
  @scope = options["scope"]
  @refresh_token = options["refresh_token"]
  @start_time = options.fetch("start_time", DateTime.now)
  @refresh_timeout = options.fetch("refresh_timeout", 43200)
  if @start_time.is_a? String
    @start_time = DateTime.parse(@start_time)
  end
end

Instance Method Details

#expired?Boolean

Is the user session token expired?

Returns:

  • (Boolean)


173
174
175
176
# File 'lib/spark_api/authentication/oauth2.rb', line 173

def expired?
  return false if @expires_in.nil?
  @start_time + Rational(@expires_in - @refresh_timeout, 86400) < DateTime.now
end

#to_hashObject



182
183
184
185
186
187
188
189
# File 'lib/spark_api/authentication/oauth2.rb', line 182

def to_hash
  hash = {}
  SESSION_ATTRIBUTES.each do |k|
    value = self.send(k)
    hash[k.to_s] = value unless value.nil?
  end
  hash 
end

#to_json(*a) ⇒ Object



178
179
180
# File 'lib/spark_api/authentication/oauth2.rb', line 178

def to_json(*a)
  to_hash.to_json(*a)
end