Class: HypertrackV3

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertrack_v3.rb

Defined Under Namespace

Classes: ClientError, Engine, HttpError, InternalServerError, RegisterHookError

Constant Summary collapse

BASE_URL =
'https://v3.api.hypertrack.com'
RES_DEVICES =
'/devices'
RES_DEVICE =
"/devices/%{device_id}"
RES_TRIPS =
"/trips"
RES_TRIP =
"/trips/%{trip_id}"
RES_TRIP_COMPLETE =
"/trips/%{trip_id}/complete"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_id, secret_key) ⇒ HypertrackV3

Returns a new instance of HypertrackV3.



27
28
29
30
31
# File 'lib/hypertrack_v3.rb', line 27

def initialize(, secret_key)
  @client = nil
  @account_id = 
  @secret_key = secret_key
end

Class Method Details

.error_handlerObject



101
102
103
# File 'lib/hypertrack_v3.rb', line 101

def self.error_handler
  @@error_handler ||= ->(*, **) { nil }
end

.error_handler=(error_handler) ⇒ Object



105
106
107
# File 'lib/hypertrack_v3.rb', line 105

def self.error_handler=(error_handler)
  @@error_handler = error_handler
end

.exception_handlerObject



109
110
111
# File 'lib/hypertrack_v3.rb', line 109

def self.exception_handler
  @@error_handler ||= ->(*, **) { nil }
end

.exception_handler=(error_handler) ⇒ Object



113
114
115
# File 'lib/hypertrack_v3.rb', line 113

def self.exception_handler=(error_handler)
  @@error_handler = error_handler
end

.log_error(message, **data) ⇒ Object



117
118
119
120
# 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) ⇒ Object



122
123
124
125
# 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

.loggerObject



93
94
95
# File 'lib/hypertrack_v3.rb', line 93

def self.logger
  @@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

.logger=(logger) ⇒ Object



97
98
99
# File 'lib/hypertrack_v3.rb', line 97

def self.logger=(logger)
  @@logger = logger
end

Instance Method Details

#clientObject



33
34
35
36
37
38
39
40
41
42
43
# 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:) ⇒ Object



60
61
62
# 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:) ⇒ Object



56
57
58
# File 'lib/hypertrack_v3.rb', line 56

def device_get(id:, **)
  parse(client.get(self.class::RES_DEVICE % {device_id: id}))
end

#device_listObject



52
53
54
# File 'lib/hypertrack_v3.rb', line 52

def device_list
  parse(client.get(self.class::RES_DEVICES))
end

#parse(res) ⇒ Object



45
46
47
48
49
50
# 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:) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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: ,
      }
    end
  )
end

#trip_get(id:) ⇒ Object



85
86
87
# 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) ⇒ Object



81
82
83
# 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:) ⇒ Object



89
90
91
# File 'lib/hypertrack_v3.rb', line 89

def trip_set_complete(id:, **)
  parse(client.post(self.class::RES_TRIP_COMPLETE % {trip_id: id}))
end