my_john_deere_api 1.3.5 → 1.3.6

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: 7f60f0a940a61665a81f8e64a2cbb8762beb8136e4e37f13a0ebf66ead20fcac
4
- data.tar.gz: 45fb772147464452fdfd3ab37b75548ed9708369d374ba5c69c20ad03a428f51
3
+ metadata.gz: a060cb86023a65bf70b80bf717e86c725cb436e13d31d88bbd9148a235ab03c2
4
+ data.tar.gz: 95198638b33c2402679745aa2c43322af1fe039b446388b1ec800b3f8be451c7
5
5
  SHA512:
6
- metadata.gz: 0166fd3c8b08975d9c5b548f459710bebd2cca464ffad209abe812e5c01c4057a4ec980faf11d0a8201b21fe4d6a24cd6ffe55a86ec70ac87fda429347b55ec7
7
- data.tar.gz: b6e4f47f306adfaf52b6bf05054d11285f2a2382ce7f846e86479e6f12eda3bc7194874140036efa35e992f2132906bcd0349ecb58a17c5f6403b7818ac011fd
6
+ metadata.gz: db67993c3685f8e9a2519fefdc2e790a5d94c6e7accca6c94fc4a6daa9fe4ec6009e9eb23e12adfb987fd437038bd861a9542516a0a9a8e7089dd071ee59b1a6
7
+ data.tar.gz: 4bc3a3fe87d49f052c87d3f5fa746886a7c9b2edfb7db9972a507334c212d232c9d4091ae2f3c594864050ec5ce8bf9689762a2201ec7d16554dc67e50ec4488
@@ -2,6 +2,19 @@ module MyJohnDeereApi
2
2
  class Model::Asset < Model::Base
3
3
  attr_reader :title, :asset_category, :asset_type, :asset_sub_type, :last_modified_date
4
4
 
5
+ ##
6
+ # A listing of attributes that can be passed back to John Deere
7
+
8
+ def attributes
9
+ {
10
+ id: id,
11
+ title: title,
12
+ asset_category: asset_category,
13
+ asset_type: asset_type,
14
+ asset_sub_type: asset_sub_type
15
+ }
16
+ end
17
+
5
18
  ##
6
19
  # locations associated with this asset
7
20
 
@@ -1,89 +1,37 @@
1
1
  require 'json'
2
2
 
3
- module MyJohnDeereApi::Request
4
- module Update::Asset
5
- # ##
6
- # # Make the request, if the instance is valid
7
- #
8
- # # def request
9
- # # validate!
10
- # #
11
- # # @response = accessor.post(resource, request_body.to_json, headers)
12
- # # end
13
- #
14
- # ##
15
- # # Object created by request
16
- #
17
- # # def object
18
- # # return @object if defined?(@object)
19
- # #
20
- # # request unless response
21
- # #
22
- # # @object = model.new(fetch_record, accessor)
23
- # # end
24
- #
25
- # ##
26
- # # Runs validations, adding to the errors hash as needed. Returns true
27
- # # if the errors hash is still empty after all validations have been run.
28
- #
29
- # # def valid?
30
- # # return @is_valid if defined?(@is_valid)
31
- # #
32
- # # validate_required
33
- # # validate_attributes
34
- # #
35
- # # @is_valid = errors.empty?
36
- # # end
37
- #
38
- # ##
39
- # # Raises an error if the record is invalid. Passes the errors hash
40
- # # to the error, in order to build a useful message string.
41
- #
42
- # # def validate!
43
- # # raise(InvalidRecordError, errors) unless valid?
44
- # # end
45
- #
46
- # private
47
- #
48
- # ##
49
- # # Run validations unique to a given model. This should be overridden
50
- # # by children where needed.
51
- #
52
- # # def validate_attributes
53
- # # end
54
- #
55
- # ##
56
- # # Convert inputs into working attributes. This allows us to auto-create
57
- # # some attributes from others, or set defaults, on a class-by-class basis.
58
- # # See Request::Create::AssetLocation for an example.
59
- #
60
- # def process_attributes
61
- # end
62
- #
63
- # ##
64
- # # Attributes that must be specified, override in child class
65
- #
66
- # def required_attributes
67
- # []
68
- # end
69
- #
70
- # ##
71
- # # Validates required attributes
72
- #
73
- # def validate_required
74
- # required_attributes.each do |attr|
75
- # errors[attr] = 'is required' unless attributes[attr]
76
- # end
77
- # end
78
- #
79
- # ##
80
- # # Headers for PUT request
81
- #
82
- # def headers
83
- # @headers ||= {
84
- # 'Accept' => 'application/vnd.deere.axiom.v3+json',
85
- # 'Content-Type' => 'application/vnd.deere.axiom.v3+json'
86
- # }
87
- # end
3
+ module MyJohnDeereApi
4
+ class Request::Update::Asset < Request::Update::Base
5
+ include Validators::Asset
6
+
7
+ private
8
+
9
+ ##
10
+ # Path supplied to API
11
+
12
+ def resource
13
+ @resource ||= "/assets/#{attributes[:id]}"
14
+ end
15
+
16
+ ##
17
+ # Request body
18
+
19
+ def request_body
20
+ return @body if defined?(@body)
21
+
22
+ @body = {
23
+ title: attributes[:title],
24
+ assetCategory: attributes[:asset_category],
25
+ assetType: attributes[:asset_type],
26
+ assetSubType: attributes[:asset_sub_type],
27
+ links: [
28
+ {
29
+ '@type' => 'Link',
30
+ 'rel' => 'contributionDefinition',
31
+ 'uri' => "#{accessor.consumer.site}/contributionDefinitions/#{attributes[:contribution_definition_id]}"
32
+ }
33
+ ]
34
+ }
35
+ end
88
36
  end
89
37
  end
@@ -1,8 +1,10 @@
1
1
  require 'json'
2
2
 
3
- module MyJohnDeereApi::Request
4
- class Update::Base
5
- attr_reader :accessor, :item, :attributes, :errors, :response
3
+ module MyJohnDeereApi
4
+ class Request::Update::Base
5
+ include Validators::Base
6
+
7
+ attr_reader :accessor, :item, :attributes, :response
6
8
 
7
9
  ##
8
10
  # Accepts a valid oAuth AccessToken, the item to be updated,
@@ -13,63 +15,29 @@ module MyJohnDeereApi::Request
13
15
  def initialize(accessor, item, attributes)
14
16
  @accessor = accessor
15
17
  @item = item
16
- @attributes = attributes
18
+ @attributes = item.attributes.merge(attributes)
17
19
 
18
20
  process_attributes
19
-
20
- @errors = {}
21
21
  end
22
22
 
23
23
  ##
24
24
  # Make the request, if the instance is valid
25
25
 
26
- # def request
27
- # validate!
28
- #
29
- # @response = accessor.post(resource, request_body.to_json, headers)
30
- # end
26
+ def request
27
+ validate!
31
28
 
32
- ##
33
- # Object created by request
34
-
35
- # def object
36
- # return @object if defined?(@object)
37
- #
38
- # request unless response
39
- #
40
- # @object = model.new(fetch_record, accessor)
41
- # end
42
-
43
- ##
44
- # Runs validations, adding to the errors hash as needed. Returns true
45
- # if the errors hash is still empty after all validations have been run.
46
-
47
- # def valid?
48
- # return @is_valid if defined?(@is_valid)
49
- #
50
- # validate_required
51
- # validate_attributes
52
- #
53
- # @is_valid = errors.empty?
54
- # end
29
+ @response = accessor.put(resource, request_body.to_json, headers)
30
+ end
55
31
 
56
32
  ##
57
- # Raises an error if the record is invalid. Passes the errors hash
58
- # to the error, in order to build a useful message string.
33
+ # Object, same as item for updates
59
34
 
60
- # def validate!
61
- # raise(InvalidRecordError, errors) unless valid?
62
- # end
35
+ def object
36
+ @object ||= item
37
+ end
63
38
 
64
39
  private
65
40
 
66
- ##
67
- # Run validations unique to a given model. This should be overridden
68
- # by children where needed.
69
-
70
- # def validate_attributes
71
- # end
72
-
73
41
  ##
74
42
  # Convert inputs into working attributes. This allows us to auto-create
75
43
  # some attributes from others, or set defaults, on a class-by-class basis.
@@ -78,22 +46,6 @@ module MyJohnDeereApi::Request
78
46
  def process_attributes
79
47
  end
80
48
 
81
- ##
82
- # Attributes that must be specified, override in child class
83
-
84
- def required_attributes
85
- []
86
- end
87
-
88
- ##
89
- # Validates required attributes
90
-
91
- def validate_required
92
- required_attributes.each do |attr|
93
- errors[attr] = 'is required' unless attributes[attr]
94
- end
95
- end
96
-
97
49
  ##
98
50
  # Headers for PUT request
99
51
 
@@ -1,3 +1,3 @@
1
1
  module MyJohnDeereApi
2
- VERSION='1.3.5'
2
+ VERSION='1.3.6'
3
3
  end
@@ -52,6 +52,19 @@ describe 'MyJohnDeereApi::Model::Asset' do
52
52
  end
53
53
  end
54
54
 
55
+ describe '#attributes' do
56
+ it 'converts properties back to an attributes hash' do
57
+ asset = JD::Model::Asset.new(record)
58
+ attributes = asset.attributes
59
+
60
+ assert_equal asset.id, attributes[:id]
61
+ assert_equal asset.title, attributes[:title]
62
+ assert_equal asset.asset_category, attributes[:asset_category]
63
+ assert_equal asset.asset_type, attributes[:asset_type]
64
+ assert_equal asset.asset_sub_type, attributes[:asset_sub_type]
65
+ end
66
+ end
67
+
55
68
  describe '#locations' do
56
69
  it 'returns a collection of locations for this asset' do
57
70
  accessor
@@ -1,35 +1,90 @@
1
1
  require 'support/helper'
2
2
 
3
3
  describe 'MyJohnDeereApi::Request::Update::Asset' do
4
- # let(:object) { JD::Request::Update::Base.new(accessor, item, attributes) }
5
- # let(:item) { 'item' }
6
- # let(:attributes) { {} }
7
- #
8
- # describe '#initialize(access_token, item, attributes)' do
9
- # it 'accepts an accessor, item and attributes' do
10
- # object = JD::Request::Update::Base.new(accessor, item, attributes)
11
- #
12
- # assert_equal accessor, object.accessor
13
- # assert_equal item, object.item
14
- # assert_equal attributes, object.attributes
15
- # end
16
- #
17
- # it 'creates an empty error hash' do
18
- # object = JD::Request::Update::Base.new(accessor, item, {})
19
- # assert_equal({}, object.errors)
20
- # end
21
- # end
22
- #
23
- # describe '#headers' do
24
- # it 'sets the accept and content-type headers' do
25
- # object = JD::Request::Update::Base.new(accessor, item, attributes)
26
- # headers = object.send(:headers)
27
- #
28
- # expected = 'application/vnd.deere.axiom.v3+json'
29
- #
30
- # assert_kind_of Hash, headers
31
- # assert_equal expected, headers['Accept']
32
- # assert_equal expected, headers['Content-Type']
33
- # end
34
- # end
4
+ let(:klass) { JD::Request::Update::Asset }
5
+ let(:object) { klass.new(accessor, item, attributes) }
6
+ let(:item) { JD::Model::Asset.new(record, accessor) }
7
+
8
+ let(:attributes) do
9
+ {
10
+ organization_id: organization_id,
11
+ contribution_definition_id: contribution_definition_id
12
+ }
13
+ end
14
+
15
+ let(:record) do
16
+ {
17
+ '@type' => 'ContributedAsset',
18
+ 'id' => asset_id,
19
+ 'title' => 'Bob',
20
+ 'assetCategory' => 'DEVICE',
21
+ 'assetType' => 'SENSOR',
22
+ 'assetSubType' => 'ENVIRONMENTAL',
23
+ 'lastModifiedDate' => CONFIG.timestamp,
24
+ 'links' => []
25
+ }
26
+ end
27
+
28
+ inherits_from MyJohnDeereApi::Request::Update::Base
29
+
30
+ describe '#initialize(access_token, item, attributes)' do
31
+ it 'accepts an accessor, item and attributes' do
32
+ assert_equal accessor, object.accessor
33
+ assert_equal item, object.item
34
+ assert_equal item.attributes.merge(attributes), object.attributes
35
+ end
36
+
37
+ it 'includes validation' do
38
+ [:validate!, :valid?].each do |method_name|
39
+ assert object.respond_to?(method_name)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#resource' do
45
+ it 'is /assets/<asset_id>' do
46
+ assert_equal "/assets/#{asset_id}", object.send(:resource)
47
+ end
48
+ end
49
+
50
+ describe '#headers' do
51
+ it 'sets the accept and content-type headers' do
52
+ object = JD::Request::Update::Asset.new(accessor, item, attributes)
53
+ headers = object.send(:headers)
54
+
55
+ expected = 'application/vnd.deere.axiom.v3+json'
56
+
57
+ assert_kind_of Hash, headers
58
+ assert_equal expected, headers['Accept']
59
+ assert_equal expected, headers['Content-Type']
60
+ end
61
+ end
62
+
63
+ describe '#request_body' do
64
+ it 'properly forms the request body' do
65
+ body = object.send(:request_body)
66
+
67
+ assert_equal item.attributes[:title], body[:title]
68
+ assert_equal item.attributes[:asset_category], body[:assetCategory]
69
+ assert_equal item.attributes[:asset_type], body[:assetType]
70
+ assert_equal item.attributes[:asset_sub_type], body[:assetSubType]
71
+
72
+ assert_kind_of Array, body[:links]
73
+ assert_equal 1, body[:links].size
74
+
75
+ assert_kind_of Hash, body[:links].first
76
+ assert_equal 'Link', body[:links].first['@type']
77
+ assert_equal 'contributionDefinition', body[:links].first['rel']
78
+ assert_equal "#{base_url}/contributionDefinitions/#{contribution_definition_id}",
79
+ body[:links].first['uri']
80
+ end
81
+ end
82
+
83
+ describe '#request' do
84
+ it 'makes the request' do
85
+ VCR.use_cassette('put_asset') { object.request }
86
+
87
+ assert_kind_of Net::HTTPNoContent, object.response
88
+ end
89
+ end
35
90
  end
@@ -2,23 +2,44 @@ require 'support/helper'
2
2
  require 'yaml'
3
3
  require 'json'
4
4
 
5
+ class UpdateBaseItem
6
+ attr_reader :attributes
7
+
8
+ def initialize
9
+ @attributes = {
10
+ name: 'Bob',
11
+ age: 21
12
+ }
13
+ end
14
+ end
15
+
5
16
  describe 'MyJohnDeereApi::Request::Update::Base' do
6
17
  let(:object) { JD::Request::Update::Base.new(accessor, item, attributes) }
7
- let(:item) { 'item' }
18
+ let(:item) { UpdateBaseItem.new }
8
19
  let(:attributes) { {} }
9
20
 
10
21
  describe '#initialize(access_token, item, attributes)' do
11
22
  it 'accepts an accessor, item and attributes' do
12
- object = JD::Request::Update::Base.new(accessor, item, attributes)
13
-
14
23
  assert_equal accessor, object.accessor
15
24
  assert_equal item, object.item
16
- assert_equal attributes, object.attributes
17
25
  end
18
26
 
19
- it 'creates an empty error hash' do
20
- object = JD::Request::Update::Base.new(accessor, item, {})
21
- assert_equal({}, object.errors)
27
+ describe 'when a new attribute (ie, name) is passed in' do
28
+ let(:attributes) { {name: 'John'} }
29
+
30
+ it 'merges name with item attributes' do
31
+ assert_equal attributes[:name], object.attributes[:name]
32
+ assert_equal item.attributes[:age], object.attributes[:age]
33
+ end
34
+ end
35
+
36
+ describe 'when a new attribute (ie, age) is passed in' do
37
+ let(:attributes) { {age: 99} }
38
+
39
+ it 'merges name with item attributes' do
40
+ assert_equal item.attributes[:name], object.attributes[:name]
41
+ assert_equal attributes[:age], object.attributes[:age]
42
+ end
22
43
  end
23
44
  end
24
45
 
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: 1.3.5
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime Bellmyer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-02-13 00:00:00.000000000 Z
12
+ date: 2020-02-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: vcr