my_john_deere_api 0.8.0 → 0.8.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/lib/my_john_deere_api/model/asset.rb +1 -1
- data/lib/my_john_deere_api/request/create/asset_location.rb +20 -15
- data/lib/my_john_deere_api/request/create/base.rb +12 -1
- data/lib/my_john_deere_api/version.rb +1 -1
- data/test/lib/my_john_deere_api/model/asset_test.rb +6 -2
- data/test/lib/my_john_deere_api/request/create/asset_location_test.rb +2 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c526296200ecd51d61d4bb113a1f224791ce52d4cacd1456be97b6c847404d45
|
4
|
+
data.tar.gz: 46e1aed00e81a3b14be252fdf206d8e33aa93ed7ccaff2766378fa3770f23e10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19f044e3ae20c63c28dcc4c30f28b79ba27159fd49b7bda254958707c6602e1c5418f8d7e328f520876ad5677958551c923eb3502044e61a3598363f868d8a84
|
7
|
+
data.tar.gz: dce51ce51507f141c1248fb5fe0e22f0a61aaecaa8fad56faa1a65f29748a9bb14827917ad34dceba1c51489f1b4e40a0aa225f64dd1009910fe265b6ef4b96c
|
@@ -9,7 +9,7 @@ module MyJohnDeereApi
|
|
9
9
|
raise AccessTokenError unless accessor
|
10
10
|
|
11
11
|
return @locations if defined?(@locations)
|
12
|
-
@locations = MyJohnDeereApi::Request::Collection::AssetLocations.new(accessor, asset: id)
|
12
|
+
@locations = MyJohnDeereApi::Request::Collection::AssetLocations.new(accessor, asset: id)
|
13
13
|
end
|
14
14
|
|
15
15
|
private
|
@@ -4,6 +4,15 @@ module MyJohnDeereApi
|
|
4
4
|
class Request::Create::AssetLocation < Request::Create::Base
|
5
5
|
private
|
6
6
|
|
7
|
+
##
|
8
|
+
# Set defaults and generate some attributes from others.
|
9
|
+
# Overridden from parent class.
|
10
|
+
|
11
|
+
def process_attributes
|
12
|
+
process_timestamp
|
13
|
+
process_geometry
|
14
|
+
end
|
15
|
+
|
7
16
|
##
|
8
17
|
# Request body
|
9
18
|
|
@@ -11,32 +20,28 @@ module MyJohnDeereApi
|
|
11
20
|
return @body if defined?(@body)
|
12
21
|
|
13
22
|
@body = [{
|
14
|
-
timestamp: timestamp,
|
15
|
-
geometry: geometry,
|
23
|
+
timestamp: attributes[:timestamp],
|
24
|
+
geometry: attributes[:geometry],
|
16
25
|
measurementData: attributes[:measurement_data]
|
17
26
|
}]
|
18
27
|
end
|
19
28
|
|
20
29
|
##
|
21
|
-
#
|
22
|
-
|
23
|
-
def timestamp
|
24
|
-
return @timestamp if defined?(@timestamp)
|
30
|
+
# Custom-process timestamp
|
25
31
|
|
32
|
+
def process_timestamp
|
26
33
|
attributes[:timestamp] ||= Time.now.utc
|
27
34
|
|
28
|
-
|
35
|
+
attributes[:timestamp] = attributes[:timestamp].is_a?(String) ?
|
29
36
|
attributes[:timestamp] :
|
30
37
|
attributes[:timestamp].strftime('%Y-%m-%dT%H:%M:%SZ')
|
31
38
|
end
|
32
39
|
|
33
40
|
##
|
34
|
-
#
|
35
|
-
|
36
|
-
def geometry
|
37
|
-
return @geometry if defined?(@geometry)
|
41
|
+
# Custom-process geometry
|
38
42
|
|
39
|
-
|
43
|
+
def process_geometry
|
44
|
+
attributes[:geometry] = if attributes[:geometry]
|
40
45
|
attributes[:geometry].is_a?(String) ?
|
41
46
|
attributes[:geometry] :
|
42
47
|
attributes[:geometry].to_json
|
@@ -89,8 +94,8 @@ module MyJohnDeereApi
|
|
89
94
|
# API will only accept a timestamp *range*, and the start must be lower
|
90
95
|
# than the end. We buffer start/end times by one second, then find the
|
91
96
|
# exact match.
|
92
|
-
start_date = timestamp_add(timestamp, -1)
|
93
|
-
end_date = timestamp_add(timestamp, 1)
|
97
|
+
start_date = timestamp_add(attributes[:timestamp], -1)
|
98
|
+
end_date = timestamp_add(attributes[:timestamp], 1)
|
94
99
|
path += "?startDate=#{start_date}&endDate=#{end_date}"
|
95
100
|
|
96
101
|
result = accessor.get(path, headers)
|
@@ -98,7 +103,7 @@ module MyJohnDeereApi
|
|
98
103
|
# Timestamps are returned with seconds in decimals, even though these
|
99
104
|
# are always zero. So we compare actual DateTime objects parsed from
|
100
105
|
# the timestamp strings.
|
101
|
-
parsed_stamp = DateTime.parse(timestamp)
|
106
|
+
parsed_stamp = DateTime.parse(attributes[:timestamp])
|
102
107
|
|
103
108
|
JSON.parse(result.body)['values'].detect do |record|
|
104
109
|
parsed_stamp == DateTime.parse(record['timestamp'])
|
@@ -21,6 +21,8 @@ module MyJohnDeereApi
|
|
21
21
|
@accessor = accessor
|
22
22
|
@attributes = attributes
|
23
23
|
|
24
|
+
process_attributes
|
25
|
+
|
24
26
|
@errors = {}
|
25
27
|
end
|
26
28
|
|
@@ -29,6 +31,7 @@ module MyJohnDeereApi
|
|
29
31
|
|
30
32
|
def request
|
31
33
|
validate!
|
34
|
+
|
32
35
|
@response = accessor.post(resource, request_body.to_json, headers)
|
33
36
|
end
|
34
37
|
|
@@ -73,6 +76,14 @@ module MyJohnDeereApi
|
|
73
76
|
|
74
77
|
private
|
75
78
|
|
79
|
+
##
|
80
|
+
# Convert inputs into working attributes. This allows us to auto-create
|
81
|
+
# some attributes from others, or set defaults, on a class-by-class basis.
|
82
|
+
# See Request::Create::AssetLocation for an example.
|
83
|
+
|
84
|
+
def process_attributes
|
85
|
+
end
|
86
|
+
|
76
87
|
##
|
77
88
|
# Attributes that must be specified, override in child class
|
78
89
|
|
@@ -85,7 +96,7 @@ module MyJohnDeereApi
|
|
85
96
|
|
86
97
|
def validate_required
|
87
98
|
required_attributes.each do |attr|
|
88
|
-
errors[attr] = 'is required' unless attributes
|
99
|
+
errors[attr] = 'is required' unless attributes[attr]
|
89
100
|
end
|
90
101
|
end
|
91
102
|
|
@@ -60,9 +60,13 @@ describe 'MyJohnDeereApi::Model::Asset' do
|
|
60
60
|
accessor
|
61
61
|
organization = VCR.use_cassette('get_organizations') { client.organizations.first }
|
62
62
|
asset = VCR.use_cassette('get_assets') { organization.assets.first }
|
63
|
-
locations = VCR.use_cassette('get_asset_locations') { asset.locations }
|
64
63
|
|
65
|
-
|
64
|
+
locations = VCR.use_cassette('get_asset_locations') do
|
65
|
+
asset.locations.all
|
66
|
+
asset.locations
|
67
|
+
end
|
68
|
+
|
69
|
+
assert_kind_of Array, locations.all
|
66
70
|
|
67
71
|
locations.each do |location|
|
68
72
|
assert_kind_of JD::Model::AssetLocation, location
|
@@ -76,7 +76,7 @@ describe 'MyJohnDeereApi::Request::Create::AssetLocation' do
|
|
76
76
|
}
|
77
77
|
}.to_json
|
78
78
|
|
79
|
-
assert_equal expected_geometry, object.
|
79
|
+
assert_equal expected_geometry, object.attributes[:geometry]
|
80
80
|
end
|
81
81
|
|
82
82
|
it 'defaults timestamp to current time' do
|
@@ -84,7 +84,7 @@ describe 'MyJohnDeereApi::Request::Create::AssetLocation' do
|
|
84
84
|
object = JD::Request::Create::AssetLocation.new(accessor, attributes)
|
85
85
|
|
86
86
|
expected_stamp = Time.now.utc.to_i
|
87
|
-
actual_stamp = DateTime.parse(object.
|
87
|
+
actual_stamp = DateTime.parse(object.attributes[:timestamp]).to_time.to_i
|
88
88
|
|
89
89
|
assert_in_delta expected_stamp, actual_stamp, 1
|
90
90
|
end
|
@@ -105,13 +105,6 @@ describe 'MyJohnDeereApi::Request::Create::AssetLocation' do
|
|
105
105
|
assert_equal 'is required', object.errors[:asset_id]
|
106
106
|
end
|
107
107
|
|
108
|
-
it 'requires timestamp' do
|
109
|
-
object = JD::Request::Create::AssetLocation.new(accessor, attributes_without(:timestamp))
|
110
|
-
|
111
|
-
refute object.valid?
|
112
|
-
assert_equal 'is required', object.errors[:timestamp]
|
113
|
-
end
|
114
|
-
|
115
108
|
it 'requires geometry' do
|
116
109
|
object = JD::Request::Create::AssetLocation.new(accessor, attributes_without(:geometry))
|
117
110
|
|