my_john_deere_api 0.5.0 → 0.5.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: 8f27839609fe2e86e82d918edae99dd77d78cb9d7dad78c061d8ab198837953d
4
- data.tar.gz: b302429624cc3f1a504e77235c105f94af9363bc075809f46f1659bf1b59859c
3
+ metadata.gz: 73cbc5de675267d73f21bf1adc0624236ad833ff0e7174dcec7e7198d9b1d1cb
4
+ data.tar.gz: '0840ae125650b4f307f74e21b4b255d5bc50878fd748ec1e1835bbf2e17c885c'
5
5
  SHA512:
6
- metadata.gz: 3b2509a126cf7aef80dc4984865660eb20285a4ccbb5aaec61a622449eebc3eb0628d1a44f3de78bfc95af42f639bbecd5a5796219b3d8be3ea4be7612e90b96
7
- data.tar.gz: 2b401cacbc5c4fc8e9b224c89639689ceb681d8b697dfdd1b1f3ff62b1b91c5ea40508a2de61ade9ad8744773a90358067e9240faaa3f4088803c3caac1eb196
6
+ metadata.gz: 07e8d738e0ecc1e3ba071cf4e09d61be831e8851c190bc805d9cc11903a445126d0a66f5c414043d4cea3bf203c014d8bd377bde18ea79ba6312fbeda3a061d7
7
+ data.tar.gz: d590a803bf509ac80ff6dc3ab1645ccacc580a733ca9cf8d53bb6d5e7ea1cfd4f2d61a6fc495ca7dc8d509fc1dede65307dbd59cb0024dd4b65d1f466e2d5af8
@@ -1,3 +1,4 @@
1
1
  module MyJohnDeereApi::Errors
2
2
  require 'my_john_deere_api/errors/access_token_error'
3
+ require 'my_john_deere_api/errors/type_mismatch_error'
3
4
  end
@@ -0,0 +1,12 @@
1
+ module MyJohnDeereApi
2
+ ##
3
+ # This error is used in a context that will fail in the absence of
4
+ # a valid oAuth access token. We have classes that may only need
5
+ # access tokens for some use cases.
6
+
7
+ class TypeMismatchError < StandardError
8
+ def initialize(message = "Record type does not match what was expected")
9
+ super
10
+ end
11
+ end
12
+ end
@@ -1,4 +1,5 @@
1
1
  module MyJohnDeereApi::Model
2
+ autoload :Base, 'my_john_deere_api/model/base'
2
3
  autoload :Asset, 'my_john_deere_api/model/asset'
3
4
  autoload :AssetLocation, 'my_john_deere_api/model/asset_location'
4
5
  autoload :Organization, 'my_john_deere_api/model/organization'
@@ -1,34 +1,6 @@
1
1
  module MyJohnDeereApi
2
- class Model::Asset
3
- include Helpers::UriHelpers
4
-
5
- attr_reader :accessor, :id, :title, :asset_category, :asset_type, :asset_sub_type, :last_modified_date, :links
6
-
7
- ##
8
- # arguments:
9
- #
10
- # [record] a JSON object of type 'Field', returned from the API.
11
- #
12
- # [accessor (optional)] a valid oAuth Access Token. This is only
13
- # needed if further API requests are going
14
- # to be made, as is the case with *flags*.
15
-
16
- def initialize(record, accessor = nil)
17
- @accessor = accessor
18
-
19
- @id = record['id']
20
- @title = record['title']
21
- @asset_category = record['assetCategory']
22
- @asset_type = record['assetType']
23
- @asset_sub_type = record['assetSubType']
24
- @last_modified_date = record['lastModifiedDate']
25
-
26
- @links = {}
27
-
28
- record['links'].each do |association|
29
- @links[association['rel']] = uri_path(association['uri'])
30
- end
31
- end
2
+ class Model::Asset < Model::Base
3
+ attr_reader :title, :asset_category, :asset_type, :asset_sub_type, :last_modified_date
32
4
 
33
5
  ##
34
6
  # locations associated with this asset
@@ -39,5 +11,15 @@ module MyJohnDeereApi
39
11
  return @locations if defined?(@locations)
40
12
  @locations = MyJohnDeereApi::Request::Collection::AssetLocations.new(accessor, asset: id).all
41
13
  end
14
+
15
+ private
16
+
17
+ def map_attributes(record)
18
+ @title = record['title']
19
+ @asset_category = record['assetCategory']
20
+ @asset_type = record['assetType']
21
+ @asset_sub_type = record['assetSubType']
22
+ @last_modified_date = record['lastModifiedDate']
23
+ end
42
24
  end
43
25
  end
@@ -1,27 +1,15 @@
1
1
  require 'json'
2
2
 
3
3
  module MyJohnDeereApi
4
- class Model::AssetLocation
5
- include Helpers::UriHelpers
4
+ class Model::AssetLocation < Model::Base
5
+ attr_reader :timestamp, :geometry, :measurement_data
6
6
 
7
- attr_reader :accessor, :timestamp, :geometry, :measurement_data, :links
8
-
9
- ##
10
- # arguments:
11
- #
12
- # [record] a JSON object of type 'Field', returned from the API.
13
- #
14
- # [accessor (optional)] a valid oAuth Access Token. This is only
15
- # needed if further API requests are going
16
- # to be made, as is the case with *flags*.
17
-
18
- def initialize(record, accessor = nil)
19
- @accessor = accessor
7
+ private
20
8
 
9
+ def map_attributes(record)
21
10
  @timestamp = record['timestamp']
22
11
  @geometry = JSON.parse(record['geometry'])
23
12
  @measurement_data = record['measurementData']
24
- @links = {}
25
13
  end
26
14
  end
27
15
  end
@@ -0,0 +1,49 @@
1
+ module MyJohnDeereApi
2
+ class Model::Base
3
+ include Helpers::CaseConversion
4
+ include Helpers::UriHelpers
5
+
6
+ attr_reader :id, :record_type, :accessor, :links
7
+
8
+ ##
9
+ # arguments:
10
+ #
11
+ # [record] a JSON object of type 'Field', returned from the API.
12
+ #
13
+ # [accessor (optional)] a valid oAuth Access Token. This is only
14
+ # needed if further API requests are going
15
+ # to be made, as is the case with *flags*.
16
+
17
+ def initialize(record, accessor = nil)
18
+ @id = record['id']
19
+ @record_type = record['@type']
20
+ @accessor = accessor
21
+
22
+ map_attributes(record)
23
+
24
+ @links = {}
25
+
26
+ record['links'].each do |association|
27
+ @links[underscore(association['rel'])] = uri_path(association['uri'])
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ ##
34
+ # This method receives the full record hash and extracts whatever extra
35
+ # attributes are needed for the given base class. This is intended to
36
+ # be overridden by child classes instead of monkeypatching #initialize.
37
+
38
+ def map_attributes(record)
39
+
40
+ end
41
+
42
+ ##
43
+ # Expected record type. Override in child classes.
44
+
45
+ def expected_record_type
46
+ 'Base'
47
+ end
48
+ end
49
+ end
@@ -1,31 +1,6 @@
1
1
  module MyJohnDeereApi
2
- class Model::Field
3
- include Helpers::UriHelpers
4
-
5
- attr_reader :name, :id, :links, :accessor
6
-
7
- ##
8
- # arguments:
9
- #
10
- # [record] a JSON object of type 'Field', returned from the API.
11
- #
12
- # [accessor (optional)] a valid oAuth Access Token. This is only
13
- # needed if further API requests are going
14
- # to be made, as is the case with *flags*.
15
-
16
- def initialize(record, accessor = nil)
17
- @accessor = accessor
18
-
19
- @name = record['name']
20
- @id = record['id']
21
- @archived = record['archived']
22
-
23
- @links = {}
24
-
25
- record['links'].each do |association|
26
- @links[association['rel']] = uri_path(association['uri'])
27
- end
28
- end
2
+ class Model::Field < Model::Base
3
+ attr_reader :name
29
4
 
30
5
  ##
31
6
  # Since the archived attribute is boolean, we reflect this in the
@@ -47,6 +22,11 @@ module MyJohnDeereApi
47
22
 
48
23
  private
49
24
 
25
+ def map_attributes(record)
26
+ @name = record['name']
27
+ @archived = record['archived']
28
+ end
29
+
50
30
  ##
51
31
  # Infer the organization_id from the 'self' link
52
32
 
@@ -1,36 +1,8 @@
1
1
  require 'json'
2
2
 
3
3
  module MyJohnDeereApi
4
- class Model::Flag
5
- include Helpers::CaseConversion
6
- include Helpers::UriHelpers
7
-
8
- attr_reader :id, :notes, :geometry, :links, :accessor
9
-
10
- ##
11
- # arguments:
12
- #
13
- # [record] a JSON object of type 'Flag', returned from the API.
14
- #
15
- # [accessor (optional)] a valid oAuth Access Token. This is only
16
- # needed if further API requests are going
17
- # to be made.
18
-
19
- def initialize(record, accessor = nil)
20
- @accessor = accessor
21
-
22
- @id = record['id']
23
- @notes = record['notes']
24
- @geometry =JSON.parse(record['geometry'])
25
- @proximity_alert_enabled = record['proximityAlertEnabled']
26
- @archived = record['archived']
27
-
28
- @links = {}
29
-
30
- record['links'].each do |association|
31
- @links[underscore(association['rel'])] = uri_path(association['uri'])
32
- end
33
- end
4
+ class Model::Flag < Model::Base
5
+ attr_reader :notes, :geometry
34
6
 
35
7
  ##
36
8
  # Since the archived attribute is boolean, we reflect this in the
@@ -47,5 +19,14 @@ module MyJohnDeereApi
47
19
  def proximity_alert_enabled?
48
20
  @proximity_alert_enabled
49
21
  end
22
+
23
+ private
24
+
25
+ def map_attributes(record)
26
+ @notes = record['notes']
27
+ @geometry =JSON.parse(record['geometry'])
28
+ @proximity_alert_enabled = record['proximityAlertEnabled']
29
+ @archived = record['archived']
30
+ end
50
31
  end
51
32
  end
@@ -1,34 +1,8 @@
1
1
  require 'uri'
2
2
 
3
3
  module MyJohnDeereApi
4
- class Model::Organization
5
- include Helpers::UriHelpers
6
-
7
- attr_reader :name, :type, :id, :links, :accessor
8
-
9
- ##
10
- # arguments:
11
- #
12
- # [record] a JSON object of type 'Organization', returned from the API.
13
- #
14
- # [accessor (optional)] a valid oAuth Access Token. This is only
15
- # needed if further API requests are going
16
- # to be made, as is the case with *fields*.
17
-
18
- def initialize(record, accessor = nil)
19
- @accessor = accessor
20
-
21
- @name = record['name']
22
- @type = record['type']
23
- @id = record['id']
24
- @member = record['member']
25
-
26
- @links = {}
27
-
28
- record['links'].each do |association|
29
- @links[association['rel']] = uri_path(association['uri'])
30
- end
31
- end
4
+ class Model::Organization < Model::Base
5
+ attr_reader :name, :type
32
6
 
33
7
  ##
34
8
  # Since the member attribute is boolean, we reflect this in the
@@ -57,5 +31,13 @@ module MyJohnDeereApi
57
31
  return @assets if defined?(@assets)
58
32
  @assets = MyJohnDeereApi::Request::Collection::Assets.new(accessor, organization: id).all
59
33
  end
34
+
35
+ private
36
+
37
+ def map_attributes(record)
38
+ @name = record['name']
39
+ @type = record['type']
40
+ @member = record['member']
41
+ end
60
42
  end
61
43
  end
@@ -1,3 +1,3 @@
1
1
  module MyJohnDeereApi
2
- VERSION='0.5.0'
2
+ VERSION='0.5.1'
3
3
  end
@@ -0,0 +1,13 @@
1
+ require 'support/helper'
2
+
3
+ describe 'MyJohnDeereApi::TypeMismatchError' do
4
+ it 'inherits from StandardError' do
5
+ error = MyJohnDeereApi::TypeMismatchError.new
6
+ assert_kind_of StandardError, error
7
+ end
8
+
9
+ it 'has a default message' do
10
+ error = MyJohnDeereApi::TypeMismatchError.new
11
+ assert_includes error.message, 'Record type does not match'
12
+ end
13
+ end
@@ -5,5 +5,9 @@ describe 'MyJohnDeereApi Errors' do
5
5
  it 'loads AccessTokenError' do
6
6
  assert JD::AccessTokenError
7
7
  end
8
+
9
+ it 'loads TypeMismatchError' do
10
+ assert JD::TypeMismatchError
11
+ end
8
12
  end
9
13
  end
@@ -0,0 +1,64 @@
1
+ require 'support/helper'
2
+
3
+ class SampleModel < JD::Model::Base
4
+ attr_reader :somefield
5
+
6
+ def map_attributes(record)
7
+ @somefield = record['somefield']
8
+ end
9
+ end
10
+
11
+ describe 'MyJohnDeereApi::Model::Base' do
12
+ let(:object) { JD::Model::Base.new(record, accessor) }
13
+ let(:accessor) { 'accessor' }
14
+
15
+ let(:record) do
16
+ {
17
+ '@type'=>'Base',
18
+ 'id'=>'123',
19
+ 'somefield'=>'somevalue',
20
+ "links"=>[
21
+ {"@type"=>"Link", "rel"=>"self", "uri"=>"https://sandboxapi.deere.com/platform/assets/123456"},
22
+ {"@type"=>"Link", "rel"=>"organization", "uri"=>"https://sandboxapi.deere.com/platform/organizations/234567"},
23
+ {"@type"=>"Link", "rel"=>"locations", "uri"=>"https://sandboxapi.deere.com/platform/assets/123456/locations"},
24
+ ]
25
+ }
26
+ end
27
+
28
+ it 'includes UriHelpers' do
29
+ assert_includes object.private_methods, :uri_path
30
+ assert_includes object.private_methods, :id_from_uri
31
+ end
32
+
33
+ describe '#initialize(record, accessor = nil)' do
34
+ def link_for label
35
+ record['links'].detect{|link| link['rel'] == label}['uri'].gsub('https://sandboxapi.deere.com/platform', '')
36
+ end
37
+
38
+ it 'sets the base attributes' do
39
+ assert_equal record['id'], object.id
40
+ assert_equal record['@type'], object.record_type
41
+ assert_equal accessor, object.accessor
42
+ end
43
+
44
+ it 'sets the links' do
45
+ links = object.links
46
+
47
+ assert_kind_of Hash, links
48
+
49
+ ['self', 'organization', 'locations'].each do |link|
50
+ assert_equal link_for(link), links[link]
51
+ end
52
+ end
53
+
54
+ it 'maps additional fields in subclasses' do
55
+ object = SampleModel.new(record)
56
+ assert_equal 'somevalue', object.somefield
57
+ end
58
+
59
+ it 'does not require accessor' do
60
+ object = JD::Model::Base.new(record)
61
+ assert_nil object.accessor
62
+ end
63
+ end
64
+ end
@@ -2,6 +2,10 @@ require 'support/helper'
2
2
 
3
3
  describe 'MyJohnDeereApi::Model' do
4
4
  describe 'loading dependencies' do
5
+ it 'loads Model::Base' do
6
+ assert JD::Model::Base
7
+ end
8
+
5
9
  it 'loads Model::Asset' do
6
10
  assert JD::Model::Asset
7
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: my_john_deere_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime. Bellmyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-10 00:00:00.000000000 Z
11
+ date: 2020-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: vcr
@@ -101,12 +101,14 @@ files:
101
101
  - lib/my_john_deere_api/consumer.rb
102
102
  - lib/my_john_deere_api/errors.rb
103
103
  - lib/my_john_deere_api/errors/access_token_error.rb
104
+ - lib/my_john_deere_api/errors/type_mismatch_error.rb
104
105
  - lib/my_john_deere_api/helpers.rb
105
106
  - lib/my_john_deere_api/helpers/case_conversion.rb
106
107
  - lib/my_john_deere_api/helpers/uri_helpers.rb
107
108
  - lib/my_john_deere_api/model.rb
108
109
  - lib/my_john_deere_api/model/asset.rb
109
110
  - lib/my_john_deere_api/model/asset_location.rb
111
+ - lib/my_john_deere_api/model/base.rb
110
112
  - lib/my_john_deere_api/model/field.rb
111
113
  - lib/my_john_deere_api/model/flag.rb
112
114
  - lib/my_john_deere_api/model/organization.rb
@@ -123,12 +125,14 @@ files:
123
125
  - test/lib/my_john_deere_api/client_test.rb
124
126
  - test/lib/my_john_deere_api/consumer_test.rb
125
127
  - test/lib/my_john_deere_api/errors/access_token_error_test.rb
128
+ - test/lib/my_john_deere_api/errors/type_mismatch_error_test.rb
126
129
  - test/lib/my_john_deere_api/errors_test.rb
127
130
  - test/lib/my_john_deere_api/helpers/case_conversion_test.rb
128
131
  - test/lib/my_john_deere_api/helpers/uri_helpers_test.rb
129
132
  - test/lib/my_john_deere_api/helpers_test.rb
130
133
  - test/lib/my_john_deere_api/model/asset_location_test.rb
131
134
  - test/lib/my_john_deere_api/model/asset_test.rb
135
+ - test/lib/my_john_deere_api/model/base_test.rb
132
136
  - test/lib/my_john_deere_api/model/field_test.rb
133
137
  - test/lib/my_john_deere_api/model/flag_test.rb
134
138
  - test/lib/my_john_deere_api/model/organization_test.rb