my_john_deere_api 2.5.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/lib/my_john_deere_api.rb +1 -1
  3. data/lib/my_john_deere_api/authorize.rb +36 -27
  4. data/lib/my_john_deere_api/client.rb +38 -29
  5. data/lib/my_john_deere_api/consumer.rb +46 -35
  6. data/lib/my_john_deere_api/helpers/case_conversion.rb +5 -2
  7. data/lib/my_john_deere_api/helpers/uri_helpers.rb +1 -1
  8. data/lib/my_john_deere_api/model/contribution_definition.rb +1 -1
  9. data/lib/my_john_deere_api/net_http_retry/decorator.rb +10 -11
  10. data/lib/my_john_deere_api/net_http_retry/invalid_response_error.rb +2 -2
  11. data/lib/my_john_deere_api/request/collection/asset_locations.rb +1 -1
  12. data/lib/my_john_deere_api/request/collection/assets.rb +1 -1
  13. data/lib/my_john_deere_api/request/collection/base.rb +2 -14
  14. data/lib/my_john_deere_api/request/collection/contribution_definitions.rb +1 -1
  15. data/lib/my_john_deere_api/request/collection/contribution_products.rb +1 -1
  16. data/lib/my_john_deere_api/request/collection/fields.rb +1 -1
  17. data/lib/my_john_deere_api/request/collection/flags.rb +1 -1
  18. data/lib/my_john_deere_api/request/collection/organizations.rb +1 -1
  19. data/lib/my_john_deere_api/request/create/asset.rb +8 -11
  20. data/lib/my_john_deere_api/request/create/asset_location.rb +24 -25
  21. data/lib/my_john_deere_api/request/create/base.rb +2 -20
  22. data/lib/my_john_deere_api/request/individual/asset.rb +2 -2
  23. data/lib/my_john_deere_api/request/individual/base.rb +6 -24
  24. data/lib/my_john_deere_api/request/individual/contribution_definition.rb +1 -1
  25. data/lib/my_john_deere_api/request/individual/contribution_product.rb +1 -1
  26. data/lib/my_john_deere_api/request/individual/field.rb +1 -1
  27. data/lib/my_john_deere_api/request/individual/organization.rb +1 -1
  28. data/lib/my_john_deere_api/request/update/asset.rb +2 -2
  29. data/lib/my_john_deere_api/request/update/base.rb +1 -19
  30. data/lib/my_john_deere_api/version.rb +1 -1
  31. data/test/lib/my_john_deere_api/authorize_test.rb +37 -25
  32. data/test/lib/my_john_deere_api/client_test.rb +22 -56
  33. data/test/lib/my_john_deere_api/consumer_test.rb +16 -28
  34. data/test/lib/my_john_deere_api/helpers/uri_helpers_test.rb +0 -10
  35. data/test/lib/my_john_deere_api/model/asset_location_test.rb +0 -4
  36. data/test/lib/my_john_deere_api/model/asset_test.rb +9 -8
  37. data/test/lib/my_john_deere_api/model/base_test.rb +4 -8
  38. data/test/lib/my_john_deere_api/model/contribution_definition_test.rb +3 -7
  39. data/test/lib/my_john_deere_api/model/contribution_product_test.rb +4 -7
  40. data/test/lib/my_john_deere_api/model/field_test.rb +4 -6
  41. data/test/lib/my_john_deere_api/model/flag_test.rb +6 -7
  42. data/test/lib/my_john_deere_api/model/organization_test.rb +3 -5
  43. data/test/lib/my_john_deere_api/net_http_retry/decorator_test.rb +14 -14
  44. data/test/lib/my_john_deere_api/net_http_retry/invalid_response_error_test.rb +22 -2
  45. data/test/lib/my_john_deere_api/request/collection/asset_locations_test.rb +2 -2
  46. data/test/lib/my_john_deere_api/request/collection/assets_test.rb +2 -2
  47. data/test/lib/my_john_deere_api/request/collection/contribution_definitions_test.rb +2 -2
  48. data/test/lib/my_john_deere_api/request/collection/contribution_products_test.rb +2 -2
  49. data/test/lib/my_john_deere_api/request/collection/fields_test.rb +2 -2
  50. data/test/lib/my_john_deere_api/request/collection/flags_test.rb +2 -2
  51. data/test/lib/my_john_deere_api/request/collection/organizations_test.rb +2 -2
  52. data/test/lib/my_john_deere_api/request/create/asset_location_test.rb +3 -2
  53. data/test/lib/my_john_deere_api/request/create/asset_test.rb +5 -4
  54. data/test/lib/my_john_deere_api/request/create/base_test.rb +0 -14
  55. data/test/lib/my_john_deere_api/request/individual/asset_test.rb +2 -3
  56. data/test/lib/my_john_deere_api/request/individual/base_test.rb +0 -1
  57. data/test/lib/my_john_deere_api/request/individual/contribution_definition_test.rb +2 -3
  58. data/test/lib/my_john_deere_api/request/individual/contribution_product_test.rb +2 -3
  59. data/test/lib/my_john_deere_api/request/individual/field_test.rb +2 -3
  60. data/test/lib/my_john_deere_api/request/individual/organization_test.rb +2 -3
  61. data/test/lib/my_john_deere_api/request/update/asset_test.rb +5 -17
  62. data/test/lib/my_john_deere_api/request/update/base_test.rb +0 -14
  63. data/test/support/helper.rb +14 -5
  64. data/test/support/link_helpers.rb +14 -0
  65. data/test/support/response_helpers.rb +18 -0
  66. data/test/support/vcr/catalog.yml +44 -37
  67. data/test/support/vcr/get_access_token.yml +90 -17
  68. data/test/support/vcr/get_refresh_token.yml +159 -0
  69. data/test/support/vcr/get_request_url.yml +51 -0
  70. data/test/support/vcr_setup.rb +80 -19
  71. metadata +11 -8
  72. data/test/support/vcr/get_request_token.yml +0 -83
@@ -11,8 +11,8 @@ module MyJohnDeereApi
11
11
 
12
12
  def initialize(response)
13
13
  message = {
14
- code: response.code,
15
- message: response.message,
14
+ code: response.status,
15
+ message: response.response.reason_phrase,
16
16
  body: response.body,
17
17
  }.to_json
18
18
 
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/assets/#{associations[:asset]}/locations"
9
+ "/platform/assets/#{associations[:asset]}/locations"
10
10
  end
11
11
 
12
12
  ##
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/organizations/#{associations[:organization]}/assets"
9
+ "/platform/organizations/#{associations[:organization]}/assets"
10
10
  end
11
11
 
12
12
  ##
@@ -15,14 +15,6 @@ module MyJohnDeereApi
15
15
  @items = []
16
16
  end
17
17
 
18
- ##
19
- # client accessor
20
-
21
- def accessor
22
- return @accessor if defined?(@accessor)
23
- @accessor = client&.accessor
24
- end
25
-
26
18
  ##
27
19
  # Iterate lazily through all records in the collection, fetching
28
20
  # additional pages as needed.
@@ -54,7 +46,7 @@ module MyJohnDeereApi
54
46
  def first_page
55
47
  return @first_page if defined?(@first_page)
56
48
 
57
- @first_page = JSON.parse(accessor.get(resource, headers).body)
49
+ @first_page = client.get(resource)
58
50
  extract_page_contents(@first_page)
59
51
 
60
52
  @first_page
@@ -63,14 +55,10 @@ module MyJohnDeereApi
63
55
  def fetch
64
56
  return unless @next_page
65
57
 
66
- page = JSON.parse(accessor.get(@next_page, headers).body)
58
+ page = client.get(@next_page)
67
59
  extract_page_contents(page)
68
60
  end
69
61
 
70
- def headers
71
- @headers ||= {accept: 'application/vnd.deere.axiom.v3+json'}
72
- end
73
-
74
62
  def extract_page_contents(page)
75
63
  add_items_from_page(page)
76
64
  set_next_page(page)
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/contributionProducts/#{associations[:contribution_product]}/contributionDefinitions"
9
+ "/platform/contributionProducts/#{associations[:contribution_product]}/contributionDefinitions"
10
10
  end
11
11
 
12
12
  ##
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/contributionProducts?clientControlled=true"
9
+ "/platform/contributionProducts?clientControlled=true"
10
10
  end
11
11
 
12
12
  ##
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/organizations/#{associations[:organization]}/fields"
9
+ "/platform/organizations/#{associations[:organization]}/fields"
10
10
  end
11
11
 
12
12
  ##
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/organizations/#{associations[:organization]}/fields/#{associations[:field]}/flags"
9
+ "/platform/organizations/#{associations[:organization]}/fields/#{associations[:field]}/flags"
10
10
  end
11
11
 
12
12
  ##
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- '/organizations'
9
+ '/platform/organizations'
10
10
  end
11
11
 
12
12
  ##
@@ -8,27 +8,24 @@ module MyJohnDeereApi
8
8
  private
9
9
 
10
10
  ##
11
- # Retrieve newly created record
11
+ # id of newly created record
12
12
 
13
- def fetch_record
14
- path = response['location'].split('/platform').last
15
- result = accessor.get(path, headers)
16
-
17
- JSON.parse(result.body)
13
+ def record_id
14
+ response.headers['location'].split('/').last
18
15
  end
19
16
 
20
17
  ##
21
- # This is the class used to model the data
18
+ # This is the class used to fetch an individual item
22
19
 
23
- def model
24
- Model::Asset
20
+ def individual_class
21
+ Request::Individual::Asset
25
22
  end
26
23
 
27
24
  ##
28
25
  # Path supplied to API
29
26
 
30
27
  def resource
31
- @resource ||= "/organizations/#{attributes[:organization_id]}/assets"
28
+ @resource ||= "/platform/organizations/#{attributes[:organization_id]}/assets"
32
29
  end
33
30
 
34
31
  ##
@@ -48,7 +45,7 @@ module MyJohnDeereApi
48
45
  {
49
46
  '@type' => 'Link',
50
47
  'rel' => 'contributionDefinition',
51
- 'uri' => "#{accessor.consumer.site}/contributionDefinitions/#{client.contribution_definition_id}"
48
+ 'uri' => client.contribution_definition_uri,
52
49
  }
53
50
  ]
54
51
  }
@@ -3,6 +3,29 @@ require 'date'
3
3
  module MyJohnDeereApi
4
4
  class Request::Create::AssetLocation < Request::Create::Base
5
5
  include Validators::AssetLocation
6
+ include Helpers::UriHelpers
7
+
8
+ ##
9
+ # Object created by request
10
+ #
11
+ # There is no endpoint to fetch a single location by id, so we have to
12
+ # override the `object` method from the base class.
13
+ #
14
+ # We have to fetch locations in bulk via the asset, but there could be
15
+ # thousands. We limit the request to just the first record from the
16
+ # location list endpoint, since locations are returned newest to oldest.
17
+
18
+ def object
19
+ return @object if defined?(@object)
20
+
21
+ request unless response
22
+
23
+ path = uri_path(response.headers['location']) + '?count=1'
24
+ result = client.get(path)
25
+ record = result['values'].first
26
+
27
+ @object = Model::AssetLocation.new(client, record)
28
+ end
6
29
 
7
30
  private
8
31
 
@@ -72,24 +95,7 @@ module MyJohnDeereApi
72
95
  # Path supplied to API
73
96
 
74
97
  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
98
+ @resource ||= "/platform/assets/#{attributes[:asset_id]}/locations"
93
99
  end
94
100
 
95
101
  ##
@@ -99,12 +105,5 @@ module MyJohnDeereApi
99
105
  stamp = DateTime.parse(timestamp).to_time + seconds
100
106
  stamp.to_datetime.strftime('%Y-%m-%dT%H:%M:%SZ')
101
107
  end
102
-
103
- ##
104
- # This is the class used to model the data
105
-
106
- def model
107
- Model::AssetLocation
108
- end
109
108
  end
110
109
  end
@@ -16,21 +16,13 @@ module MyJohnDeereApi
16
16
  process_attributes
17
17
  end
18
18
 
19
- ##
20
- # client accessor
21
-
22
- def accessor
23
- return @accessor if defined?(@accessor)
24
- @accessor = client&.accessor
25
- end
26
-
27
19
  ##
28
20
  # Make the request, if the instance is valid
29
21
 
30
22
  def request
31
23
  validate!
32
24
 
33
- @response = accessor.post(resource, request_body.to_json, headers)
25
+ @response = client.post(resource, request_body)
34
26
  end
35
27
 
36
28
  ##
@@ -41,7 +33,7 @@ module MyJohnDeereApi
41
33
 
42
34
  request unless response
43
35
 
44
- @object = model.new(client, fetch_record)
36
+ @object = individual_class.new(client, record_id).object
45
37
  end
46
38
 
47
39
  private
@@ -53,15 +45,5 @@ module MyJohnDeereApi
53
45
 
54
46
  def process_attributes
55
47
  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
48
  end
67
49
  end
@@ -3,10 +3,10 @@ require 'json'
3
3
  module MyJohnDeereApi::Request
4
4
  class Individual::Asset < Individual::Base
5
5
  ##
6
- # The resource path for the first page in the collection
6
+ # The resource path for the object
7
7
 
8
8
  def resource
9
- "/assets/#{id}"
9
+ "/platform/assets/#{id}"
10
10
  end
11
11
 
12
12
  ##
@@ -2,7 +2,7 @@ require 'json'
2
2
 
3
3
  module MyJohnDeereApi::Request
4
4
  class Individual::Base
5
- attr_reader :client, :id, :associations, :response
5
+ attr_reader :client, :id, :associations
6
6
 
7
7
  ##
8
8
  # Initialize with a client, and asset id
@@ -13,40 +13,22 @@ module MyJohnDeereApi::Request
13
13
  @associations = associations
14
14
  end
15
15
 
16
- ##
17
- # client accessor
18
-
19
- def accessor
20
- return @accessor if defined?(@accessor)
21
- @accessor = client&.accessor
22
- end
23
-
24
16
  ##
25
17
  # The object being requested, an asset in this case
26
18
 
27
19
  def object
28
20
  return @object if defined?(@object)
29
-
30
- request unless response
31
- @object = model.new(client, JSON.parse(response.body))
21
+ @object = model.new(client, response)
32
22
  end
33
23
 
34
24
  private
35
25
 
36
26
  ##
37
- # Make the request
38
-
39
- def request
40
- @response = accessor.get(resource, headers)
41
- end
42
-
43
- ##
44
- # Headers for GET request
27
+ # response from object request
45
28
 
46
- def headers
47
- @headers ||= {
48
- 'Accept' => 'application/vnd.deere.axiom.v3+json',
49
- }
29
+ def response
30
+ return @response if defined?(@response)
31
+ @response = client.get(resource)
50
32
  end
51
33
  end
52
34
  end
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/contributionDefinitions/#{id}"
9
+ "/platform/contributionDefinitions/#{id}"
10
10
  end
11
11
 
12
12
  ##
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/contributionProducts/#{id}"
9
+ "/platform/contributionProducts/#{id}"
10
10
  end
11
11
 
12
12
  ##
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/organizations/#{associations[:organization]}/fields/#{id}"
9
+ "/platform/organizations/#{associations[:organization]}/fields/#{id}"
10
10
  end
11
11
 
12
12
  ##
@@ -6,7 +6,7 @@ module MyJohnDeereApi::Request
6
6
  # The resource path for the first page in the collection
7
7
 
8
8
  def resource
9
- "/organizations/#{id}"
9
+ "/platform/organizations/#{id}"
10
10
  end
11
11
 
12
12
  ##
@@ -11,7 +11,7 @@ module MyJohnDeereApi
11
11
  # Path supplied to API
12
12
 
13
13
  def resource
14
- @resource ||= "/assets/#{attributes[:id]}"
14
+ @resource ||= "/platform/assets/#{attributes[:id]}"
15
15
  end
16
16
 
17
17
  ##
@@ -31,7 +31,7 @@ module MyJohnDeereApi
31
31
  {
32
32
  '@type' => 'Link',
33
33
  'rel' => 'contributionDefinition',
34
- 'uri' => "#{accessor.consumer.site}/contributionDefinitions/#{client.contribution_definition_id}"
34
+ 'uri' => client.contribution_definition_uri,
35
35
  }
36
36
  ]
37
37
  }
@@ -20,21 +20,13 @@ module MyJohnDeereApi
20
20
  process_attributes
21
21
  end
22
22
 
23
- #
24
- # client accessor
25
-
26
- def accessor
27
- return @accessor if defined?(@accessor)
28
- @accessor = client&.accessor
29
- end
30
-
31
23
  ##
32
24
  # Make the request, if the instance is valid
33
25
 
34
26
  def request
35
27
  validate!
36
28
 
37
- @response = accessor.put(resource, request_body.to_json, headers)
29
+ @response = client.put(resource, request_body.to_json)
38
30
  end
39
31
 
40
32
  ##
@@ -53,15 +45,5 @@ module MyJohnDeereApi
53
45
 
54
46
  def process_attributes
55
47
  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
48
  end
67
49
  end