class HypertrackV3

Constants

BASE_URL
RES_DEVICE
RES_DEVICES
RES_TRIP
RES_TRIPS
RES_TRIP_COMPLETE

Public Class Methods

error_handler() click to toggle source
# File lib/hypertrack_v3.rb, line 101
def self.error_handler
  @@error_handler ||= ->(*, **) { nil }
end
error_handler=(error_handler) click to toggle source
# File lib/hypertrack_v3.rb, line 105
def self.error_handler=(error_handler)
  @@error_handler = error_handler
end
exception_handler() click to toggle source
# File lib/hypertrack_v3.rb, line 109
def self.exception_handler
  @@error_handler ||= ->(*, **) { nil }
end
exception_handler=(error_handler) click to toggle source
# File lib/hypertrack_v3.rb, line 113
def self.exception_handler=(error_handler)
  @@error_handler = error_handler
end
log_error(message, **data) click to toggle source
# File lib/hypertrack_v3.rb, line 117
def self.log_error(message, **data)
  self.logger.error({message: message}.merge(data))
  self.error_handler.(message, **data)
end
log_exception(exception, **data) click to toggle source
# File lib/hypertrack_v3.rb, line 122
def self.log_exception(exception, **data)
  self.logger.error({exception: exception.as_json}.merge(data))
  self.exception_handler.(exception, **data)
end
logger() click to toggle source
# File lib/hypertrack_v3.rb, line 93
def self.logger
  @@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end
logger=(logger) click to toggle source
# File lib/hypertrack_v3.rb, line 97
def self.logger=(logger)
  @@logger = logger
end
new(account_id, secret_key) click to toggle source
# File lib/hypertrack_v3.rb, line 27
def initialize(account_id, secret_key)
  @client = nil
  @account_id = account_id
  @secret_key = secret_key
end

Public Instance Methods

client() click to toggle source
# File lib/hypertrack_v3.rb, line 33
def client
  @client ||= Faraday.new url: self.class::BASE_URL do |conn|
    conn.basic_auth(@account_id, @secret_key)
    conn.request :json
    conn.response :json, :content_type => /\bjson$/
    conn.response :json, :parser_options => { :object_class => OpenStruct }
    conn.use Faraday::Response::Logger, HypertrackV3.logger, bodies: true
    conn.use :instrumentation
    conn.adapter Faraday.default_adapter
  end
end
device_del(id:, **) click to toggle source
# File lib/hypertrack_v3.rb, line 60
def device_del(id:, **)
  parse(client.delete(self.class::RES_DEVICE % {device_id: id}))
end
device_get(id:, **) click to toggle source
# File lib/hypertrack_v3.rb, line 56
def device_get(id:, **)
  parse(client.get(self.class::RES_DEVICE % {device_id: id}))
end
device_list() click to toggle source
# File lib/hypertrack_v3.rb, line 52
def device_list
  parse(client.get(self.class::RES_DEVICES))
end
parse(res) click to toggle source
# File lib/hypertrack_v3.rb, line 45
def parse(res)
  raise InternalServerError.new(res.status, res.body) if res.status >= 500
  raise ClientError.new(res.status, res.body) if res.status >= 400
  raise HttpError.new(res.status, res.body) unless res.success?
  res.body
end
trip_create( device_id:, destination:, geofences:, metadata:, **) click to toggle source
# File lib/hypertrack_v3.rb, line 64
def trip_create(
    device_id:,
    destination:,
    geofences:,
    metadata:, **)
  parse(
    client.post(self.class::RES_TRIPS) do |req|
      req.body = {
        device_id: device_id,
        destination: destination,
        geofences: geofences,
        metadata: metadata,
      }
    end
  )
end
trip_get(id:, **) click to toggle source
# File lib/hypertrack_v3.rb, line 85
def trip_get(id:, **)
  parse(client.get(self.class::RES_TRIP % {trip_id: id}))
end
trip_list(limit=50, offset=0) click to toggle source
# File lib/hypertrack_v3.rb, line 81
def trip_list(limit=50, offset=0)
  parse(client.get(self.class::RES_TRIPS, params={limit: limit, offset: offset}))
end
trip_set_complete(id:, **) click to toggle source
# File lib/hypertrack_v3.rb, line 89
def trip_set_complete(id:, **)
  parse(client.post(self.class::RES_TRIP_COMPLETE % {trip_id: id}))
end