class Geotrigger::AGO::Session::Device

AGO::Session implementation for Devices

Attributes

ago_data[R]
refresh_token[RW]

Public Class Methods

new(session, opts = {}) click to toggle source

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

# 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

Public Instance Methods

access_token() click to toggle source

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

# 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_data() click to toggle source

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

# File lib/geotrigger/ago/session.rb, line 194
def device_data
  @device_data ||= hc(:get, 'portals/self', token: access_token)['deviceInfo']
end

Private Instance Methods

refresh_access_token() click to toggle source

Gets a new access_token.

# File lib/geotrigger/ago/session.rb, line 216
def refresh_access_token
  wrap_token_retrieval do
    @ago_data = hc :post, 'oauth2/token',
      client_id: @client_id,
      refresh_token: @refresh_token,
      grant_type: 'refresh_token'
  end
end
register() click to toggle source

Registers as a new Device with AGO.

# File lib/geotrigger/ago/session.rb, line 202
def register
  wrap_token_retrieval do
    data = hc :post, 'oauth2/registerDevice', client_id: @client_id, expiration: -1
    @ago_data = {
      'access_token' => data['deviceToken']['access_token'],
      'expires_in' => data['deviceToken']['expires_in']
    }
    @device_data = data['device']
    @refresh_token = data['deviceToken']['refresh_token']
  end
end