ax-track 0.1.14 → 0.2.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c016c0f8b6b21a305c193d350b3a1bf003bc4022da5116c56eb9eb46bd87689
4
- data.tar.gz: fb56d8a6ad760e23c4d90d513d28d1bc46a06832cc8fe25e18e0332556021fac
3
+ metadata.gz: 6f78da793c4f53b05b464fc50ee9706fdcfee25b4d72feadc27ef0e60e1a868e
4
+ data.tar.gz: 54cabd6e87be27a30eb0826dd142b5c80ff4cd69a361b12ff437e18fb38e11d1
5
5
  SHA512:
6
- metadata.gz: '02385bde367ee4eb289955f65e8516d9e0b548ba4e5539d6da4bd1838307c99fdcc1940f6fa53460887ddb821fb95939b4fe8de224627cd0c6ecc4b148eecd69'
7
- data.tar.gz: ec3105555b83b46efecf619c04794e9e7873620fffaa82d7574d6a309b32949e93c84f929714687ca17b7cf798c920e54ed3f3d2c46b3cc4caf941fe509e00f8
6
+ metadata.gz: 7dc4c3bf7479eff86ac3f04b38520c1b2d0252b180f1cb34e27e476749b29c982ccfa579f2d7c98079a47e53c0d7a4f09b190b7b5baf568de45a60eb4b949208
7
+ data.tar.gz: cb6c264e58bc56f5cfcd816a6cd0a728451dbba2193c33e94ed3ab204fbf53732dff6575a455f2523c3b3a2c8170e81c2507a586e0e74fc02ee3f9ce607041b8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ax-track (0.1.14)
4
+ ax-track (0.2.2)
5
5
  faraday (~> 1.7)
6
6
  faraday_middleware (~> 1.1)
7
7
 
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'
@@ -12,9 +12,12 @@ module AxTrack
12
12
  APIKeyMissing = Class.new(StandardError)
13
13
 
14
14
  attr_reader :api_key, :adapter
15
- def initialize(api_key: ENV['AXTRACK_API_KEY'], adapter: Faraday.default_adapter)
16
- @api_key = api_key
17
- @adapter = adapter
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 Faraday.default_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
@@ -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
- data: body[key].map { |attrs| type.new(attrs ) },
11
- total: body.dig('count'),
12
- next_cursor: body.dig('next'),
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 (data:, total:, next_cursor:, prev_cursor:)
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
@@ -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,28 +1,38 @@
1
1
  module AxTrack
2
2
  class Tracker < Object
3
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'])
4
+ def asset_details
5
+ @asset_details = Asset.new @asset_details unless @asset_details.is_a? Asset
6
+ @asset_details
7
+ end
8
+
9
+ def asset_id
10
+ asset_details.id
11
+ end
12
+
13
+ def name
14
+ @name = @asset_details['name']
15
+ end
16
+
17
+ def last_message_timestamp
18
+ DateTime.parse(@last_message_timestamp, false) if @last_message_timestamp
19
+ end
17
20
 
18
- @battery = json_response.dig('asset_details', 'sensor_data', 'battery', 'value')
19
- sensor_data = json_response.dig('asset_details', 'sensor_data')
21
+ def last_gps_position
22
+ #GPSPosition.new(@last_gps_measurement || { lat: asset_details.lat, lng: asset_details.lng } )
23
+ GPSPosition.new(@last_gps_measurement)
24
+ end
20
25
 
21
- @sensor_data = sensor_data
26
+ def battery
27
+ asset_details&.sensor_data.dig('battery', 'value')
28
+ end
22
29
 
23
- @network = json_response['network']
30
+ def sensor_data
31
+ asset_details.sensor_data
32
+ end
24
33
 
25
- create_getters
34
+ def website_url
35
+ "https://app.ax-track.ch/#/map/assets/#{@tracker_id}"
26
36
  end
27
37
 
28
38
  def available_sensor_data
@@ -33,23 +43,5 @@ module AxTrack
33
43
  sensor_data_temp = sensor_data_temp.unshift('gps') if self.last_gps_position.respond_to? :timestamp
34
44
  sensor_data_temp
35
45
  end
36
-
37
- def website_url
38
- "https://app.ax-track.ch/#/map/assets/#{@tracker_id}"
39
- end
40
-
41
-
42
- class GPSPosition < Object
43
-
44
- def initialize(json_response)
45
- @lat = json_response['lat']
46
- @lng = json_response['lng']
47
- @timestamp = DateTime.parse(json_response['timestamp'], false) if json_response['timestamp']
48
-
49
- create_getters
50
- end
51
-
52
- end
53
-
54
46
  end
55
47
  end
@@ -10,7 +10,6 @@ module AxTrack
10
10
  ApiRequestsQuotaReachedError = Class.new(ApiError)
11
11
  NotFoundError = Class.new(ApiError)
12
12
  UnprocessableEntityError = Class.new(ApiError)
13
- ApiError = 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?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AxTrack
4
- VERSION = "0.1.14"
4
+ VERSION = "0.2.2"
5
5
  end
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.14
4
+ version: 0.2.2
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-07 00:00:00.000000000 Z
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