my_john_deere_api 1.3.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/my_john_deere_api.rb +1 -0
- data/lib/my_john_deere_api/request.rb +1 -0
- data/lib/my_john_deere_api/request/create/asset.rb +1 -33
- data/lib/my_john_deere_api/request/create/base.rb +0 -10
- data/lib/my_john_deere_api/request/update.rb +4 -0
- data/lib/my_john_deere_api/request/update/asset.rb +89 -0
- data/lib/my_john_deere_api/request/update/base.rb +107 -0
- data/lib/my_john_deere_api/validators.rb +4 -0
- data/lib/my_john_deere_api/validators/asset.rb +35 -0
- data/lib/my_john_deere_api/validators/base.rb +67 -0
- data/lib/my_john_deere_api/version.rb +1 -1
- data/test/lib/my_john_deere_api/request/create/base_test.rb +3 -3
- data/test/lib/my_john_deere_api/request/update/asset_test.rb +35 -0
- data/test/lib/my_john_deere_api/request/update/base_test.rb +37 -0
- data/test/lib/my_john_deere_api/request/update_test.rb +13 -0
- data/test/lib/my_john_deere_api/request_test.rb +4 -0
- data/test/lib/my_john_deere_api/validators/asset_test.rb +99 -0
- data/test/lib/my_john_deere_api/validators/base_test.rb +92 -0
- data/test/lib/my_john_deere_api/validators_test.rb +13 -0
- data/test/my_john_deere_api_test.rb +4 -0
- metadata +14 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4919eaf224090046a44f2ec4712dfddaebcbb7b5279dc9499cb09ab5b13f1445
|
4
|
+
data.tar.gz: 5b9d66230b2a3fc29f7111ddad1fc82ea3796935d1d87465238e4f8a70c66f18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d908341e42d6a225810c7463157dc453490c7dc5a5c93fea52c5445a91e3ec73d2860c3565918a0a70d9858fe61f7170dd5acdc1b9bb6b5cc0eb57f7d4597cc
|
7
|
+
data.tar.gz: 6f23310c0403f482d092026780c2280778f7e55ccf309dec23e993522a4d9c80aaf62a94cf045272e16b8563571cd8b00b2e092b775c304a5eed52ae4d27b4c3
|
data/lib/my_john_deere_api.rb
CHANGED
@@ -10,6 +10,7 @@ module MyJohnDeereApi
|
|
10
10
|
autoload :Request, 'my_john_deere_api/request'
|
11
11
|
autoload :Model, 'my_john_deere_api/model'
|
12
12
|
autoload :Helpers, 'my_john_deere_api/helpers'
|
13
|
+
autoload :Validators, 'my_john_deere_api/validators'
|
13
14
|
|
14
15
|
require 'my_john_deere_api/errors'
|
15
16
|
end
|
@@ -2,26 +2,10 @@ require 'json'
|
|
2
2
|
|
3
3
|
module MyJohnDeereApi
|
4
4
|
class Request::Create::Asset < Request::Create::Base
|
5
|
-
|
6
|
-
'DEVICE' => {
|
7
|
-
'SENSOR' => ['GRAIN_BIN', 'ENVIRONMENTAL', 'IRRIGATION_PIVOT', 'OTHER']
|
8
|
-
},
|
9
|
-
|
10
|
-
'EQUIPMENT' => {
|
11
|
-
'MACHINE' => ['PICKUP_TRUCK', 'UTILITY_VEHICLE'],
|
12
|
-
'OTHER' => ['ANHYDROUS_AMMONIA_TANK', 'NURSE_TRUCK', 'NURSE_WAGON', 'TECHNICIAN_TRUCK']
|
13
|
-
},
|
14
|
-
}
|
5
|
+
include Validators::Asset
|
15
6
|
|
16
7
|
private
|
17
8
|
|
18
|
-
##
|
19
|
-
# attributes that must be specified
|
20
|
-
|
21
|
-
def required_attributes
|
22
|
-
[:organization_id, :contribution_definition_id, :title]
|
23
|
-
end
|
24
|
-
|
25
9
|
##
|
26
10
|
# Retrieve newly created record
|
27
11
|
|
@@ -39,15 +23,6 @@ module MyJohnDeereApi
|
|
39
23
|
Model::Asset
|
40
24
|
end
|
41
25
|
|
42
|
-
##
|
43
|
-
# Handle any custom validation for this model that may not apply to others
|
44
|
-
|
45
|
-
def validate_attributes
|
46
|
-
unless valid_categories?(attributes[:asset_category], attributes[:asset_type], attributes[:asset_sub_type])
|
47
|
-
errors[:asset_category] = 'requires valid combination of category/type/subtype'
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
26
|
##
|
52
27
|
# Path supplied to API
|
53
28
|
|
@@ -75,12 +50,5 @@ module MyJohnDeereApi
|
|
75
50
|
]
|
76
51
|
}
|
77
52
|
end
|
78
|
-
|
79
|
-
##
|
80
|
-
# Returns boolean, true if this combination is valid
|
81
|
-
|
82
|
-
def valid_categories?(category, type, subtype)
|
83
|
-
VALID_CATEGORIES.dig(category, type).to_a.include?(subtype)
|
84
|
-
end
|
85
53
|
end
|
86
54
|
end
|
@@ -6,16 +6,6 @@ module MyJohnDeereApi
|
|
6
6
|
|
7
7
|
##
|
8
8
|
# Accepts a valid oAuth AccessToken, and a hash of attributes.
|
9
|
-
#
|
10
|
-
# Required attributes:
|
11
|
-
# - organization_id
|
12
|
-
# - contribution_definition_id
|
13
|
-
# - title
|
14
|
-
# - asset_category
|
15
|
-
# - asset_type
|
16
|
-
# - asset_sub_type
|
17
|
-
#
|
18
|
-
# category/type/subtype must be a recognized combination as defined above.
|
19
9
|
|
20
10
|
def initialize(accessor, attributes)
|
21
11
|
@accessor = accessor
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'json'
|
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
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module MyJohnDeereApi::Request
|
4
|
+
class Update::Base
|
5
|
+
attr_reader :accessor, :item, :attributes, :errors, :response
|
6
|
+
|
7
|
+
##
|
8
|
+
# Accepts a valid oAuth AccessToken, the item to be updated,
|
9
|
+
# and a hash of attributes.
|
10
|
+
#
|
11
|
+
# category/type/subtype must be a recognized combination as defined above.
|
12
|
+
|
13
|
+
def initialize(accessor, item, attributes)
|
14
|
+
@accessor = accessor
|
15
|
+
@item = item
|
16
|
+
@attributes = attributes
|
17
|
+
|
18
|
+
process_attributes
|
19
|
+
|
20
|
+
@errors = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Make the request, if the instance is valid
|
25
|
+
|
26
|
+
# def request
|
27
|
+
# validate!
|
28
|
+
#
|
29
|
+
# @response = accessor.post(resource, request_body.to_json, headers)
|
30
|
+
# end
|
31
|
+
|
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
|
55
|
+
|
56
|
+
##
|
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.
|
59
|
+
|
60
|
+
# def validate!
|
61
|
+
# raise(InvalidRecordError, errors) unless valid?
|
62
|
+
# end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
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
|
+
##
|
74
|
+
# Convert inputs into working attributes. This allows us to auto-create
|
75
|
+
# some attributes from others, or set defaults, on a class-by-class basis.
|
76
|
+
# See Request::Create::AssetLocation for an example.
|
77
|
+
|
78
|
+
def process_attributes
|
79
|
+
end
|
80
|
+
|
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
|
+
##
|
98
|
+
# Headers for PUT request
|
99
|
+
|
100
|
+
def headers
|
101
|
+
@headers ||= {
|
102
|
+
'Accept' => 'application/vnd.deere.axiom.v3+json',
|
103
|
+
'Content-Type' => 'application/vnd.deere.axiom.v3+json'
|
104
|
+
}
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module MyJohnDeereApi::Validators
|
2
|
+
module Asset
|
3
|
+
include Base
|
4
|
+
|
5
|
+
VALID_CATEGORIES = {
|
6
|
+
'DEVICE' => {
|
7
|
+
'SENSOR' => ['GRAIN_BIN', 'ENVIRONMENTAL', 'IRRIGATION_PIVOT', 'OTHER']
|
8
|
+
},
|
9
|
+
|
10
|
+
'EQUIPMENT' => {
|
11
|
+
'MACHINE' => ['PICKUP_TRUCK', 'UTILITY_VEHICLE'],
|
12
|
+
'OTHER' => ['ANHYDROUS_AMMONIA_TANK', 'NURSE_TRUCK', 'NURSE_WAGON', 'TECHNICIAN_TRUCK']
|
13
|
+
},
|
14
|
+
}
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def required_attributes
|
19
|
+
[:organization_id, :contribution_definition_id, :title]
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate_attributes
|
23
|
+
unless valid_categories?(attributes[:asset_category], attributes[:asset_type], attributes[:asset_sub_type])
|
24
|
+
errors[:asset_category] = 'requires valid combination of category/type/subtype'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# Returns boolean, true if this combination is valid
|
30
|
+
|
31
|
+
def valid_categories?(category, type, subtype)
|
32
|
+
VALID_CATEGORIES.dig(category, type).to_a.include?(subtype)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module MyJohnDeereApi
|
2
|
+
##
|
3
|
+
# This is a mix-in for Create/Update Reqest classes. It assumes that
|
4
|
+
# the class in question has a hash of attributes that will be passed
|
5
|
+
# to the request.
|
6
|
+
#
|
7
|
+
# This module creates the errors hash as a reader. The key of the hash
|
8
|
+
# is the attribute name, and the value is an array of error messages
|
9
|
+
# for that attribute. Follow this format when defining custom
|
10
|
+
# validations in the `validate_attributes` method.
|
11
|
+
|
12
|
+
module Validators::Base
|
13
|
+
attr_reader :errors
|
14
|
+
|
15
|
+
##
|
16
|
+
# Raises an error if the record is invalid. Passes the errors hash
|
17
|
+
# to the error, in order to build a useful message string.
|
18
|
+
|
19
|
+
def validate!
|
20
|
+
raise(InvalidRecordError, errors) unless valid?
|
21
|
+
true
|
22
|
+
end
|
23
|
+
##
|
24
|
+
|
25
|
+
# Runs validations, adding to the errors hash as needed. Returns true
|
26
|
+
# if the errors hash is still empty after all validations have been run.
|
27
|
+
|
28
|
+
def valid?
|
29
|
+
return @is_valid if defined?(@is_valid)
|
30
|
+
|
31
|
+
@errors = {}
|
32
|
+
validate_required
|
33
|
+
validate_attributes
|
34
|
+
|
35
|
+
@is_valid = errors.empty?
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
##
|
41
|
+
# Validates required attributes
|
42
|
+
|
43
|
+
def validate_required
|
44
|
+
required_attributes.each do |attr|
|
45
|
+
errors[attr] = 'is required' unless attributes[attr]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# Attributes that must be specified, override in child module if needed
|
51
|
+
|
52
|
+
def required_attributes
|
53
|
+
[]
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# Handle any custom validation for this class, override in child module if needed.
|
58
|
+
#
|
59
|
+
# Add messages to errors hash with the attribute name as the key, and an array
|
60
|
+
# of error messages as the value.
|
61
|
+
|
62
|
+
def validate_attributes
|
63
|
+
# Example:
|
64
|
+
# errors[:name] = "can't be blank" if errors[:name].size == 0
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -5,21 +5,21 @@ describe 'MyJohnDeereApi::Request::Create::Base' do
|
|
5
5
|
|
6
6
|
describe '#initialize(access_token, attributes)' do
|
7
7
|
it 'accepts an accessor and attributes' do
|
8
|
-
object = JD::Request::Create::
|
8
|
+
object = JD::Request::Create::Base.new(accessor, attributes)
|
9
9
|
|
10
10
|
assert_equal accessor, object.accessor
|
11
11
|
assert_equal attributes, object.attributes
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'creates an empty error hash' do
|
15
|
-
object = JD::Request::Create::
|
15
|
+
object = JD::Request::Create::Base.new(accessor, {})
|
16
16
|
assert_equal({}, object.errors)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
describe '#headers' do
|
21
21
|
it 'sets the accept and content-type headers' do
|
22
|
-
object = JD::Request::Create::
|
22
|
+
object = JD::Request::Create::Base.new(accessor, attributes)
|
23
23
|
headers = object.send(:headers)
|
24
24
|
|
25
25
|
expected = 'application/vnd.deere.axiom.v3+json'
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
|
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
|
35
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
require 'yaml'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
describe 'MyJohnDeereApi::Request::Update::Base' do
|
6
|
+
let(:object) { JD::Request::Update::Base.new(accessor, item, attributes) }
|
7
|
+
let(:item) { 'item' }
|
8
|
+
let(:attributes) { {} }
|
9
|
+
|
10
|
+
describe '#initialize(access_token, item, attributes)' do
|
11
|
+
it 'accepts an accessor, item and attributes' do
|
12
|
+
object = JD::Request::Update::Base.new(accessor, item, attributes)
|
13
|
+
|
14
|
+
assert_equal accessor, object.accessor
|
15
|
+
assert_equal item, object.item
|
16
|
+
assert_equal attributes, object.attributes
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'creates an empty error hash' do
|
20
|
+
object = JD::Request::Update::Base.new(accessor, item, {})
|
21
|
+
assert_equal({}, object.errors)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#headers' do
|
26
|
+
it 'sets the accept and content-type headers' do
|
27
|
+
object = JD::Request::Update::Base.new(accessor, item, attributes)
|
28
|
+
headers = object.send(:headers)
|
29
|
+
|
30
|
+
expected = 'application/vnd.deere.axiom.v3+json'
|
31
|
+
|
32
|
+
assert_kind_of Hash, headers
|
33
|
+
assert_equal expected, headers['Accept']
|
34
|
+
assert_equal expected, headers['Content-Type']
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
|
3
|
+
describe 'MyJohnDeereApi::Request::Update' do
|
4
|
+
describe 'loading dependencies' do
|
5
|
+
it 'loads Request::Update::Base' do
|
6
|
+
assert JD::Request::Update::Base
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'loads Request::Update::Asset' do
|
10
|
+
assert JD::Request::Update::Asset
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
|
3
|
+
module AssetValidatorTest
|
4
|
+
class Base
|
5
|
+
include JD::Validators::Asset
|
6
|
+
|
7
|
+
attr_reader :attributes
|
8
|
+
|
9
|
+
def initialize(attributes)
|
10
|
+
@attributes = attributes
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Vanilla < Base
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'MyJohnDeereApi::Validators::Asset' do
|
19
|
+
let(:klass) { AssetValidatorTest::Base }
|
20
|
+
let(:object) { klass.new(attributes) }
|
21
|
+
let(:attributes) { valid_attributes }
|
22
|
+
|
23
|
+
let(:valid_attributes) do
|
24
|
+
{
|
25
|
+
organization_id: '000000',
|
26
|
+
contribution_definition_id: '00000000-0000-0000-0000-000000000000',
|
27
|
+
title: "Bob's Stuff"
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'exists' do
|
32
|
+
assert JD::Validators::Asset
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'inherits from MyJohnDeereApi::Validators::Base' do
|
36
|
+
[:validate!, :valid?].each{ |method_name| assert object.respond_to?(method_name) }
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'requires several attributes' do
|
40
|
+
[:organization_id, :contribution_definition_id, :title].each do |attr|
|
41
|
+
object = klass.new(valid_attributes.merge(attr => nil))
|
42
|
+
|
43
|
+
refute object.valid?
|
44
|
+
exception = assert_raises(JD::InvalidRecordError) { object.validate! }
|
45
|
+
|
46
|
+
assert_includes exception.message, "#{attr} is required"
|
47
|
+
assert_includes object.errors[attr], 'is required'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'requires a valid combination of category/type/subtype' do
|
52
|
+
valid_combos = [
|
53
|
+
['DEVICE', 'SENSOR', 'ENVIRONMENTAL'],
|
54
|
+
['DEVICE', 'SENSOR', 'GRAIN_BIN'],
|
55
|
+
['DEVICE', 'SENSOR', 'IRRIGATION_PIVOT'],
|
56
|
+
['DEVICE', 'SENSOR', 'OTHER'],
|
57
|
+
['EQUIPMENT', 'MACHINE', 'PICKUP_TRUCK'],
|
58
|
+
['EQUIPMENT', 'MACHINE', 'UTILITY_VEHICLE'],
|
59
|
+
['EQUIPMENT', 'OTHER', 'ANHYDROUS_AMMONIA_TANK'],
|
60
|
+
['EQUIPMENT', 'OTHER', 'NURSE_TRUCK'],
|
61
|
+
['EQUIPMENT', 'OTHER', 'NURSE_WAGON'],
|
62
|
+
['EQUIPMENT', 'OTHER', 'TECHNICIAN_TRUCK']
|
63
|
+
]
|
64
|
+
|
65
|
+
error_message = 'requires valid combination of category/type/subtype'
|
66
|
+
|
67
|
+
# cycle through all possible permutations, only proving valid if
|
68
|
+
# listed above
|
69
|
+
|
70
|
+
['DEVICE', 'EQUIPMENT', 'RANDOM_INVALID'].each do |category|
|
71
|
+
['MACHINE', 'OTHER', 'SENSOR', 'RANDOM_INVALID'].each do |type|
|
72
|
+
[
|
73
|
+
'ANHYDROUS_AMMONIA_TANK', 'ENVIRONMENTAL', 'GRAIN_BIN',
|
74
|
+
'IRRIGATION_PIVOT', 'NURSE_TRUCK', 'NURSE_WAGON',
|
75
|
+
'OTHER', 'PICKUP_TRUCK', 'TECHNICIAN_TRUCK',
|
76
|
+
'UTILITY_VEHICLE', 'RANDOM_INVALID'
|
77
|
+
].each do |subtype|
|
78
|
+
categories = {
|
79
|
+
asset_category: category,
|
80
|
+
asset_type: type,
|
81
|
+
asset_sub_type: subtype
|
82
|
+
}
|
83
|
+
|
84
|
+
object = klass.new(valid_attributes.merge(categories))
|
85
|
+
|
86
|
+
if valid_combos.include?([category, type, subtype])
|
87
|
+
assert object.valid?
|
88
|
+
else
|
89
|
+
refute object.valid?
|
90
|
+
exception = assert_raises(JD::InvalidRecordError) { object.validate! }
|
91
|
+
|
92
|
+
assert_includes exception.message, "asset_category #{error_message}"
|
93
|
+
assert_includes object.errors[:asset_category], error_message
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
|
3
|
+
module BaseValidatorTest
|
4
|
+
class Base
|
5
|
+
include JD::Validators::Base
|
6
|
+
|
7
|
+
attr_reader :attributes
|
8
|
+
|
9
|
+
def initialize(attributes)
|
10
|
+
@attributes = attributes
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Vanilla < Base
|
15
|
+
end
|
16
|
+
|
17
|
+
class WithRequiredAttributes < Base
|
18
|
+
def required_attributes
|
19
|
+
[:name]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class WithCustomValidations < Base
|
24
|
+
def validate_attributes
|
25
|
+
errors[:name] = 'is not Bob' unless attributes[:name] == 'Bob'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'MyJohnDeereApi::Validators::Base' do
|
31
|
+
let(:klass) { BaseValidatorTest::Vanilla }
|
32
|
+
let(:object) { klass.new(attributes) }
|
33
|
+
let(:valid_attributes) { {name: 'Bob'} }
|
34
|
+
let(:attributes) { valid_attributes }
|
35
|
+
|
36
|
+
it 'exists' do
|
37
|
+
assert klass
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#validate!' do
|
41
|
+
it 'returns true when valid' do
|
42
|
+
assert object.validate!
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'with required attributes' do
|
47
|
+
let(:klass) { BaseValidatorTest::WithRequiredAttributes }
|
48
|
+
|
49
|
+
it 'returns true when valid' do
|
50
|
+
assert object.validate!
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'without required attribute' do
|
54
|
+
let(:attributes) { {} }
|
55
|
+
|
56
|
+
it 'raises an error' do
|
57
|
+
exception = assert_raises(JD::InvalidRecordError) { object.validate! }
|
58
|
+
|
59
|
+
assert_includes exception.message, 'name is required'
|
60
|
+
assert_includes object.errors[:name], 'is required'
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'with unrequired attributes' do
|
64
|
+
let(:attributes) { valid_attributes.merge(age: 101) }
|
65
|
+
|
66
|
+
it 'returns true' do
|
67
|
+
assert object.validate!
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'custom validations' do
|
74
|
+
let(:klass) { BaseValidatorTest::WithCustomValidations }
|
75
|
+
let(:attributes) { valid_attributes.merge(name: 'Bob') }
|
76
|
+
|
77
|
+
it 'returns true when valid' do
|
78
|
+
assert object.validate!
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'when custom validation fails' do
|
82
|
+
let(:attributes) { valid_attributes.merge(name: 'Turtles') }
|
83
|
+
|
84
|
+
it 'raises an error' do
|
85
|
+
exception = assert_raises(JD::InvalidRecordError) { object.validate! }
|
86
|
+
|
87
|
+
assert_includes exception.message, 'name is not Bob'
|
88
|
+
assert_includes object.errors[:name], 'is not Bob'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'support/helper'
|
2
|
+
|
3
|
+
describe 'MyJohnDeereApi::Validators' do
|
4
|
+
describe 'loading dependencies' do
|
5
|
+
it 'loads Validators::Base' do
|
6
|
+
assert JD::Validators::Base
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'loads Validators::Asset' do
|
10
|
+
assert JD::Validators::Asset
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
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.
|
4
|
+
version: 1.3.3
|
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-
|
12
|
+
date: 2020-02-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: vcr
|
@@ -146,6 +146,12 @@ files:
|
|
146
146
|
- lib/my_john_deere_api/request/individual/contribution_product.rb
|
147
147
|
- lib/my_john_deere_api/request/individual/field.rb
|
148
148
|
- lib/my_john_deere_api/request/individual/organization.rb
|
149
|
+
- lib/my_john_deere_api/request/update.rb
|
150
|
+
- lib/my_john_deere_api/request/update/asset.rb
|
151
|
+
- lib/my_john_deere_api/request/update/base.rb
|
152
|
+
- lib/my_john_deere_api/validators.rb
|
153
|
+
- lib/my_john_deere_api/validators/asset.rb
|
154
|
+
- lib/my_john_deere_api/validators/base.rb
|
149
155
|
- lib/my_john_deere_api/version.rb
|
150
156
|
- test/lib/my_john_deere_api/authorize_test.rb
|
151
157
|
- test/lib/my_john_deere_api/client_test.rb
|
@@ -189,7 +195,13 @@ files:
|
|
189
195
|
- test/lib/my_john_deere_api/request/individual/field_test.rb
|
190
196
|
- test/lib/my_john_deere_api/request/individual/organization_test.rb
|
191
197
|
- test/lib/my_john_deere_api/request/individual_test.rb
|
198
|
+
- test/lib/my_john_deere_api/request/update/asset_test.rb
|
199
|
+
- test/lib/my_john_deere_api/request/update/base_test.rb
|
200
|
+
- test/lib/my_john_deere_api/request/update_test.rb
|
192
201
|
- test/lib/my_john_deere_api/request_test.rb
|
202
|
+
- test/lib/my_john_deere_api/validators/asset_test.rb
|
203
|
+
- test/lib/my_john_deere_api/validators/base_test.rb
|
204
|
+
- test/lib/my_john_deere_api/validators_test.rb
|
193
205
|
- test/lib/my_john_deere_api/version_test.rb
|
194
206
|
- test/my_john_deere_api_test.rb
|
195
207
|
- test/support/helper.rb
|