Class: Geotrigger::AGO::Session::Device

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
AccessToken, ExpirySet
Defined in:
lib/geotrigger/ago/session.rb

Overview

AGO::Session implementation for Devices

Constant Summary

Constants included from ExpirySet

ExpirySet::TOKEN_EXPIRY_BUFFER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AccessToken

#access_token=

Methods included from ExpirySet

#wrap_token_retrieval

Constructor Details

#initialize(session, opts = {}) ⇒ Device

Accepts the abstract AGO::Session and a Hash with :client_id and :refresh_token keys.



171
172
173
174
# File 'lib/geotrigger/ago/session.rb', line 171

def initialize session, opts = {}
  @session, @client_id, @refresh_token =
    session, opts[:client_id], opts[:refresh_token]
end

Instance Attribute Details

#ago_dataObject (readonly)

Returns the value of attribute ago_data.



166
167
168
# File 'lib/geotrigger/ago/session.rb', line 166

def ago_data
  @ago_data
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



165
166
167
# File 'lib/geotrigger/ago/session.rb', line 165

def refresh_token
  @refresh_token
end

Instance Method Details

#access_tokenObject

Returns a valid access_token. Registers a new Device with AGO if needed.



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/geotrigger/ago/session.rb', line 179

def access_token
  if @ago_data.nil?
    if @refresh_token.nil?
      register
    else
      refresh_access_token
    end
  elsif not @ago_data[:expires_at].nil? and Time.now >= @ago_data[:expires_at]
    refresh_access_token
  end
  @ago_data['access_token']
end

#device_dataObject

Fetches data from AGO about the device specified by this :access_token+.



194
195
196
# File 'lib/geotrigger/ago/session.rb', line 194

def device_data
  @device_data ||= hc(:get, 'portals/self', token: access_token)['deviceInfo']
end