my_john_deere_api 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc9478ddbe0b4aa4f4e1d20f822db028cbc13154cd158b2cf937be9a18d4279a
4
- data.tar.gz: ca8f3397f9d3ec5156c7a346d77c719e3ef427c166c6aa8d6db1a8ba8269c35f
3
+ metadata.gz: c526296200ecd51d61d4bb113a1f224791ce52d4cacd1456be97b6c847404d45
4
+ data.tar.gz: 46e1aed00e81a3b14be252fdf206d8e33aa93ed7ccaff2766378fa3770f23e10
5
5
  SHA512:
6
- metadata.gz: 8f910a45e19494e09476f2cb506403e94df8eb86eb6816db23d6d2aeabe02a6ddff7ec5e4bdc35a59b3d2a53857a2cf86949ed1109c19490149ca5857d158bbe
7
- data.tar.gz: a10d6662be6a6fd3d63acc2e8e5c6bbd41fe4e74243c5f0ad3c41aef6dcf65f740be5507e761723da879a380fcfbe9b9744dac8a79024882632c1f1300371229
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).all
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
- # Parsed timestamp
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
- @timestamp = attributes[:timestamp].is_a?(String) ?
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
- # Parse geometry
35
-
36
- def geometry
37
- return @geometry if defined?(@geometry)
41
+ # Custom-process geometry
38
42
 
39
- @geometry = if attributes[:geometry]
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.keys.include?(attr)
99
+ errors[attr] = 'is required' unless attributes[attr]
89
100
  end
90
101
  end
91
102
 
@@ -1,3 +1,3 @@
1
1
  module MyJohnDeereApi
2
- VERSION='0.8.0'
2
+ VERSION='0.8.1'
3
3
  end
@@ -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
- assert_kind_of Array, locations
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.send(:geometry)
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.send(:timestamp)).to_time.to_i
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: my_john_deere_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime. Bellmyer