Class: CommaAPI::API

Inherits:
Object
  • Object
show all
Extended by:
Config, HTTP
Defined in:
lib/comma_api.rb

Constant Summary

Constants included from Config

Config::API_HOST, Config::DONGLE_ID_DEFAULT

Class Method Summary collapse

Methods included from HTTP

http, post_request, request

Class Method Details

.argsObject



87
88
89
# File 'lib/comma_api.rb', line 87

def args
  defaultArgs # alias (alias :args :defaultArgs)
end

.defaultArgsObject



91
92
93
94
# File 'lib/comma_api.rb', line 91

def defaultArgs
  one_week = 604800
  "?from=#{(Time.now - one_week - 1).to_i}"
end

.device(id:) ⇒ Object



21
22
23
# File 'lib/comma_api.rb', line 21

def device(id:)
  get "/v1.1/devices/#{id}/"
end

.device404ErrorObject

errors



114
115
116
# File 'lib/comma_api.rb', line 114

def device404Error
  { error: 'Error: DONGLE_ID_DEFAULT is missing - this method cannot be executed, you need to specify a dongle id' }.to_json
end

.deviceDefaultObject



34
35
36
37
# File 'lib/comma_api.rb', line 34

def deviceDefault
  return device404Error unless DONGLE_ID_DEFAULT
  device id: DONGLE_ID_DEFAULT
end

.deviceDefaultLocationObject



29
30
31
32
# File 'lib/comma_api.rb', line 29

def deviceDefaultLocation
  return device404Error unless DONGLE_ID_DEFAULT
  deviceLocation id: DONGLE_ID_DEFAULT
end

.deviceDefaultSegmentsObject



82
83
84
85
# File 'lib/comma_api.rb', line 82

def deviceDefaultSegments
  return device404Error unless DONGLE_ID_DEFAULT
  deviceSegments id: DONGLE_ID_DEFAULT
end

.deviceDefaultStatsObject



39
40
41
42
# File 'lib/comma_api.rb', line 39

def deviceDefaultStats
  return device404Error unless DONGLE_ID_DEFAULT
  deviceStats id: DONGLE_ID_DEFAULT
end

.deviceLocation(id:) ⇒ Object



25
26
27
# File 'lib/comma_api.rb', line 25

def deviceLocation(id:)
  get "/v1/devices/#{id}/location"
end

.devicesObject

devices



17
18
19
# File 'lib/comma_api.rb', line 17

def devices
  get "/v1/me/devices/"
end

.deviceSegments(id:) ⇒ Object



77
78
79
80
# File 'lib/comma_api.rb', line 77

def deviceSegments(id:)
  return device404Error unless DONGLE_ID_DEFAULT
  get "/v1/devices/#{id}/segments#{args}"
end

.devicesStat(id:) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/comma_api.rb', line 48

def devicesStat(id:)
  devices = get "/v1/devices"
  # TODO: loop through all devices, return all stats and a total
  devices.map { |device|
    get "/v1/devices/#{device.f("dongle_id")}/stats#{args}"
  }
end

.deviceStats(id:) ⇒ Object



44
45
46
# File 'lib/comma_api.rb', line 44

def deviceStats(id:)
  get "/v1/devices/#{id}/stats"
end

.deviceStatsId(id:) ⇒ Object



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

def deviceStatsId(id:)
  get "/v1/devices/#{id}/stats#{args}"
end

.get(method_name) ⇒ Object

helper



103
104
105
106
107
108
109
# File 'lib/comma_api.rb', line 103

def get(method_name)
  # note: trailing slash gets added here
  puts "requesting: #{method_name}"
  url = "#{API_HOST}#{method_name}"
  puts url
  request url: url
end

.meObject

user



11
12
13
# File 'lib/comma_api.rb', line 11

def me
  get "/v1/me/"
end

.opAuthObject



96
97
98
# File 'lib/comma_api.rb', line 96

def opAuth
  "https://api.commadotai.com/v2/pilotauth/"
end

.route(route_name:) ⇒ Object

routes



62
63
64
# File 'lib/comma_api.rb', line 62

def route(route_name:)
  get "/v1/route/#{route_name}/"
end

.routeFiles(route_name:) ⇒ Object



66
67
68
# File 'lib/comma_api.rb', line 66

def routeFiles(route_name:)
  get "/v1/route/#{route_name}/files"
end

.segmentsObject

segments



72
73
74
75
# File 'lib/comma_api.rb', line 72

def segments
  return device404Error unless DONGLE_ID_DEFAULT
  get "/v1/devices/#{DONGLE_ID_DEFAULT}/segments#{args}"
end