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,5 @@
1
+ module MyJohnDeereApi::Validators
2
+ autoload :Base, 'my_john_deere_api/validators/base'
3
+ autoload :Asset, 'my_john_deere_api/validators/asset'
4
+ autoload :AssetLocation, 'my_john_deere_api/validators/asset_location'
5
+ 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, :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,34 @@
1
+ module MyJohnDeereApi::Validators
2
+ module AssetLocation
3
+ include Base
4
+
5
+ private
6
+
7
+ def required_attributes
8
+ [:asset_id, :timestamp, :geometry, :measurement_data]
9
+ end
10
+
11
+ ##
12
+ # Custom validations for this class
13
+
14
+ def validate_attributes
15
+ validate_measurement_data
16
+ end
17
+
18
+ def validate_measurement_data
19
+ unless attributes[:measurement_data].is_a?(Array)
20
+ errors[:measurement_data] ||= 'must be an array'
21
+ return
22
+ end
23
+
24
+ attributes[:measurement_data].each do |measurement|
25
+ [:name, :value, :unit].each do |attr|
26
+ unless measurement.has_key?(attr)
27
+ errors[:measurement_data] ||= "must include #{attr}"
28
+ return
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ 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
@@ -0,0 +1,3 @@
1
+ module MyJohnDeereApi
2
+ VERSION='2.4.1'
3
+ end
@@ -0,0 +1,81 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+ require 'support/helper'
4
+
5
+ def contains_parameters?(uri)
6
+ !URI.parse(uri).query.nil?
7
+ end
8
+
9
+ def create_authorize
10
+ VCR.use_cassette('catalog'){ JD::Authorize.new(api_key, api_secret, environment: :sandbox) }
11
+ end
12
+
13
+ def fancy_url
14
+ 'https://example.com/turtles'
15
+ end
16
+
17
+ describe 'MyJohnDeereApi::Authorize' do
18
+ describe 'initialization' do
19
+ it 'sets the api key/secret' do
20
+ authorize = VCR.use_cassette('catalog') { JD::Authorize.new(api_key, api_secret) }
21
+
22
+ assert_equal api_key, authorize.api_key
23
+ assert_equal api_secret, authorize.api_secret
24
+ end
25
+
26
+ it 'accepts the environment' do
27
+ environment = :sandbox
28
+
29
+ authorize = VCR.use_cassette('catalog') { JD::Authorize.new(api_key, api_secret, environment: environment) }
30
+ assert_equal environment, authorize.environment
31
+ end
32
+ end
33
+
34
+ describe '#consumer' do
35
+ it "returns a non-user-specific consumer configured for GET requests" do
36
+ authorize = create_authorize
37
+ consumer = VCR.use_cassette('catalog') { authorize.consumer }
38
+
39
+ assert_kind_of OAuth::Consumer, consumer
40
+ assert_equal :get, consumer.http_method
41
+ end
42
+ end
43
+
44
+ describe '#authorize_url' do
45
+ it 'retrieves a request url' do
46
+ authorize = create_authorize
47
+
48
+ url = VCR.use_cassette('get_request_token') { authorize.authorize_url }
49
+ links = VCR.use_cassette('catalog') { JD::Consumer.new(api_key, api_secret, environment: :sandbox).send(:links) }
50
+
51
+ assert_includes url, links[:authorize_request_token]
52
+
53
+ query = URI.parse(url).query
54
+ params = CGI::parse(query)
55
+
56
+ assert_match(TOKEN_PATTERN, params['oauth_token'].first)
57
+ end
58
+
59
+ it 'sets the request token/secret' do
60
+ authorize = create_authorize
61
+
62
+ VCR.use_cassette('get_request_token') { authorize.authorize_url }
63
+
64
+ assert_match TOKEN_PATTERN, authorize.request_token
65
+ assert_match SECRET_PATTERN, authorize.request_secret
66
+ end
67
+ end
68
+
69
+ describe '#verify(code, token, secret)' do
70
+ it 'sets the access token/secret' do
71
+ authorize = create_authorize
72
+ code = 'VERIFY'
73
+
74
+ VCR.use_cassette('get_request_token') { authorize.authorize_url }
75
+ VCR.use_cassette('get_access_token') { authorize.verify(code) }
76
+
77
+ assert_match TOKEN_PATTERN, authorize.access_token
78
+ assert_match SECRET_PATTERN, authorize.access_secret
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,225 @@
1
+ require 'support/helper'
2
+
3
+ describe 'MyJohnDeereApi::Client' do
4
+ it 'includes the CaseConversion helper' do
5
+ client = JD::Client.new(api_key, api_secret)
6
+ assert_equal 'thisIsATest', client.send(:camelize, :this_is_a_test)
7
+ end
8
+
9
+ describe '#initialize(api_key, api_secret, options={})' do
10
+ it 'sets the api key/secret' do
11
+ client = JD::Client.new(api_key, api_secret)
12
+
13
+ assert_equal api_key, client.api_key
14
+ assert_equal api_secret, client.api_secret
15
+ end
16
+
17
+ it 'accepts access token/secret' do
18
+ access_token = 'token'
19
+ access_secret = 'secret'
20
+
21
+ client = JD::Client.new(api_key, api_secret, access: [access_token, access_secret])
22
+
23
+ assert_equal access_token, client.access_token
24
+ assert_equal access_secret, client.access_secret
25
+ end
26
+
27
+ it 'accepts the environment' do
28
+ environment = :sandbox
29
+
30
+ client = JD::Client.new(api_key, api_secret, environment: environment)
31
+ assert_equal environment, client.environment
32
+ end
33
+
34
+ it 'accepts a contribution_definition_id' do
35
+ client = JD::Client.new(api_key, api_secret, contribution_definition_id: contribution_definition_id)
36
+ assert_equal contribution_definition_id, client.contribution_definition_id
37
+ end
38
+
39
+ it 'accepts a list of parameters for NetHttpRetry' do
40
+ custom_retries = JD::NetHttpRetry::Decorator::DEFAULTS[:max_retries] + 10
41
+
42
+ VCR.use_cassette('catalog') do
43
+ new_client = JD::Client.new(
44
+ api_key,
45
+ api_secret,
46
+ contribution_definition_id: contribution_definition_id,
47
+ environment: :sandbox,
48
+ access: [access_token, access_secret],
49
+ http_retry: {max_retries: custom_retries}
50
+ )
51
+
52
+ assert_equal custom_retries, new_client.accessor.max_retries
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '#contribution_definition_id' do
58
+ it 'can be set after instantiation' do
59
+ client = JD::Client.new(api_key, api_secret)
60
+ assert_nil client.contribution_definition_id
61
+
62
+ client.contribution_definition_id = '123'
63
+ assert_equal '123', client.contribution_definition_id
64
+ end
65
+ end
66
+
67
+ describe '#get' do
68
+ it 'returns the response as a Hash' do
69
+ response = VCR.use_cassette('get_organizations') { client.get('/organizations') }
70
+
71
+ assert_kind_of Hash, response
72
+ assert_kind_of Integer, response['total']
73
+ assert response['values'].all?{|value| value['@type'] == 'Organization'}
74
+ assert response['values'].all?{|value| value.has_key?('links')}
75
+ end
76
+
77
+ it 'prepends the leading slash if needed' do
78
+ response = VCR.use_cassette('get_organizations') { client.get('organizations') }
79
+
80
+ assert_kind_of Hash, response
81
+ assert_kind_of Integer, response['total']
82
+ assert response['values'].all?{|value| value['@type'] == 'Organization'}
83
+ assert response['values'].all?{|value| value.has_key?('links')}
84
+ end
85
+
86
+ it 'allows symbols for simple resources' do
87
+ response = VCR.use_cassette('get_organizations') { client.get(:organizations) }
88
+
89
+ assert_kind_of Hash, response
90
+ assert_kind_of Integer, response['total']
91
+ assert response['values'].all?{|value| value['@type'] == 'Organization'}
92
+ assert response['values'].all?{|value| value.has_key?('links')}
93
+ end
94
+ end
95
+
96
+ describe '#post' do
97
+ let(:attributes) do
98
+ CONFIG.asset_attributes.merge(
99
+ links: [
100
+ {
101
+ '@type' => 'Link',
102
+ 'rel' => 'contributionDefinition',
103
+ 'uri' => "#{CONFIG.url}/contributionDefinitions/#{contribution_definition_id}"
104
+ }
105
+ ]
106
+ )
107
+ end
108
+
109
+ it 'returns the response as a Hash' do
110
+ response = VCR.use_cassette('post_assets') do
111
+ client.post("/organizations/#{organization_id}/assets", attributes)
112
+ end
113
+
114
+ assert_equal '201', response.code
115
+ assert_equal 'Created', response.message
116
+ assert_equal "#{base_url}/assets/#{asset_id}", response['Location']
117
+ end
118
+
119
+ it 'prepends the leading slash if needed' do
120
+ response = VCR.use_cassette('post_assets') do
121
+ client.post("organizations/#{organization_id}/assets", attributes)
122
+ end
123
+
124
+ assert_equal '201', response.code
125
+ assert_equal 'Created', response.message
126
+ assert_equal "#{base_url}/assets/#{asset_id}", response['Location']
127
+ end
128
+ end
129
+
130
+ describe '#put' do
131
+ let(:new_title) { 'i REALLY like turtles!' }
132
+
133
+ let(:attributes) do
134
+ CONFIG.asset_attributes.slice(
135
+ :asset_category, :asset_type, :asset_sub_type, :links
136
+ ).merge(
137
+ title: new_title,
138
+ links: [
139
+ {
140
+ '@type' => 'Link',
141
+ 'rel' => 'contributionDefinition',
142
+ 'uri' => "#{CONFIG.url}/contributionDefinitions/#{contribution_definition_id}"
143
+ }
144
+ ]
145
+ )
146
+ end
147
+
148
+ it 'sends the request' do
149
+ response = VCR.use_cassette('put_asset') { client.put("/assets/#{asset_id}", attributes) }
150
+
151
+ assert_equal '204', response.code
152
+ assert_equal 'No Content', response.message
153
+ end
154
+
155
+ it 'prepends the leading slash if needed' do
156
+ response = VCR.use_cassette('put_asset') { client.put("assets/#{asset_id}", attributes) }
157
+
158
+ assert_equal '204', response.code
159
+ assert_equal 'No Content', response.message
160
+ end
161
+ end
162
+
163
+ describe '#delete' do
164
+ it 'sends the request' do
165
+ response = VCR.use_cassette('delete_asset') { client.delete("/assets/#{asset_id}") }
166
+
167
+ assert_equal '204', response.code
168
+ assert_equal 'No Content', response.message
169
+ end
170
+
171
+ it 'prepends the leading slash if needed' do
172
+ response = VCR.use_cassette('delete_asset') { client.delete("assets/#{asset_id}") }
173
+
174
+ assert_equal '204', response.code
175
+ assert_equal 'No Content', response.message
176
+ end
177
+ end
178
+
179
+ describe '#organizations' do
180
+ it 'returns a collection of organizations for this account' do
181
+ organizations = VCR.use_cassette('get_organizations') { client.organizations.all; client.organizations }
182
+
183
+ assert_kind_of JD::Request::Collection::Organizations, organizations
184
+
185
+ organizations.each do |organization|
186
+ assert_kind_of JD::Model::Organization, organization
187
+ end
188
+ end
189
+ end
190
+
191
+ describe '#contribution_products' do
192
+ it 'returns a collection of contribution products for this account' do
193
+ contribution_products = VCR.use_cassette('get_contribution_products') { client.contribution_products.all; client.contribution_products }
194
+
195
+ assert_kind_of JD::Request::Collection::ContributionProducts, contribution_products
196
+
197
+ contribution_products.each do |contribution_product|
198
+ assert_kind_of JD::Model::ContributionProduct, contribution_product
199
+ end
200
+ end
201
+ end
202
+
203
+ describe '#consumer' do
204
+ it 'receives the api key/secret and environment of the client' do
205
+ environment = :sandbox
206
+
207
+ client = JD::Client.new(api_key, api_secret, environment: environment)
208
+ consumer = client.send :consumer
209
+
210
+ assert_kind_of JD::Consumer, consumer
211
+ assert_equal api_key, consumer.api_key
212
+ assert_equal api_secret, consumer.api_secret
213
+ assert_equal environment, consumer.environment
214
+ end
215
+ end
216
+
217
+ describe '#accessor' do
218
+ it 'returns an object that can make user-specific requests' do
219
+ assert_kind_of JD::NetHttpRetry::Decorator, accessor
220
+ assert_kind_of OAuth::Consumer, accessor.consumer
221
+ assert_equal access_token, accessor.token
222
+ assert_equal access_secret, accessor.secret
223
+ end
224
+ end
225
+ end