Class: FlexmlsApi::Authentication::OAuthSession

Inherits:
Object
  • Object
show all
Defined in:
lib/flexmls_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(options = {}) ⇒ OAuthSession

Returns a new instance of OAuthSession.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/flexmls_api/authentication/oauth2.rb', line 92

def initialize(options={})
  @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",3600)
  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)


104
105
106
# File 'lib/flexmls_api/authentication/oauth2.rb', line 104

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

#to_json(*a) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/flexmls_api/authentication/oauth2.rb', line 108

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