my_john_deere_api 0.12.4 → 0.12.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/lib/my_john_deere_api/client.rb +10 -3
  3. data/lib/my_john_deere_api/model/contribution_product.rb +24 -0
  4. data/lib/my_john_deere_api/model/field.rb +1 -1
  5. data/lib/my_john_deere_api/model/organization.rb +1 -1
  6. data/lib/my_john_deere_api/model.rb +1 -0
  7. data/lib/my_john_deere_api/request/collection/asset_locations.rb +2 -2
  8. data/lib/my_john_deere_api/request/collection/assets.rb +2 -2
  9. data/lib/my_john_deere_api/request/collection/contribution_products.rb +26 -0
  10. data/lib/my_john_deere_api/request/collection.rb +1 -0
  11. data/lib/my_john_deere_api/request/individual/contribution_product.rb +19 -0
  12. data/lib/my_john_deere_api/request/individual.rb +3 -2
  13. data/lib/my_john_deere_api/version.rb +1 -1
  14. data/test/lib/my_john_deere_api/authorize_test.rb +6 -6
  15. data/test/lib/my_john_deere_api/client_test.rb +31 -70
  16. data/test/lib/my_john_deere_api/consumer_test.rb +14 -14
  17. data/test/lib/my_john_deere_api/model/asset_location_test.rb +8 -23
  18. data/test/lib/my_john_deere_api/model/asset_test.rb +6 -9
  19. data/test/lib/my_john_deere_api/model/base_test.rb +3 -4
  20. data/test/lib/my_john_deere_api/model/contribution_product_test.rb +68 -0
  21. data/test/lib/my_john_deere_api/model/field_test.rb +8 -11
  22. data/test/lib/my_john_deere_api/model/flag_test.rb +1 -1
  23. data/test/lib/my_john_deere_api/model/organization_test.rb +19 -22
  24. data/test/lib/my_john_deere_api/model_test.rb +4 -0
  25. data/test/lib/my_john_deere_api/request/collection/asset_locations_test.rb +9 -48
  26. data/test/lib/my_john_deere_api/request/collection/assets_test.rb +9 -31
  27. data/test/lib/my_john_deere_api/request/collection/base_test.rb +2 -5
  28. data/test/lib/my_john_deere_api/request/collection/contribution_products_test.rb +88 -0
  29. data/test/lib/my_john_deere_api/request/collection/fields_test.rb +4 -12
  30. data/test/lib/my_john_deere_api/request/collection/flags_test.rb +5 -19
  31. data/test/lib/my_john_deere_api/request/collection/organizations_test.rb +2 -4
  32. data/test/lib/my_john_deere_api/request/collection_test.rb +4 -0
  33. data/test/lib/my_john_deere_api/request/create/asset_location_test.rb +20 -50
  34. data/test/lib/my_john_deere_api/request/create/asset_test.rb +10 -24
  35. data/test/lib/my_john_deere_api/request/create/base_test.rb +0 -2
  36. data/test/lib/my_john_deere_api/request/individual/asset_test.rb +0 -3
  37. data/test/lib/my_john_deere_api/request/individual/base_test.rb +0 -3
  38. data/test/lib/my_john_deere_api/request/individual/contribution_product_test.rb +32 -0
  39. data/test/lib/my_john_deere_api/request/individual_test.rb +4 -0
  40. data/test/support/helper.rb +56 -5
  41. data/test/support/vcr/catalog.yml +51 -7
  42. data/test/support/vcr/delete_asset.yml +51 -10
  43. data/test/support/vcr/get_access_token.yml +8 -8
  44. data/test/support/vcr/get_asset.yml +107 -10
  45. data/test/support/vcr/get_asset_locations.yml +154 -17
  46. data/test/support/vcr/get_assets.yml +110 -10
  47. data/test/support/vcr/get_contribution_product.yml +91 -0
  48. data/test/support/vcr/get_contribution_products.yml +91 -0
  49. data/test/support/vcr/get_fields.yml +108 -11
  50. data/test/support/vcr/get_flags.yml +11 -11
  51. data/test/support/vcr/get_organization.yml +90 -0
  52. data/test/support/vcr/get_organizations.yml +73 -30
  53. data/test/support/vcr/get_request_token.yml +14 -14
  54. data/test/support/vcr/post_asset_locations.yml +165 -20
  55. data/test/support/vcr/post_assets.yml +119 -22
  56. data/test/support/vcr/{app_consumer.yml → url.yml} +7 -7
  57. data/test/support/vcr/warning.txt +24 -0
  58. data/test/support/vcr_setup.rb +420 -0
  59. metadata +14 -4
  60. data/test/support/vcr/post_flag_categories.yml +0 -49
@@ -0,0 +1,68 @@
1
+ require 'support/helper'
2
+
3
+ describe 'MyJohnDeereApi::Model::ContributionProduct' do
4
+ let(:record) do
5
+ {
6
+ "@type" => "ContributionProduct",
7
+ "marketPlaceName" => "SpiffyApp",
8
+ "marketPlaceDescription" => "SpiffyApp",
9
+ "marketPlaceLogo" => "https://example.com/logo.png",
10
+ "defaultLocale" => "en-us",
11
+ "currentStatus" => "APPROVED",
12
+ "activationCallback" => "https://example.com/activation_callback",
13
+ "previewImages" => ["https://example.com/preview.png"],
14
+ "supportedRegions" => ["US"],
15
+ "supportedOperationCenters" => ["something"],
16
+ "id" => "#{contribution_product_id}",
17
+ "links" => [
18
+ {
19
+ "@type" => "Link",
20
+ "rel" => "self",
21
+ "uri" => "https://sandboxapi.deere.com/platform/contributionProducts/#{contribution_product_id}"
22
+ },
23
+ {
24
+ "@type" => "Link",
25
+ "rel" => "contributionDefinition",
26
+ "uri" => "https://sandboxapi.deere.com/platform/contributionProducts/#{contribution_product_id}/contributionDefinitions"
27
+ }
28
+ ]
29
+ }
30
+ end
31
+
32
+ describe '#initialize' do
33
+ def link_for label
34
+ camel_label = label.gsub(/_(.)/){|m| m[1].upcase}
35
+ record['links'].detect{|link| link['rel'] == camel_label}['uri'].gsub('https://sandboxapi.deere.com/platform', '')
36
+ end
37
+
38
+ it 'sets the attributes from the given record' do
39
+ product = JD::Model::ContributionProduct.new(record)
40
+
41
+ # basic attributes
42
+ assert_equal record['id'], product.id
43
+ assert_equal record['marketPlaceName'], product.market_place_name
44
+ assert_equal record['marketPlaceDescription'], product.market_place_description
45
+ assert_equal record['defaultLocale'], product.default_locale
46
+ assert_equal record['currentStatus'], product.current_status
47
+ assert_equal record['activationCallback'], product.activation_callback
48
+ assert_equal record['previewImages'], product.preview_images
49
+ assert_equal record['supportedRegions'], product.supported_regions
50
+ assert_equal record['supportedOperationCenters'], product.supported_operation_centers
51
+ end
52
+
53
+ it 'links to other things' do
54
+ product = JD::Model::ContributionProduct.new(record)
55
+
56
+ ['self', 'contribution_definition'].each do |association|
57
+ assert_equal link_for(association), product.links[association]
58
+ end
59
+ end
60
+
61
+ it 'accepts an optional accessor' do
62
+ mock_accessor = 'mock-accessor'
63
+
64
+ asset = JD::Model::ContributionProduct.new(record, mock_accessor)
65
+ assert_equal mock_accessor, asset.accessor
66
+ end
67
+ end
68
+ end
@@ -8,16 +8,13 @@ describe 'MyJohnDeereApi::Model::Field' do
8
8
  "archived"=>false,
9
9
  "id"=>"123456",
10
10
  "links"=>[
11
- {"@type"=>"Link", "rel"=>"self", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/fields/123456"},
12
- {"@type"=>"Link", "rel"=>"clients", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/fields/123456/clients"},
13
- {"@type"=>"Link", "rel"=>"notes", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/fields/123456/notes"},
11
+ {"@type"=>"Link", "rel"=>"self", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields/#{field_id}"},
12
+ {"@type"=>"Link", "rel"=>"clients", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields/#{field_id}/clients"},
13
+ {"@type"=>"Link", "rel"=>"notes", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields/#{field_id}/notes"},
14
14
  ]
15
15
  }
16
16
  end
17
17
 
18
- let(:client) { JD::Client.new(API_KEY, API_SECRET, environment: :sandbox, access: [ACCESS_TOKEN, ACCESS_SECRET]) }
19
- let(:accessor) { VCR.use_cassette('catalog') { client.send(:accessor) } }
20
-
21
18
  describe '#initialize' do
22
19
  def link_for label
23
20
  record['links'].detect{|link| link['rel'] == label}['uri'].gsub('https://sandboxapi.deere.com/platform', '')
@@ -42,10 +39,10 @@ describe 'MyJohnDeereApi::Model::Field' do
42
39
  end
43
40
 
44
41
  it 'accepts an optional accessor' do
45
- accessor = 'mock-accessor'
42
+ mock_accessor = 'mock-accessor'
46
43
 
47
- field = JD::Model::Field.new(record, accessor)
48
- assert_equal accessor, field.accessor
44
+ field = JD::Model::Field.new(record, mock_accessor)
45
+ assert_equal mock_accessor, field.accessor
49
46
  end
50
47
  end
51
48
 
@@ -54,7 +51,7 @@ describe 'MyJohnDeereApi::Model::Field' do
54
51
  accessor
55
52
  organization = VCR.use_cassette('get_organizations') { client.organizations.first }
56
53
  field = VCR.use_cassette('get_fields') { organization.fields.first }
57
- flags = VCR.use_cassette('get_flags') { field.flags }
54
+ flags = VCR.use_cassette('get_flags') { field.flags.all }
58
55
 
59
56
  assert_kind_of Array, flags
60
57
 
@@ -75,7 +72,7 @@ describe 'MyJohnDeereApi::Model::Field' do
75
72
  describe 'private #organization_id' do
76
73
  it "infers the organization_id from links" do
77
74
  field = JD::Model::Field.new(record)
78
- assert_equal '123456', field.send(:organization_id)
75
+ assert_equal organization_id, field.send(:organization_id)
79
76
  end
80
77
  end
81
78
  end
@@ -11,7 +11,7 @@ describe 'MyJohnDeereApi::Model::Flag' do
11
11
  "notes"=>"Our flag is a very, very, very nice flag!",
12
12
  "id"=>"123456",
13
13
  "links"=>[
14
- {"@type"=>"Link", "rel"=>"field", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/fields/123456"},
14
+ {"@type"=>"Link", "rel"=>"field", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields/#{field_id}"},
15
15
  {"@type"=>"Link", "rel"=>"createdBy", "uri"=>"https://sandboxapi.deere.com/platform/users/bobsmith"},
16
16
  {"@type"=>"Link", "rel"=>"lastModifiedBy", "uri"=>"https://sandboxapi.deere.com/platform/users/jonsmith"},
17
17
  ]
@@ -9,28 +9,25 @@ describe 'MyJohnDeereApi::Model::Organization' do
9
9
  "member"=>true,
10
10
  "id"=>"123456",
11
11
  "links"=>[
12
- {"@type"=>"Link", "rel"=>"self", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456"},
13
- {"@type"=>"Link", "rel"=>"machines", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/machines"},
14
- {"@type"=>"Link", "rel"=>"wdtCapableMachines", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/machines?capability=wdt"},
15
- {"@type"=>"Link", "rel"=>"files", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/files"},
16
- {"@type"=>"Link", "rel"=>"transferableFiles", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/files?transferable=true"},
17
- {"@type"=>"Link", "rel"=>"uploadFile", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/files"},
18
- {"@type"=>"Link", "rel"=>"sendFileToMachine", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/fileTransfers"},
19
- {"@type"=>"Link", "rel"=>"addMachine", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/machines"},
20
- {"@type"=>"Link", "rel"=>"addField", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/fields"},
21
- {"@type"=>"Link", "rel"=>"assets", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/assets"},
22
- {"@type"=>"Link", "rel"=>"fields", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/fields"},
23
- {"@type"=>"Link", "rel"=>"farms", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/farms"},
24
- {"@type"=>"Link", "rel"=>"boundaries", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/boundaries"},
25
- {"@type"=>"Link", "rel"=>"clients", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/clients"},
26
- {"@type"=>"Link", "rel"=>"controllers", "uri"=>"https://sandboxapi.deere.com/platform/organizations/123456/orgController"}
12
+ {"@type"=>"Link", "rel"=>"self", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}"},
13
+ {"@type"=>"Link", "rel"=>"machines", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/machines"},
14
+ {"@type"=>"Link", "rel"=>"wdtCapableMachines", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/machines?capability=wdt"},
15
+ {"@type"=>"Link", "rel"=>"files", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/files"},
16
+ {"@type"=>"Link", "rel"=>"transferableFiles", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/files?transferable=true"},
17
+ {"@type"=>"Link", "rel"=>"uploadFile", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/files"},
18
+ {"@type"=>"Link", "rel"=>"sendFileToMachine", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fileTransfers"},
19
+ {"@type"=>"Link", "rel"=>"addMachine", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/machines"},
20
+ {"@type"=>"Link", "rel"=>"addField", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields"},
21
+ {"@type"=>"Link", "rel"=>"assets", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/assets"},
22
+ {"@type"=>"Link", "rel"=>"fields", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields"},
23
+ {"@type"=>"Link", "rel"=>"farms", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/farms"},
24
+ {"@type"=>"Link", "rel"=>"boundaries", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/boundaries"},
25
+ {"@type"=>"Link", "rel"=>"clients", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/clients"},
26
+ {"@type"=>"Link", "rel"=>"controllers", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/orgController"}
27
27
  ]
28
28
  }
29
29
  end
30
30
 
31
- let(:client) { JD::Client.new(API_KEY, API_SECRET, environment: :sandbox, access: [ACCESS_TOKEN, ACCESS_SECRET]) }
32
- let(:accessor) { VCR.use_cassette('catalog') { client.send(:accessor) } }
33
-
34
31
  describe '#initialize(record, accessor = nil)' do
35
32
  def link_for label
36
33
  record['links'].detect{|link| link['rel'] == label}['uri'].gsub('https://sandboxapi.deere.com/platform', '')
@@ -55,10 +52,10 @@ describe 'MyJohnDeereApi::Model::Organization' do
55
52
  end
56
53
 
57
54
  it 'accepts an optional accessor' do
58
- accessor = 'mock-accessor'
55
+ mock_accessor = 'mock-accessor'
59
56
 
60
- organization = JD::Model::Organization.new(record, accessor)
61
- assert_equal accessor, organization.accessor
57
+ organization = JD::Model::Organization.new(record, mock_accessor)
58
+ assert_equal mock_accessor, organization.accessor
62
59
  end
63
60
  end
64
61
 
@@ -66,7 +63,7 @@ describe 'MyJohnDeereApi::Model::Organization' do
66
63
  it 'returns a collection of fields for this organization' do
67
64
  accessor
68
65
  organization = VCR.use_cassette('get_organizations') { client.organizations.first }
69
- fields = VCR.use_cassette('get_fields') { organization.fields }
66
+ fields = VCR.use_cassette('get_fields') { organization.fields.all }
70
67
 
71
68
  assert_kind_of Array, fields
72
69
 
@@ -14,6 +14,10 @@ describe 'MyJohnDeereApi::Model' do
14
14
  assert JD::Model::AssetLocation
15
15
  end
16
16
 
17
+ it 'loads Model::ContributionProduct' do
18
+ assert JD::Model::ContributionProduct
19
+ end
20
+
17
21
  it 'loads Model::Organization' do
18
22
  assert JD::Model::Organization
19
23
  end
@@ -3,14 +3,6 @@ require 'yaml'
3
3
  require 'json'
4
4
 
5
5
  describe 'MyJohnDeereApi::Request::Collection::AssetLocations' do
6
- let(:asset_id) do
7
- contents = File.read('test/support/vcr/get_assets.yml')
8
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
9
- JSON.parse(body)['values'].first['id']
10
- end
11
-
12
- let(:client) { JD::Client.new(API_KEY, API_SECRET, environment: :sandbox, access: [ACCESS_TOKEN, ACCESS_SECRET]) }
13
- let(:accessor) { VCR.use_cassette('catalog') { client.send(:accessor) } }
14
6
  let(:collection) { JD::Request::Collection::AssetLocations.new(accessor, asset: asset_id) }
15
7
  let(:object) { collection }
16
8
 
@@ -22,10 +14,10 @@ describe 'MyJohnDeereApi::Request::Collection::AssetLocations' do
22
14
  end
23
15
 
24
16
  it 'accepts associations' do
25
- collection = JD::Request::Collection::AssetLocations.new(accessor, asset: '123')
17
+ collection = JD::Request::Collection::AssetLocations.new(accessor, asset: asset_id)
26
18
 
27
19
  assert_kind_of Hash, collection.associations
28
- assert_equal '123', collection.associations[:asset]
20
+ assert_equal asset_id, collection.associations[:asset]
29
21
  end
30
22
  end
31
23
 
@@ -50,40 +42,8 @@ describe 'MyJohnDeereApi::Request::Collection::AssetLocations' do
50
42
  end
51
43
 
52
44
  describe '#create(attributes)' do
53
- let(:asset_id) { ENV['ASSET_ID'] }
54
- let(:timestamp) { DateTime.parse(timestamp_string) }
55
- let(:timestamp_string) { '2020-01-21T10:49:00Z' }
56
- let(:coordinates) { [-103.115633, 41.670166] }
57
-
58
- let(:geometry) do
59
- {
60
- type: 'Feature',
61
- geometry: {
62
- geometries: [
63
- coordinates: coordinates,
64
- type: 'Point'
65
- ],
66
- type: 'GeometryCollection'
67
- }
68
- }
69
- end
70
-
71
- let(:measurement_data) do
72
- [
73
- {
74
- name: 'Temperature',
75
- value: '68.0',
76
- unit: 'F'
77
- }
78
- ]
79
- end
80
-
81
45
  it 'creates a new asset with the given attributes' do
82
- attributes = {
83
- timestamp: timestamp,
84
- geometry: geometry,
85
- measurement_data: measurement_data
86
- }
46
+ attributes = CONFIG.asset_location_attributes
87
47
 
88
48
  object = VCR.use_cassette('post_asset_locations') { collection.create(attributes) }
89
49
 
@@ -91,20 +51,21 @@ describe 'MyJohnDeereApi::Request::Collection::AssetLocations' do
91
51
 
92
52
  # API returns seconds with decimals, even though they're always zero
93
53
  integer_stamp = DateTime.parse(object.timestamp).strftime('%Y-%m-%dT%H:%M:%SZ')
54
+ expected_stamp = DateTime.parse(attributes[:timestamp]).strftime('%Y-%m-%dT%H:%M:%SZ')
94
55
 
95
56
  # API returns string keys and an extra '@type' key
96
57
  object_measurement_data = object.measurement_data.first.transform_keys{|k| k.to_sym}.slice(:name, :value, :unit)
97
58
 
98
- assert_equal timestamp_string, integer_stamp
99
- assert_equal geometry.to_json, object.geometry.to_json
100
- assert_equal measurement_data.first, object_measurement_data
59
+ assert_equal expected_stamp, integer_stamp
60
+ assert_equal attributes[:geometry].to_json, object.geometry.to_json
61
+ assert_equal attributes[:measurement_data].first, object_measurement_data
101
62
  end
102
63
  end
103
64
 
104
65
  describe '#count' do
105
66
  let(:server_response) do
106
67
  contents = File.read('test/support/vcr/get_asset_locations.yml')
107
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
68
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
108
69
  JSON.parse(body)
109
70
  end
110
71
 
@@ -120,7 +81,7 @@ describe 'MyJohnDeereApi::Request::Collection::AssetLocations' do
120
81
  describe 'results' do
121
82
  let(:location_timestamps) do
122
83
  contents = File.read('test/support/vcr/get_asset_locations.yml')
123
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
84
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
124
85
  JSON.parse(body)['values'].map{|v| v['timestamp']}
125
86
  end
126
87
 
@@ -3,14 +3,6 @@ require 'yaml'
3
3
  require 'json'
4
4
 
5
5
  describe 'MyJohnDeereApi::Request::Collection::Assets' do
6
- let(:organization_id) do
7
- contents = File.read('test/support/vcr/get_organizations.yml')
8
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
9
- JSON.parse(body)['values'].first['id']
10
- end
11
-
12
- let(:client) { JD::Client.new(API_KEY, API_SECRET, environment: :sandbox, access: [ACCESS_TOKEN, ACCESS_SECRET]) }
13
- let(:accessor) { VCR.use_cassette('catalog') { client.send(:accessor) } }
14
6
  let(:collection) { JD::Request::Collection::Assets.new(accessor, organization: organization_id) }
15
7
  let(:object) { collection }
16
8
 
@@ -22,10 +14,10 @@ describe 'MyJohnDeereApi::Request::Collection::Assets' do
22
14
  end
23
15
 
24
16
  it 'accepts associations' do
25
- collection = JD::Request::Collection::Assets.new(accessor, organization: '123')
17
+ collection = JD::Request::Collection::Assets.new(accessor, organization: organization_id)
26
18
 
27
19
  assert_kind_of Hash, collection.associations
28
- assert_equal '123', collection.associations[:organization]
20
+ assert_equal organization_id, collection.associations[:organization]
29
21
  end
30
22
  end
31
23
 
@@ -49,33 +41,19 @@ describe 'MyJohnDeereApi::Request::Collection::Assets' do
49
41
  end
50
42
 
51
43
  describe '#create(attributes)' do
52
- let(:title) { 'i like turtles' }
53
- let(:category) { 'DEVICE' }
54
- let(:type) { 'SENSOR' }
55
- let(:subtype) { 'ENVIRONMENTAL' }
56
-
57
44
  it 'creates a new asset with the given attributes' do
58
- attributes = {
59
- contribution_definition_id: ENV['CONTRIBUTION_DEFINITION_ID'],
60
- title: title,
61
- asset_category: category,
62
- asset_type: type,
63
- asset_sub_type: subtype
64
- }
65
-
45
+ attributes = CONFIG.asset_attributes
66
46
  object = VCR.use_cassette('post_assets') { collection.create(attributes) }
67
47
 
68
48
  assert_kind_of JD::Model::Asset, object
69
- assert_equal title, object.title
70
- assert_equal category, object.asset_category
71
- assert_equal type, object.asset_type
72
- assert_equal subtype, object.asset_sub_type
49
+ assert_equal attributes[:title], object.title
50
+ assert_equal attributes[:asset_category], object.asset_category
51
+ assert_equal attributes[:asset_type], object.asset_type
52
+ assert_equal attributes[:asset_sub_type], object.asset_sub_type
73
53
  end
74
54
  end
75
55
 
76
56
  describe '#find(asset_id)' do
77
- let(:asset_id) { '123' }
78
-
79
57
  it 'retrieves the asset' do
80
58
  asset = VCR.use_cassette('get_asset') { collection.find(asset_id) }
81
59
  assert_kind_of JD::Model::Asset, asset
@@ -85,7 +63,7 @@ describe 'MyJohnDeereApi::Request::Collection::Assets' do
85
63
  describe '#count' do
86
64
  let(:server_response) do
87
65
  contents = File.read('test/support/vcr/get_assets.yml')
88
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
66
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
89
67
  JSON.parse(body)
90
68
  end
91
69
 
@@ -101,7 +79,7 @@ describe 'MyJohnDeereApi::Request::Collection::Assets' do
101
79
  describe 'results' do
102
80
  let(:asset_titles) do
103
81
  contents = File.read('test/support/vcr/get_assets.yml')
104
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
82
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
105
83
  JSON.parse(body)['values'].map{|v| v['title']}
106
84
  end
107
85
 
@@ -1,9 +1,6 @@
1
1
  require 'support/helper'
2
2
 
3
3
  describe 'MyJohnDeereApi::Request::Collection::Base' do
4
- let(:client) { JD::Client.new(API_KEY, API_SECRET, environment: :sandbox, access: [ACCESS_TOKEN, ACCESS_SECRET]) }
5
- let(:accessor) { VCR.use_cassette('catalog') { client.send(:accessor) } }
6
-
7
4
  describe '#initialize(access_token)' do
8
5
  it 'accepts an access token' do
9
6
  collection = JD::Request::Collection::Base.new(accessor)
@@ -11,10 +8,10 @@ describe 'MyJohnDeereApi::Request::Collection::Base' do
11
8
  end
12
9
 
13
10
  it 'accepts associations' do
14
- collection = JD::Request::Collection::Base.new(accessor, organization: '123')
11
+ collection = JD::Request::Collection::Base.new(accessor, organization: organization_id)
15
12
 
16
13
  assert_kind_of Hash, collection.associations
17
- assert_equal '123', collection.associations[:organization]
14
+ assert_equal organization_id, collection.associations[:organization]
18
15
  end
19
16
  end
20
17
 
@@ -0,0 +1,88 @@
1
+ require 'support/helper'
2
+ require 'yaml'
3
+ require 'json'
4
+
5
+ describe 'MyJohnDeereApi::Request::Collection::ContributionProducts' do
6
+ let(:klass) { MyJohnDeereApi::Request::Collection::ContributionProducts }
7
+
8
+ let(:collection) { klass.new(accessor, organization: organization_id) }
9
+ let(:object) { collection }
10
+
11
+ inherits_from JD::Request::Collection::Base
12
+
13
+ describe '#initialize(access_token)' do
14
+ it 'accepts an access token' do
15
+ assert_kind_of OAuth::AccessToken, collection.accessor
16
+ end
17
+
18
+ it 'accepts associations' do
19
+ collection = klass.new(accessor, organization: organization_id)
20
+
21
+ assert_kind_of Hash, collection.associations
22
+ assert_equal organization_id, collection.associations[:organization]
23
+ end
24
+ end
25
+
26
+ describe '#resource' do
27
+ it 'returns /contributionProducts' do
28
+ assert_equal "/contributionProducts?clientControlled=true", collection.resource
29
+ end
30
+ end
31
+
32
+ describe '#all' do
33
+ it 'returns all records' do
34
+ all = VCR.use_cassette('get_contribution_products') { collection.all }
35
+
36
+ assert_kind_of Array, all
37
+ assert_equal collection.count, all.size
38
+
39
+ all.each do |item|
40
+ assert_kind_of JD::Model::ContributionProduct, item
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#count' do
46
+ let(:server_response) do
47
+ contents = File.read('test/support/vcr/get_contribution_products.yml')
48
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
49
+ JSON.parse(body)
50
+ end
51
+
52
+ let(:server_count) { server_response['total'] }
53
+
54
+ it 'returns the total count of records in the collection' do
55
+ count = VCR.use_cassette('get_contribution_products') { collection.count }
56
+
57
+ assert_equal server_count, count
58
+ end
59
+ end
60
+
61
+ describe 'results' do
62
+ let(:product_names) do
63
+ contents = File.read('test/support/vcr/get_contribution_products.yml')
64
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
65
+
66
+ JSON.parse(body)['values'].map{|v| v['marketPlaceName']}
67
+ end
68
+
69
+ it 'returns all records as a single enumerator' do
70
+ count = VCR.use_cassette('get_contribution_products') { collection.count }
71
+ names = VCR.use_cassette('get_contribution_products') { collection.map(&:market_place_name) }
72
+
73
+ assert_kind_of Array, names
74
+ assert_equal count, names.size
75
+
76
+ product_names.each do |expected_name|
77
+ assert_includes names, expected_name
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '#find(contribution_product_id)' do
83
+ it 'retrieves the asset' do
84
+ product = VCR.use_cassette('get_contribution_product') { collection.find(contribution_product_id) }
85
+ assert_kind_of JD::Model::ContributionProduct, product
86
+ end
87
+ end
88
+ end
@@ -3,14 +3,6 @@ require 'yaml'
3
3
  require 'json'
4
4
 
5
5
  describe 'MyJohnDeereApi::Request::Collection::Fields' do
6
- let(:organization_id) do
7
- contents = File.read('test/support/vcr/get_organizations.yml')
8
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
9
- JSON.parse(body)['values'].first['id']
10
- end
11
-
12
- let(:client) { JD::Client.new(API_KEY, API_SECRET, environment: :sandbox, access: [ACCESS_TOKEN, ACCESS_SECRET]) }
13
- let(:accessor) { VCR.use_cassette('catalog') { client.send(:accessor) } }
14
6
  let(:collection) { JD::Request::Collection::Fields.new(accessor, organization: organization_id) }
15
7
  let(:object) { collection }
16
8
 
@@ -22,10 +14,10 @@ describe 'MyJohnDeereApi::Request::Collection::Fields' do
22
14
  end
23
15
 
24
16
  it 'accepts associations' do
25
- collection = JD::Request::Collection::Fields.new(accessor, organization: '123')
17
+ collection = JD::Request::Collection::Fields.new(accessor, organization: organization_id)
26
18
 
27
19
  assert_kind_of Hash, collection.associations
28
- assert_equal '123', collection.associations[:organization]
20
+ assert_equal organization_id, collection.associations[:organization]
29
21
  end
30
22
  end
31
23
 
@@ -51,7 +43,7 @@ describe 'MyJohnDeereApi::Request::Collection::Fields' do
51
43
  describe '#count' do
52
44
  let(:server_response) do
53
45
  contents = File.read('test/support/vcr/get_fields.yml')
54
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
46
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
55
47
  JSON.parse(body)
56
48
  end
57
49
 
@@ -67,7 +59,7 @@ describe 'MyJohnDeereApi::Request::Collection::Fields' do
67
59
  describe 'results' do
68
60
  let(:field_names) do
69
61
  contents = File.read('test/support/vcr/get_fields.yml')
70
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
62
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
71
63
  JSON.parse(body)['values'].map{|v| v['name']}
72
64
  end
73
65
 
@@ -3,20 +3,6 @@ require 'yaml'
3
3
  require 'json'
4
4
 
5
5
  describe 'MyJohnDeereApi::Request::Collection::Flags' do
6
- let(:organization_id) do
7
- contents = File.read('test/support/vcr/get_organizations.yml')
8
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
9
- JSON.parse(body)['values'].first['id']
10
- end
11
-
12
- let(:field_id) do
13
- contents = File.read('test/support/vcr/get_fields.yml')
14
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
15
- JSON.parse(body)['values'].first['id']
16
- end
17
-
18
- let(:client) { JD::Client.new(API_KEY, API_SECRET, environment: :sandbox, access: [ACCESS_TOKEN, ACCESS_SECRET]) }
19
- let(:accessor) { VCR.use_cassette('catalog') { client.send(:accessor) } }
20
6
  let(:collection) { JD::Request::Collection::Flags.new(accessor, organization: organization_id, field: field_id) }
21
7
  let(:object) { collection }
22
8
 
@@ -28,11 +14,11 @@ describe 'MyJohnDeereApi::Request::Collection::Flags' do
28
14
  end
29
15
 
30
16
  it 'accepts associations' do
31
- collection = JD::Request::Collection::Flags.new(accessor, organization: '123', field: '456')
17
+ collection = JD::Request::Collection::Flags.new(accessor, organization: organization_id, field: field_id)
32
18
 
33
19
  assert_kind_of Hash, collection.associations
34
- assert_equal '123', collection.associations[:organization]
35
- assert_equal '456', collection.associations[:field]
20
+ assert_equal organization_id, collection.associations[:organization]
21
+ assert_equal field_id, collection.associations[:field]
36
22
  end
37
23
  end
38
24
 
@@ -58,7 +44,7 @@ describe 'MyJohnDeereApi::Request::Collection::Flags' do
58
44
  describe '#count' do
59
45
  let(:server_response) do
60
46
  contents = File.read('test/support/vcr/get_flags.yml')
61
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
47
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
62
48
  JSON.parse(body)
63
49
  end
64
50
 
@@ -74,7 +60,7 @@ describe 'MyJohnDeereApi::Request::Collection::Flags' do
74
60
  describe 'results' do
75
61
  let(:flag_geometries) do
76
62
  contents = File.read('test/support/vcr/get_flags.yml')
77
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
63
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
78
64
  JSON.parse(body)['values'].map{|v| JSON.parse(v['geometry'])}
79
65
  end
80
66
 
@@ -3,8 +3,6 @@ require 'yaml'
3
3
  require 'json'
4
4
 
5
5
  describe 'MyJohnDeereApi::Request::Collection::Organizations' do
6
- let(:client) { JD::Client.new(API_KEY, API_SECRET, environment: :sandbox, access: [ACCESS_TOKEN, ACCESS_SECRET]) }
7
- let(:accessor) { VCR.use_cassette('catalog') { client.send(:accessor) } }
8
6
  let(:collection) { JD::Request::Collection::Organizations.new(accessor) }
9
7
  let(:object) { collection }
10
8
 
@@ -38,7 +36,7 @@ describe 'MyJohnDeereApi::Request::Collection::Organizations' do
38
36
  describe '#count' do
39
37
  let(:server_response) do
40
38
  contents = File.read('test/support/vcr/get_organizations.yml')
41
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
39
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
42
40
  JSON.parse(body)
43
41
  end
44
42
 
@@ -54,7 +52,7 @@ describe 'MyJohnDeereApi::Request::Collection::Organizations' do
54
52
  describe 'pagination' do
55
53
  let(:organization_names) do
56
54
  contents = File.read('test/support/vcr/get_organizations.yml')
57
- body = YAML.load(contents)['http_interactions'].first['response']['body']['string']
55
+ body = YAML.load(contents)['http_interactions'].last['response']['body']['string']
58
56
  JSON.parse(body)['values'].map{|v| v['name']}
59
57
  end
60
58
 
@@ -14,6 +14,10 @@ describe 'MyJohnDeereApi::Request::Collection' do
14
14
  assert JD::Request::Collection::AssetLocations
15
15
  end
16
16
 
17
+ it 'loads Request::Collection::ContributionProducts' do
18
+ assert JD::Request::Collection::ContributionProducts
19
+ end
20
+
17
21
  it 'loads Request::Collection::Organizations' do
18
22
  assert JD::Request::Collection::Organizations
19
23
  end