my_john_deere_api 2.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +813 -0
  3. data/Rakefile +9 -0
  4. data/lib/my_john_deere_api.rb +17 -0
  5. data/lib/my_john_deere_api/authorize.rb +69 -0
  6. data/lib/my_john_deere_api/client.rb +151 -0
  7. data/lib/my_john_deere_api/consumer.rb +90 -0
  8. data/lib/my_john_deere_api/errors.rb +7 -0
  9. data/lib/my_john_deere_api/errors/invalid_record_error.rb +29 -0
  10. data/lib/my_john_deere_api/errors/missing_contribution_definition_id_error.rb +12 -0
  11. data/lib/my_john_deere_api/errors/not_yet_implemented_error.rb +12 -0
  12. data/lib/my_john_deere_api/errors/type_mismatch_error.rb +12 -0
  13. data/lib/my_john_deere_api/errors/unsupported_environment_error.rb +18 -0
  14. data/lib/my_john_deere_api/helpers.rb +6 -0
  15. data/lib/my_john_deere_api/helpers/case_conversion.rb +37 -0
  16. data/lib/my_john_deere_api/helpers/environment_helper.rb +25 -0
  17. data/lib/my_john_deere_api/helpers/uri_helpers.rb +19 -0
  18. data/lib/my_john_deere_api/helpers/validate_contribution_definition.rb +18 -0
  19. data/lib/my_john_deere_api/model.rb +10 -0
  20. data/lib/my_john_deere_api/model/asset.rb +69 -0
  21. data/lib/my_john_deere_api/model/asset_location.rb +19 -0
  22. data/lib/my_john_deere_api/model/base.rb +97 -0
  23. data/lib/my_john_deere_api/model/contribution_definition.rb +15 -0
  24. data/lib/my_john_deere_api/model/contribution_product.rb +33 -0
  25. data/lib/my_john_deere_api/model/field.rb +40 -0
  26. data/lib/my_john_deere_api/model/flag.rb +36 -0
  27. data/lib/my_john_deere_api/model/organization.rb +43 -0
  28. data/lib/my_john_deere_api/net_http_retry.rb +2 -0
  29. data/lib/my_john_deere_api/net_http_retry/decorator.rb +53 -0
  30. data/lib/my_john_deere_api/net_http_retry/max_retries_exceeded_error.rb +20 -0
  31. data/lib/my_john_deere_api/request.rb +6 -0
  32. data/lib/my_john_deere_api/request/collection.rb +10 -0
  33. data/lib/my_john_deere_api/request/collection/asset_locations.rb +27 -0
  34. data/lib/my_john_deere_api/request/collection/assets.rb +34 -0
  35. data/lib/my_john_deere_api/request/collection/base.rb +91 -0
  36. data/lib/my_john_deere_api/request/collection/contribution_definitions.rb +26 -0
  37. data/lib/my_john_deere_api/request/collection/contribution_products.rb +26 -0
  38. data/lib/my_john_deere_api/request/collection/fields.rb +26 -0
  39. data/lib/my_john_deere_api/request/collection/flags.rb +33 -0
  40. data/lib/my_john_deere_api/request/collection/organizations.rb +26 -0
  41. data/lib/my_john_deere_api/request/create.rb +5 -0
  42. data/lib/my_john_deere_api/request/create/asset.rb +57 -0
  43. data/lib/my_john_deere_api/request/create/asset_location.rb +110 -0
  44. data/lib/my_john_deere_api/request/create/base.rb +67 -0
  45. data/lib/my_john_deere_api/request/individual.rb +8 -0
  46. data/lib/my_john_deere_api/request/individual/asset.rb +19 -0
  47. data/lib/my_john_deere_api/request/individual/base.rb +52 -0
  48. data/lib/my_john_deere_api/request/individual/contribution_definition.rb +19 -0
  49. data/lib/my_john_deere_api/request/individual/contribution_product.rb +19 -0
  50. data/lib/my_john_deere_api/request/individual/field.rb +19 -0
  51. data/lib/my_john_deere_api/request/individual/organization.rb +19 -0
  52. data/lib/my_john_deere_api/request/update.rb +4 -0
  53. data/lib/my_john_deere_api/request/update/asset.rb +40 -0
  54. data/lib/my_john_deere_api/request/update/base.rb +67 -0
  55. data/lib/my_john_deere_api/validators.rb +5 -0
  56. data/lib/my_john_deere_api/validators/asset.rb +35 -0
  57. data/lib/my_john_deere_api/validators/asset_location.rb +34 -0
  58. data/lib/my_john_deere_api/validators/base.rb +67 -0
  59. data/lib/my_john_deere_api/version.rb +3 -0
  60. data/test/lib/my_john_deere_api/authorize_test.rb +81 -0
  61. data/test/lib/my_john_deere_api/client_test.rb +225 -0
  62. data/test/lib/my_john_deere_api/consumer_test.rb +58 -0
  63. data/test/lib/my_john_deere_api/errors/invalid_record_error_test.rb +26 -0
  64. data/test/lib/my_john_deere_api/errors/max_retries_exceeded_error_test.rb +13 -0
  65. data/test/lib/my_john_deere_api/errors/missing_contribution_definition_id_error_test.rb +14 -0
  66. data/test/lib/my_john_deere_api/errors/not_yet_implemented_error_test.rb +13 -0
  67. data/test/lib/my_john_deere_api/errors/type_mismatch_error_test.rb +13 -0
  68. data/test/lib/my_john_deere_api/errors/unsupported_environment_error_test.rb +18 -0
  69. data/test/lib/my_john_deere_api/errors_test.rb +25 -0
  70. data/test/lib/my_john_deere_api/helpers/case_conversion_test.rb +100 -0
  71. data/test/lib/my_john_deere_api/helpers/environment_helper_test.rb +67 -0
  72. data/test/lib/my_john_deere_api/helpers/uri_helpers_test.rb +58 -0
  73. data/test/lib/my_john_deere_api/helpers/validate_contribution_definition_test.rb +74 -0
  74. data/test/lib/my_john_deere_api/helpers_test.rb +21 -0
  75. data/test/lib/my_john_deere_api/model/asset_location_test.rb +38 -0
  76. data/test/lib/my_john_deere_api/model/asset_test.rb +133 -0
  77. data/test/lib/my_john_deere_api/model/base_test.rb +76 -0
  78. data/test/lib/my_john_deere_api/model/contribution_definition_test.rb +52 -0
  79. data/test/lib/my_john_deere_api/model/contribution_product_test.rb +82 -0
  80. data/test/lib/my_john_deere_api/model/field_test.rb +65 -0
  81. data/test/lib/my_john_deere_api/model/flag_test.rb +48 -0
  82. data/test/lib/my_john_deere_api/model/organization_test.rb +84 -0
  83. data/test/lib/my_john_deere_api/model_test.rb +37 -0
  84. data/test/lib/my_john_deere_api/net_http_retry/decorator_test.rb +163 -0
  85. data/test/lib/my_john_deere_api/request/collection/asset_locations_test.rb +102 -0
  86. data/test/lib/my_john_deere_api/request/collection/assets_test.rb +99 -0
  87. data/test/lib/my_john_deere_api/request/collection/base_test.rb +27 -0
  88. data/test/lib/my_john_deere_api/request/collection/contribution_definitions_test.rb +88 -0
  89. data/test/lib/my_john_deere_api/request/collection/contribution_products_test.rb +88 -0
  90. data/test/lib/my_john_deere_api/request/collection/fields_test.rb +86 -0
  91. data/test/lib/my_john_deere_api/request/collection/flags_test.rb +92 -0
  92. data/test/lib/my_john_deere_api/request/collection/organizations_test.rb +87 -0
  93. data/test/lib/my_john_deere_api/request/collection_test.rb +37 -0
  94. data/test/lib/my_john_deere_api/request/create/asset_location_test.rb +163 -0
  95. data/test/lib/my_john_deere_api/request/create/asset_test.rb +182 -0
  96. data/test/lib/my_john_deere_api/request/create/base_test.rb +29 -0
  97. data/test/lib/my_john_deere_api/request/create_test.rb +17 -0
  98. data/test/lib/my_john_deere_api/request/individual/asset_test.rb +33 -0
  99. data/test/lib/my_john_deere_api/request/individual/base_test.rb +18 -0
  100. data/test/lib/my_john_deere_api/request/individual/contribution_definition_test.rb +33 -0
  101. data/test/lib/my_john_deere_api/request/individual/contribution_product_test.rb +33 -0
  102. data/test/lib/my_john_deere_api/request/individual/field_test.rb +37 -0
  103. data/test/lib/my_john_deere_api/request/individual/organization_test.rb +33 -0
  104. data/test/lib/my_john_deere_api/request/individual_test.rb +29 -0
  105. data/test/lib/my_john_deere_api/request/update/asset_test.rb +99 -0
  106. data/test/lib/my_john_deere_api/request/update/base_test.rb +60 -0
  107. data/test/lib/my_john_deere_api/request/update_test.rb +13 -0
  108. data/test/lib/my_john_deere_api/request_test.rb +21 -0
  109. data/test/lib/my_john_deere_api/validators/asset_location_test.rb +61 -0
  110. data/test/lib/my_john_deere_api/validators/asset_test.rb +93 -0
  111. data/test/lib/my_john_deere_api/validators/base_test.rb +92 -0
  112. data/test/lib/my_john_deere_api/validators_test.rb +17 -0
  113. data/test/lib/my_john_deere_api/version_test.rb +9 -0
  114. data/test/my_john_deere_api_test.rb +37 -0
  115. data/test/support/helper.rb +97 -0
  116. data/test/support/vcr/accessor/delete_failed.yml +327 -0
  117. data/test/support/vcr/accessor/delete_max_failed.yml +615 -0
  118. data/test/support/vcr/accessor/delete_retry.yml +191 -0
  119. data/test/support/vcr/accessor/delete_retry_too_soon.yml +191 -0
  120. data/test/support/vcr/accessor/get_failed.yml +390 -0
  121. data/test/support/vcr/accessor/get_max_failed.yml +734 -0
  122. data/test/support/vcr/accessor/get_retry.yml +226 -0
  123. data/test/support/vcr/accessor/get_retry_too_soon.yml +226 -0
  124. data/test/support/vcr/accessor/post_failed.yml +417 -0
  125. data/test/support/vcr/accessor/post_max_failed.yml +785 -0
  126. data/test/support/vcr/accessor/post_retry.yml +241 -0
  127. data/test/support/vcr/accessor/post_retry_too_soon.yml +241 -0
  128. data/test/support/vcr/accessor/put_failed.yml +372 -0
  129. data/test/support/vcr/accessor/put_max_failed.yml +700 -0
  130. data/test/support/vcr/accessor/put_retry.yml +216 -0
  131. data/test/support/vcr/accessor/put_retry_too_soon.yml +216 -0
  132. data/test/support/vcr/catalog.yml +89 -0
  133. data/test/support/vcr/delete_asset.yml +82 -0
  134. data/test/support/vcr/get_access_token.yml +41 -0
  135. data/test/support/vcr/get_asset.yml +144 -0
  136. data/test/support/vcr/get_asset_locations.yml +196 -0
  137. data/test/support/vcr/get_assets.yml +152 -0
  138. data/test/support/vcr/get_contribution_definition.yml +90 -0
  139. data/test/support/vcr/get_contribution_definitions.yml +91 -0
  140. data/test/support/vcr/get_contribution_product.yml +91 -0
  141. data/test/support/vcr/get_contribution_products.yml +91 -0
  142. data/test/support/vcr/get_field.yml +144 -0
  143. data/test/support/vcr/get_fields.yml +146 -0
  144. data/test/support/vcr/get_flags.yml +47 -0
  145. data/test/support/vcr/get_organization.yml +90 -0
  146. data/test/support/vcr/get_organizations.yml +149 -0
  147. data/test/support/vcr/get_request_token.yml +83 -0
  148. data/test/support/vcr/post_asset_locations.yml +244 -0
  149. data/test/support/vcr/post_assets.yml +192 -0
  150. data/test/support/vcr/put_asset.yml +87 -0
  151. data/test/support/vcr/warning.txt +28 -0
  152. data/test/support/vcr_setup.rb +488 -0
  153. metadata +277 -0
@@ -0,0 +1,110 @@
1
+ require 'date'
2
+
3
+ module MyJohnDeereApi
4
+ class Request::Create::AssetLocation < Request::Create::Base
5
+ include Validators::AssetLocation
6
+
7
+ private
8
+
9
+ ##
10
+ # Set defaults and generate some attributes from others.
11
+ # Overridden from parent class.
12
+
13
+ def process_attributes
14
+ process_timestamp
15
+ process_geometry
16
+ end
17
+
18
+ ##
19
+ # Request body
20
+
21
+ def request_body
22
+ return @body if defined?(@body)
23
+
24
+ @body = [{
25
+ timestamp: attributes[:timestamp],
26
+ geometry: attributes[:geometry],
27
+ measurementData: attributes[:measurement_data]
28
+ }]
29
+ end
30
+
31
+ ##
32
+ # Custom-process timestamp
33
+
34
+ def process_timestamp
35
+ attributes[:timestamp] ||= Time.now.utc
36
+
37
+ attributes[:timestamp] = attributes[:timestamp].is_a?(String) ?
38
+ attributes[:timestamp] :
39
+ attributes[:timestamp].strftime('%Y-%m-%dT%H:%M:%SZ')
40
+ end
41
+
42
+ ##
43
+ # Custom-process geometry
44
+
45
+ def process_geometry
46
+ attributes[:geometry] = if attributes[:geometry]
47
+ attributes[:geometry].is_a?(String) ?
48
+ attributes[:geometry] :
49
+ attributes[:geometry].to_json
50
+ elsif attributes[:coordinates]
51
+ geometry_from_coordinates
52
+ end
53
+ end
54
+
55
+ ##
56
+ # Convert just coordinates into valid geometry hash
57
+
58
+ def geometry_from_coordinates
59
+ {
60
+ type: 'Feature',
61
+ geometry: {
62
+ geometries: [
63
+ coordinates: attributes[:coordinates],
64
+ type: 'Point'
65
+ ],
66
+ type: 'GeometryCollection'
67
+ }
68
+ }.to_json
69
+ end
70
+
71
+ ##
72
+ # Path supplied to API
73
+
74
+ def resource
75
+ @resource ||= "/assets/#{attributes[:asset_id]}/locations"
76
+ end
77
+
78
+ ##
79
+ # Retrieve newly created record
80
+
81
+ def fetch_record
82
+ # There is no endpoint to fetch a single location by id. We have to fetch
83
+ # them in bulk via the asset, but there could be thousands. We limit the
84
+ # request to just the first record from the location list endpoint, since
85
+ # locations are returned newest to oldest.
86
+
87
+ path = response['location'].split('/platform').last
88
+ path += "?count=1"
89
+
90
+ result = accessor.get(path, headers)
91
+
92
+ JSON.parse(result.body)['values'].first
93
+ end
94
+
95
+ ##
96
+ # Create a new timestamp adjusted by X minutes
97
+
98
+ def timestamp_add(timestamp, seconds)
99
+ stamp = DateTime.parse(timestamp).to_time + seconds
100
+ stamp.to_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')
101
+ end
102
+
103
+ ##
104
+ # This is the class used to model the data
105
+
106
+ def model
107
+ Model::AssetLocation
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,67 @@
1
+ require 'json'
2
+
3
+ module MyJohnDeereApi
4
+ class Request::Create::Base
5
+ include Validators::Base
6
+
7
+ attr_reader :client, :attributes, :response
8
+
9
+ ##
10
+ # Accepts a valid oAuth AccessToken, and a hash of attributes.
11
+
12
+ def initialize(client, attributes)
13
+ @client = client
14
+ @attributes = attributes
15
+
16
+ process_attributes
17
+ end
18
+
19
+ ##
20
+ # client accessor
21
+
22
+ def accessor
23
+ return @accessor if defined?(@accessor)
24
+ @accessor = client&.accessor
25
+ end
26
+
27
+ ##
28
+ # Make the request, if the instance is valid
29
+
30
+ def request
31
+ validate!
32
+
33
+ @response = accessor.post(resource, request_body.to_json, headers)
34
+ end
35
+
36
+ ##
37
+ # Object created by request
38
+
39
+ def object
40
+ return @object if defined?(@object)
41
+
42
+ request unless response
43
+
44
+ @object = model.new(client, fetch_record)
45
+ end
46
+
47
+ private
48
+
49
+ ##
50
+ # Convert inputs into working attributes. This allows us to auto-create
51
+ # some attributes from others, or set defaults, on a class-by-class basis.
52
+ # See Request::Create::AssetLocation for an example.
53
+
54
+ def process_attributes
55
+ end
56
+
57
+ ##
58
+ # Headers for POST request
59
+
60
+ def headers
61
+ @headers ||= {
62
+ 'Accept' => 'application/vnd.deere.axiom.v3+json',
63
+ 'Content-Type' => 'application/vnd.deere.axiom.v3+json'
64
+ }
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,8 @@
1
+ module MyJohnDeereApi::Request::Individual
2
+ autoload :Base, 'my_john_deere_api/request/individual/base'
3
+ autoload :Asset, 'my_john_deere_api/request/individual/asset'
4
+ autoload :ContributionProduct, 'my_john_deere_api/request/individual/contribution_product'
5
+ autoload :ContributionDefinition, 'my_john_deere_api/request/individual/contribution_definition'
6
+ autoload :Field, 'my_john_deere_api/request/individual/field'
7
+ autoload :Organization, 'my_john_deere_api/request/individual/organization'
8
+ end
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+
3
+ module MyJohnDeereApi::Request
4
+ class Individual::Asset < Individual::Base
5
+ ##
6
+ # The resource path for the first page in the collection
7
+
8
+ def resource
9
+ "/assets/#{id}"
10
+ end
11
+
12
+ ##
13
+ # This is the class used to model the data
14
+
15
+ def model
16
+ MyJohnDeereApi::Model::Asset
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ require 'json'
2
+
3
+ module MyJohnDeereApi::Request
4
+ class Individual::Base
5
+ attr_reader :client, :id, :associations, :response
6
+
7
+ ##
8
+ # Initialize with a client, and asset id
9
+
10
+ def initialize(client, id, associations = {})
11
+ @client = client
12
+ @id = id
13
+ @associations = associations
14
+ end
15
+
16
+ ##
17
+ # client accessor
18
+
19
+ def accessor
20
+ return @accessor if defined?(@accessor)
21
+ @accessor = client&.accessor
22
+ end
23
+
24
+ ##
25
+ # The object being requested, an asset in this case
26
+
27
+ def object
28
+ return @object if defined?(@object)
29
+
30
+ request unless response
31
+ @object = model.new(client, JSON.parse(response.body))
32
+ end
33
+
34
+ private
35
+
36
+ ##
37
+ # Make the request
38
+
39
+ def request
40
+ @response = accessor.get(resource, headers)
41
+ end
42
+
43
+ ##
44
+ # Headers for GET request
45
+
46
+ def headers
47
+ @headers ||= {
48
+ 'Accept' => 'application/vnd.deere.axiom.v3+json',
49
+ }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+
3
+ module MyJohnDeereApi::Request
4
+ class Individual::ContributionDefinition < Individual::Base
5
+ ##
6
+ # The resource path for the first page in the collection
7
+
8
+ def resource
9
+ "/contributionDefinitions/#{id}"
10
+ end
11
+
12
+ ##
13
+ # This is the class used to model the data
14
+
15
+ def model
16
+ MyJohnDeereApi::Model::ContributionDefinition
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+
3
+ module MyJohnDeereApi::Request
4
+ class Individual::ContributionProduct < Individual::Base
5
+ ##
6
+ # The resource path for the first page in the collection
7
+
8
+ def resource
9
+ "/contributionProducts/#{id}"
10
+ end
11
+
12
+ ##
13
+ # This is the class used to model the data
14
+
15
+ def model
16
+ MyJohnDeereApi::Model::ContributionProduct
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+
3
+ module MyJohnDeereApi::Request
4
+ class Individual::Field < Individual::Base
5
+ ##
6
+ # The resource path for the first page in the collection
7
+
8
+ def resource
9
+ "/organizations/#{associations[:organization]}/fields/#{id}"
10
+ end
11
+
12
+ ##
13
+ # This is the class used to model the data
14
+
15
+ def model
16
+ MyJohnDeereApi::Model::Field
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+
3
+ module MyJohnDeereApi::Request
4
+ class Individual::Organization < Individual::Base
5
+ ##
6
+ # The resource path for the first page in the collection
7
+
8
+ def resource
9
+ "/organizations/#{id}"
10
+ end
11
+
12
+ ##
13
+ # This is the class used to model the data
14
+
15
+ def model
16
+ MyJohnDeereApi::Model::Organization
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module MyJohnDeereApi::Request::Update
2
+ autoload :Base, 'my_john_deere_api/request/update/base'
3
+ autoload :Asset, 'my_john_deere_api/request/update/asset'
4
+ end
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+
3
+ module MyJohnDeereApi
4
+ class Request::Update::Asset < Request::Update::Base
5
+ include Validators::Asset
6
+ include Helpers::ValidateContributionDefinition
7
+
8
+ private
9
+
10
+ ##
11
+ # Path supplied to API
12
+
13
+ def resource
14
+ @resource ||= "/assets/#{attributes[:id]}"
15
+ end
16
+
17
+ ##
18
+ # Request body
19
+
20
+ def request_body
21
+ return @body if defined?(@body)
22
+
23
+ validate_contribution_definition
24
+
25
+ @body = {
26
+ title: attributes[:title],
27
+ assetCategory: attributes[:asset_category],
28
+ assetType: attributes[:asset_type],
29
+ assetSubType: attributes[:asset_sub_type],
30
+ links: [
31
+ {
32
+ '@type' => 'Link',
33
+ 'rel' => 'contributionDefinition',
34
+ 'uri' => "#{accessor.consumer.site}/contributionDefinitions/#{client.contribution_definition_id}"
35
+ }
36
+ ]
37
+ }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,67 @@
1
+ require 'json'
2
+
3
+ module MyJohnDeereApi
4
+ class Request::Update::Base
5
+ include Validators::Base
6
+
7
+ attr_reader :client, :item, :attributes, :response
8
+
9
+ ##
10
+ # Accepts a valid oAuth AccessToken, the item to be updated,
11
+ # and a hash of attributes.
12
+ #
13
+ # category/type/subtype must be a recognized combination as defined above.
14
+
15
+ def initialize(client, item, attributes)
16
+ @client = client
17
+ @item = item
18
+ @attributes = item.attributes.merge(attributes)
19
+
20
+ process_attributes
21
+ end
22
+
23
+ #
24
+ # client accessor
25
+
26
+ def accessor
27
+ return @accessor if defined?(@accessor)
28
+ @accessor = client&.accessor
29
+ end
30
+
31
+ ##
32
+ # Make the request, if the instance is valid
33
+
34
+ def request
35
+ validate!
36
+
37
+ @response = accessor.put(resource, request_body.to_json, headers)
38
+ end
39
+
40
+ ##
41
+ # Object, same as item for updates
42
+
43
+ def object
44
+ @object ||= item
45
+ end
46
+
47
+ private
48
+
49
+ ##
50
+ # Convert inputs into working attributes. This allows us to auto-create
51
+ # some attributes from others, or set defaults, on a class-by-class basis.
52
+ # See Request::Create::AssetLocation for an example.
53
+
54
+ def process_attributes
55
+ end
56
+
57
+ ##
58
+ # Headers for PUT request
59
+
60
+ def headers
61
+ @headers ||= {
62
+ 'Accept' => 'application/vnd.deere.axiom.v3+json',
63
+ 'Content-Type' => 'application/vnd.deere.axiom.v3+json'
64
+ }
65
+ end
66
+ end
67
+ end