ax-track 0.1.12 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +5 -2
- data/lib/ax-track.rb +1 -0
- data/lib/ax_track/client.rb +7 -5
- data/lib/ax_track/collection.rb +5 -7
- data/lib/ax_track/object.rb +9 -3
- data/lib/ax_track/objects/asset.rb +0 -10
- data/lib/ax_track/objects/tracker/gps_position.rb +15 -0
- data/lib/ax_track/objects/tracker.old +42 -0
- data/lib/ax_track/objects/tracker.rb +22 -36
- data/lib/ax_track/resource.rb +16 -11
- data/lib/ax_track/resources/asset_resource.rb +3 -2
- data/lib/ax_track/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34c0014b35fd2c5751cd11b90c99730472099f0218fe121f19add0e28b14d9f9
|
4
|
+
data.tar.gz: 9ddee5341711f61bdc51961363b4434dff2cacd11d16ae7791f45327db975ad3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 785311de5a3ce20c3ef733055b001a2af4aae4e035bb2fa3ee07ca816bf32dd45a123cd830237e136cbc741a24bcb3b8e1302a9f15ea7e3e938982f000213da8
|
7
|
+
data.tar.gz: 27b1852ba78ada2a75b7ba2dceca80a264e4f67a37bcf19f7b784cceb7985ea2501517187ce60c6e20950664a0df203104992d5182f41eacf0c05c0bbd763350
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -34,7 +34,7 @@ client = AxTrack::Client.new(api_key: your_api_key)
|
|
34
34
|
# Get a list of all trackers
|
35
35
|
client.tracker.list
|
36
36
|
|
37
|
-
# Get the specific information of a specific tracker
|
37
|
+
# Get the specific information of a specific tracker. If :asset_id is invalid, a AxTrack::Resource::NotFoundError is returned
|
38
38
|
client.tracker.retrieve(:tracker_id)
|
39
39
|
```
|
40
40
|
|
@@ -43,7 +43,7 @@ client.tracker.retrieve(:tracker_id)
|
|
43
43
|
# Get a list of all assets
|
44
44
|
client.asset.list
|
45
45
|
|
46
|
-
# Get a specific asset
|
46
|
+
# Get a specific asset. If :asset_id is invalid, a AxTrack::Resource::NotFoundError is returned
|
47
47
|
client.asset.retrieve(:asset_id)
|
48
48
|
|
49
49
|
# Update a specif asset
|
@@ -52,6 +52,9 @@ client.asset.update(:tracker_id, params)
|
|
52
52
|
client.tracker.update(:tracker_id, name: 'New name')
|
53
53
|
```
|
54
54
|
|
55
|
+
### Errors
|
56
|
+
|
57
|
+
|
55
58
|
## Development
|
56
59
|
|
57
60
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/ax-track.rb
CHANGED
@@ -11,6 +11,7 @@ module AxTrack
|
|
11
11
|
|
12
12
|
autoload :Tracker, 'ax_track/objects/tracker'
|
13
13
|
autoload :Asset, 'ax_track/objects/asset'
|
14
|
+
autoload :GPSPosition, 'ax_track/objects/tracker/gps_position'
|
14
15
|
|
15
16
|
autoload :TrackerResource, 'ax_track/resources/tracker_resource'
|
16
17
|
autoload :AssetResource, 'ax_track/resources/asset_resource'
|
data/lib/ax_track/client.rb
CHANGED
@@ -12,9 +12,12 @@ module AxTrack
|
|
12
12
|
APIKeyMissing = Class.new(StandardError)
|
13
13
|
|
14
14
|
attr_reader :api_key, :adapter
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
attr_writer :connection
|
16
|
+
|
17
|
+
def initialize(api_key: nil, adapter: nil, stubs: nil)
|
18
|
+
@api_key = api_key || ENV['AXTRACK_API_KEY']
|
19
|
+
@adapter = adapter || Faraday.default_adapter
|
20
|
+
@stubs = stubs
|
18
21
|
|
19
22
|
raise APIKeyMissing, "No API key provided" if !defined?(api_key) || api_key.nil? || api_key.empty?
|
20
23
|
end
|
@@ -33,10 +36,9 @@ module AxTrack
|
|
33
36
|
#conn.request :url_encoded
|
34
37
|
conn.request :json
|
35
38
|
conn.response :json, content_type: 'application/json'
|
36
|
-
conn.adapter
|
39
|
+
conn.adapter adapter, @stubs
|
37
40
|
conn.headers['Authorization'] = "Token #{api_key}" unless api_key.empty?
|
38
41
|
end
|
39
42
|
end
|
40
|
-
|
41
43
|
end
|
42
44
|
end
|
data/lib/ax_track/collection.rb
CHANGED
@@ -6,15 +6,13 @@ module AxTrack
|
|
6
6
|
# type is the class Type to wrap the results from key
|
7
7
|
def self.from_response(json_response, key:, type: )
|
8
8
|
body = json_response.body
|
9
|
-
new(
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
prev_cursor: body.dig('previous')
|
14
|
-
)
|
9
|
+
new(data: body[key].map { |attrs| type.new(attrs ) },
|
10
|
+
total: body.dig('count'),
|
11
|
+
next_cursor: body.dig('next'),
|
12
|
+
prev_cursor: body.dig('previous'))
|
15
13
|
end
|
16
14
|
|
17
|
-
def initialize
|
15
|
+
def initialize(data:, total:, next_cursor:, prev_cursor:)
|
18
16
|
@data = data
|
19
17
|
@total = total
|
20
18
|
@next_cursor = next_cursor.nil? || next_cursor.empty? ? nil : next_cursor
|
data/lib/ax_track/object.rb
CHANGED
@@ -3,14 +3,20 @@ require 'ostruct'
|
|
3
3
|
module AxTrack
|
4
4
|
class Object
|
5
5
|
|
6
|
+
def initialize(json_response)
|
7
|
+
# for each key create an own instance variable with a getter
|
8
|
+
json_response.each do |key, value|
|
9
|
+
instance_variable_set "@#{key}", value
|
10
|
+
end
|
11
|
+
|
12
|
+
create_getters
|
13
|
+
end
|
14
|
+
|
6
15
|
# pass in an array for getters which should be generated.
|
7
16
|
# if nothing is passed in, it will create an instance variable for all instance variables.
|
8
17
|
def create_getters(required_getter_methods = instance_variables.map { |attr_name| attr_name[1..-1 ]})
|
9
18
|
required_getter_methods.each do |attr|
|
10
19
|
singleton_class.send :attr_reader, attr unless self.respond_to? attr
|
11
|
-
# define_singleton_method(v.to_s.tr('@','')) do
|
12
|
-
# instance_variable_get(v)
|
13
|
-
# end
|
14
20
|
end
|
15
21
|
end
|
16
22
|
|
@@ -1,14 +1,4 @@
|
|
1
1
|
module AxTrack
|
2
2
|
class Asset < Object
|
3
|
-
|
4
|
-
def initialize(json_response)
|
5
|
-
# for each key create an own instance variable with a getter
|
6
|
-
json_response.each do |key, value|
|
7
|
-
instance_variable_set "@#{key}", value
|
8
|
-
end
|
9
|
-
|
10
|
-
create_getters
|
11
|
-
end
|
12
3
|
end
|
13
|
-
|
14
4
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module AxTrack
|
2
|
+
class Tracker
|
3
|
+
class GPSPosition < Object
|
4
|
+
|
5
|
+
def initialize(json_response)
|
6
|
+
@lat = json_response['lat']
|
7
|
+
@lng = json_response['lng']
|
8
|
+
@timestamp = DateTime.parse(json_response['timestamp'], false) if json_response['timestamp']
|
9
|
+
|
10
|
+
create_getters
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module AxTrack
|
2
|
+
class Tracker < Object
|
3
|
+
|
4
|
+
def initialize(json_response)
|
5
|
+
@tracker_id = json_response['id']
|
6
|
+
@asset_id = json_response['asset']
|
7
|
+
@identifier = json_response['identifier']
|
8
|
+
@url = json_response['url']
|
9
|
+
@active = json_response['active']
|
10
|
+
@model = json_response['model']
|
11
|
+
@asset_details = Asset.new json_response['asset_details'] if json_response['asset_details']
|
12
|
+
@name = json_response.dig('asset_details', 'name')
|
13
|
+
@last_message_timestamp = DateTime.parse(json_response['last_message_timestamp'], false) if json_response['last_message_timestamp']
|
14
|
+
@url = json_response['url']
|
15
|
+
@user_url = website_url
|
16
|
+
@last_gps_position = GPSPosition.new(json_response['last_gps_measurement'] || json_response['asset_details'])
|
17
|
+
|
18
|
+
@battery = json_response.dig('asset_details', 'sensor_data', 'battery', 'value')
|
19
|
+
sensor_data = json_response.dig('asset_details', 'sensor_data')
|
20
|
+
|
21
|
+
@sensor_data = sensor_data
|
22
|
+
|
23
|
+
@network = json_response['network']
|
24
|
+
|
25
|
+
create_getters
|
26
|
+
end
|
27
|
+
|
28
|
+
def available_sensor_data
|
29
|
+
# returns a hash with available senson data
|
30
|
+
sensor_data_temp = self.sensor_data.keys
|
31
|
+
# if no timestamp is available in the GPSPosition, then there wasn't a last_gps_measurement returned in the json
|
32
|
+
# hence the sensor doesn't contain a GPS module.
|
33
|
+
sensor_data_temp = sensor_data_temp.unshift('gps') if self.last_gps_position.respond_to? :timestamp
|
34
|
+
sensor_data_temp
|
35
|
+
end
|
36
|
+
|
37
|
+
def website_url
|
38
|
+
"https://app.ax-track.ch/#/map/assets/#{@tracker_id}"
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -1,53 +1,39 @@
|
|
1
1
|
module AxTrack
|
2
2
|
class Tracker < Object
|
3
3
|
|
4
|
-
def
|
5
|
-
@
|
6
|
-
@
|
7
|
-
|
8
|
-
@url = json_response['url']
|
9
|
-
@active = json_response['active']
|
10
|
-
@model = json_response['model']
|
11
|
-
@asset_details = Asset.new json_response['asset_details'] if json_response['asset_details']
|
12
|
-
@name = json_response.dig('asset_details', 'name')
|
13
|
-
@last_message_timestamp = DateTime.parse(json_response['last_message_timestamp'], false) if json_response['last_message_timestamp']
|
14
|
-
@url = json_response['url']
|
15
|
-
@user_url = website_url
|
16
|
-
@last_gps_position = GPSPosition.new(json_response['last_gps_measurement'] || json_response['asset_details'])
|
17
|
-
|
18
|
-
@battery = json_response.dig('asset_details', 'sensor_data', 'battery', 'value')
|
19
|
-
sensor_data = json_response.dig('asset_details', 'sensor_data')
|
4
|
+
def asset_details
|
5
|
+
@asset_details = Asset.new @asset_details unless @asset_details.is_a? Asset
|
6
|
+
@asset_details
|
7
|
+
end
|
20
8
|
|
21
|
-
|
9
|
+
def name
|
10
|
+
@name = @asset_details['name']
|
11
|
+
end
|
22
12
|
|
23
|
-
|
13
|
+
def last_message_timestamp
|
14
|
+
DateTime.parse(@last_message_timestamp, false) if @last_message_timestamp
|
15
|
+
end
|
24
16
|
|
25
|
-
|
17
|
+
def last_gps_position
|
18
|
+
#GPSPosition.new(@last_gps_measurement || { lat: asset_details.lat, lng: asset_details.lng } )
|
19
|
+
GPSPosition.new(@last_gps_measurement)
|
26
20
|
end
|
27
21
|
|
28
|
-
def
|
29
|
-
|
30
|
-
sensor_data_temp = self.sensor_data.keys
|
31
|
-
sensor_data_temp = sensor_data_temp.unshift('gps') if self.respond_to? :last_gps_position
|
32
|
-
sensor_data_temp
|
22
|
+
def battery
|
23
|
+
asset_details&.sensor_data.dig('battery', 'value')
|
33
24
|
end
|
34
25
|
|
35
26
|
def website_url
|
36
27
|
"https://app.ax-track.ch/#/map/assets/#{@tracker_id}"
|
37
28
|
end
|
38
29
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
create_getters
|
48
|
-
end
|
49
|
-
|
30
|
+
def available_sensor_data
|
31
|
+
# returns a hash with available senson data
|
32
|
+
sensor_data_temp = self.sensor_data.keys
|
33
|
+
# if no timestamp is available in the GPSPosition, then there wasn't a last_gps_measurement returned in the json
|
34
|
+
# hence the sensor doesn't contain a GPS module.
|
35
|
+
sensor_data_temp = sensor_data_temp.unshift('gps') if self.last_gps_position.respond_to? :timestamp
|
36
|
+
sensor_data_temp
|
50
37
|
end
|
51
|
-
|
52
38
|
end
|
53
39
|
end
|
data/lib/ax_track/resource.rb
CHANGED
@@ -3,14 +3,13 @@ module AxTrack
|
|
3
3
|
class Resource
|
4
4
|
attr_reader :client, :response
|
5
5
|
|
6
|
-
|
7
|
-
BadRequestError = Class.new(
|
8
|
-
UnauthorizedError = Class.new(
|
9
|
-
ForbiddenError = Class.new(
|
10
|
-
ApiRequestsQuotaReachedError = Class.new(
|
11
|
-
NotFoundError = Class.new(
|
12
|
-
UnprocessableEntityError = Class.new(
|
13
|
-
ApiError = Class.new(GithubAPIError)
|
6
|
+
ApiError = Class.new(StandardError)
|
7
|
+
BadRequestError = Class.new(ApiError)
|
8
|
+
UnauthorizedError = Class.new(ApiError)
|
9
|
+
ForbiddenError = Class.new(ApiError)
|
10
|
+
ApiRequestsQuotaReachedError = Class.new(ApiError)
|
11
|
+
NotFoundError = Class.new(ApiError)
|
12
|
+
UnprocessableEntityError = Class.new(ApiError)
|
14
13
|
|
15
14
|
HTTP_OK_CODE = 200
|
16
15
|
|
@@ -28,6 +27,14 @@ module AxTrack
|
|
28
27
|
def request(http_method: :get, endpoint:, headers: {}, params: {}, body: {}, result_subset: nil)
|
29
28
|
raise "Client not defined" unless defined? @client
|
30
29
|
endpoint = endpoint + "/" unless endpoint[-1] == "/"
|
30
|
+
|
31
|
+
body['picture'] = Faraday::UploadIO.new(
|
32
|
+
body['picture'].tempfile.path,
|
33
|
+
body['picture'].content_type,
|
34
|
+
body['picture'].filename
|
35
|
+
) if body.key? :picture
|
36
|
+
|
37
|
+
# client.connection['headers']['Content-Type'] = 'multipart/form-data' if body.key? :picture
|
31
38
|
@response = client.connection.public_send(http_method, endpoint, params.merge(body))
|
32
39
|
|
33
40
|
unless response_successful?
|
@@ -43,9 +50,7 @@ module AxTrack
|
|
43
50
|
BadRequestError
|
44
51
|
when HTTP_UNAUTHORIZED_CODE
|
45
52
|
UnauthorizedError
|
46
|
-
when HTTP_FORBIDDEN_CODE
|
47
|
-
ForbiddenError
|
48
|
-
when HTTP_NOT_FOUND_CODE
|
53
|
+
when HTTP_NOT_FOUND_CODE, HTTP_FORBIDDEN_CODE
|
49
54
|
NotFoundError
|
50
55
|
when HTTP_UNPROCESSABLE_ENTITY_CODE
|
51
56
|
UnprocessableEntityError
|
@@ -13,10 +13,11 @@ module AxTrack
|
|
13
13
|
endpoint: "assets/#{asset_id}").body
|
14
14
|
end
|
15
15
|
|
16
|
-
def update(asset_id, **attributes)
|
16
|
+
def update(asset_id, headers: {}, **attributes)
|
17
17
|
Asset.new request(http_method: :patch,
|
18
18
|
endpoint: "assets/#{asset_id}",
|
19
|
-
body: attributes
|
19
|
+
body: attributes,
|
20
|
+
headers: {}).body
|
20
21
|
|
21
22
|
end
|
22
23
|
end
|
data/lib/ax_track/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ax-track
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Philipp Baumann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10-
|
11
|
+
date: 2021-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -63,7 +63,9 @@ files:
|
|
63
63
|
- lib/ax_track/error.rb
|
64
64
|
- lib/ax_track/object.rb
|
65
65
|
- lib/ax_track/objects/asset.rb
|
66
|
+
- lib/ax_track/objects/tracker.old
|
66
67
|
- lib/ax_track/objects/tracker.rb
|
68
|
+
- lib/ax_track/objects/tracker/gps_position.rb
|
67
69
|
- lib/ax_track/resource.rb
|
68
70
|
- lib/ax_track/resources/asset_resource.rb
|
69
71
|
- lib/ax_track/resources/tracker_resource.rb
|