my_john_deere_api 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b36f05ba98236dbdddfcab7086fa13c6af8d673dc8c2d2c1f185abcbf0f1ceeb
4
- data.tar.gz: d5bda4fae253a293fd58b44045eacf2b7eba8dd142560aa3813da3deb334bc7c
3
+ metadata.gz: ba2b1a3274a3f5ae20bdfeaf13a770db0aa6b5b5cf9a0d87b0894e99a17d2423
4
+ data.tar.gz: 806bb77756d614db71ebdc06d3ffd68e7ea1a4afc5dac0612444501acafafc22
5
5
  SHA512:
6
- metadata.gz: 454ac16787a9448ce1fb0d5db1bc4f2b39b9c0c729552fe9c0730093d681dc442fc33b21cdbb8aea79678a1a2e1fd1bdab2ff042b931251818435cbcbd7a54e9
7
- data.tar.gz: b90f4aa488daa2e0c99c31ede5045769f6d7cd04fce875d92375b369318845737fc5dbf9d9e4824b048980261e6ccc1880e8b4803a01cd9f0be4299d826cd491
6
+ metadata.gz: cba9de4b20f90959a9c9c3103515345cad97346fd1515d8d2d6e1aaa5e44e463e9d991724b52d9b7f58f5cee27960e512c0def8715eea00782a5cbaaeb47ef5f
7
+ data.tar.gz: 81273181c21ecd1ceba98f265f5d7c22f9050e96e036e2899fe1ccaa66a6855ccb1bf190c454920890daca38872359753c828f80f8431b0ee5a8e7b9bdbcc66f
@@ -13,6 +13,25 @@ module MyJohnDeereApi
13
13
  #
14
14
  # This is used to obtain authentication an access key/secret
15
15
  # on behalf of a user.
16
+ #
17
+ # parameters:
18
+ #
19
+ # api_key - required JD API key for client application
20
+ # api_secret - required JD API secret for client application
21
+ # options - options hash, see below for details
22
+ #
23
+ # options:
24
+ #
25
+ # :environment - one of [:live, :staging]
26
+ # :scope - a space-delimited list of scopes requested
27
+ # :scopes - an array of scopes requested. 'offline_access' will be added if
28
+ # it isn't included already. This will overwrite :scope option if
29
+ # both are provided.
30
+ # :state - a unique identifier for this request. JD will return this when
31
+ # it redirects back after authorization, so that the client can identify
32
+ # which of its users this request belongs to.
33
+ # :redirect_uri - the uri for JD to redirect back to, after authorization.
34
+
16
35
 
17
36
  def initialize(api_key, api_secret, options = {})
18
37
  @options = DEFAULTS.merge(options)
@@ -62,7 +81,7 @@ module MyJohnDeereApi
62
81
  token = oauth_client.auth_code.get_token(code, redirect_uri: options[:redirect_uri])
63
82
 
64
83
  # normalize hash
65
- @token_hash = JSON.parse(token.to_hash.to_json)
84
+ @token_hash = token.to_hash.transform_keys(&:to_s)
66
85
  end
67
86
 
68
87
  ##
@@ -7,12 +7,13 @@ module MyJohnDeereApi::Helpers::CaseConversion
7
7
  # convert a text or camelcase string to underscore
8
8
 
9
9
  def underscore(something)
10
- something = something.to_s if something.is_a?(Symbol)
11
-
12
- if something.is_a?(String)
13
- something.gsub(/([a-z])([A-Z])/, '\1_\2').gsub(/\s+/, '_').gsub(/_+/, '_').downcase
14
- elsif something.is_a?(Hash)
10
+ case something
11
+ when Symbol, String
12
+ something.to_s.gsub(/([a-z])([A-Z])/, '\1_\2').gsub(/\s+/, '_').gsub(/_+/, '_').downcase
13
+ when Hash
15
14
  something.transform_keys{ |key| underscore(key) }
15
+ when Array
16
+ something.map{|element| underscore(element)}
16
17
  end
17
18
  end
18
19
 
@@ -20,11 +21,9 @@ module MyJohnDeereApi::Helpers::CaseConversion
20
21
  # convert text or underscored string to camelcase
21
22
 
22
23
  def camelize(something)
23
- something = something.to_s if something.is_a?(Symbol)
24
-
25
24
  case something
26
- when String
27
- list = something.strip.split(/[_\s]+/)
25
+ when Symbol, String
26
+ list = something.to_s.strip.split(/[_\s]+/)
28
27
 
29
28
  # preserve case of the first element
30
29
  new_list = [list.shift]
@@ -3,7 +3,7 @@ module MyJohnDeereApi
3
3
  include Helpers::CaseConversion
4
4
  include Helpers::UriHelpers
5
5
 
6
- attr_reader :id, :record_type, :client, :links
6
+ attr_reader :id, :record, :record_type, :client, :links
7
7
 
8
8
  ##
9
9
  # arguments:
@@ -19,6 +19,7 @@ module MyJohnDeereApi
19
19
  verify_record_type(record['@type'])
20
20
 
21
21
  @id = record['id']
22
+ @record = record
22
23
  @record_type = record['@type']
23
24
  @client = client
24
25
  @unsaved = false
@@ -28,6 +28,20 @@ module MyJohnDeereApi
28
28
  @assets = MyJohnDeereApi::Request::Collection::Assets.new(client, organization: id)
29
29
  end
30
30
 
31
+ ##
32
+ # whether this organization still needs to be approved in JD "connections"
33
+
34
+ def needs_connection?
35
+ links.key?('connections')
36
+ end
37
+
38
+ ##
39
+ # the URI for JD connections page, if available
40
+
41
+ def connections_uri
42
+ record['links'].detect{|link| link['rel'] == 'connections'}&.fetch('uri')
43
+ end
44
+
31
45
  private
32
46
 
33
47
  def map_attributes(record)
@@ -40,4 +54,4 @@ module MyJohnDeereApi
40
54
  'Organization'
41
55
  end
42
56
  end
43
- end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module MyJohnDeereApi
2
- VERSION='3.0.0'
2
+ VERSION='3.1.0'
3
3
  end
@@ -85,7 +85,7 @@ describe 'MyJohnDeereApi::Authorize' do
85
85
  new_hash = VCR.use_cassette('get_refresh_token') { subject }
86
86
 
87
87
  # normalize response hash
88
- new_hash = JSON.parse(new_hash.to_json)
88
+ new_hash = new_hash.transform_keys(&:to_s)
89
89
 
90
90
  assert_equal new_access_token, new_hash['access_token']
91
91
  end
@@ -34,6 +34,15 @@ describe 'Helpers::CaseConversion' do
34
34
  assert_equal new_hash['asset_type'], hash[:assetType]
35
35
  end
36
36
 
37
+ it 'converts the elements of an array' do
38
+ array = ['assetCategory', 'assetType']
39
+
40
+ new_array = object.send(:underscore, array)
41
+
42
+ assert_includes new_array, 'asset_category'
43
+ assert_includes new_array, 'asset_type'
44
+ end
45
+
37
46
  it 'handles leading uppercase' do
38
47
  string = object.send(:underscore, 'CamelCaseExample')
39
48
  assert_equal 'camel_case_example', string
@@ -88,6 +97,15 @@ describe 'Helpers::CaseConversion' do
88
97
  assert_equal new_hash['assetType'], hash[:asset_type]
89
98
  end
90
99
 
100
+ it 'converts the elements of an array' do
101
+ array = ['asset_category', 'asset_type']
102
+
103
+ new_array = object.send(:camelize, array)
104
+
105
+ assert_includes new_array, 'assetCategory'
106
+ assert_includes new_array, 'assetType'
107
+ end
108
+
91
109
  it 'is a private method' do
92
110
  exception = assert_raises(NoMethodError) { object.camelize('test') }
93
111
  assert_includes exception.message, 'private method'
@@ -4,6 +4,7 @@ describe 'MyJohnDeereApi::Model::Organization' do
4
4
  include JD::LinkHelpers
5
5
 
6
6
  let(:klass) { JD::Model::Organization }
7
+ let(:object) { klass.new(client, record) }
7
8
 
8
9
  let(:record) do
9
10
  {
@@ -12,44 +13,54 @@ describe 'MyJohnDeereApi::Model::Organization' do
12
13
  "type"=>"customer",
13
14
  "member"=>true,
14
15
  "id"=>"123456",
15
- "links"=>[
16
- {"@type"=>"Link", "rel"=>"self", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}"},
17
- {"@type"=>"Link", "rel"=>"machines", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/machines"},
18
- {"@type"=>"Link", "rel"=>"wdtCapableMachines", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/machines?capability=wdt"},
19
- {"@type"=>"Link", "rel"=>"files", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/files"},
20
- {"@type"=>"Link", "rel"=>"transferableFiles", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/files?transferable=true"},
21
- {"@type"=>"Link", "rel"=>"uploadFile", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/files"},
22
- {"@type"=>"Link", "rel"=>"sendFileToMachine", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fileTransfers"},
23
- {"@type"=>"Link", "rel"=>"addMachine", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/machines"},
24
- {"@type"=>"Link", "rel"=>"addField", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields"},
25
- {"@type"=>"Link", "rel"=>"assets", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/assets"},
26
- {"@type"=>"Link", "rel"=>"fields", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields"},
27
- {"@type"=>"Link", "rel"=>"farms", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/farms"},
28
- {"@type"=>"Link", "rel"=>"boundaries", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/boundaries"},
29
- {"@type"=>"Link", "rel"=>"clients", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/clients"},
30
- {"@type"=>"Link", "rel"=>"controllers", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/orgController"}
31
- ]
16
+ "links"=> links,
17
+ }
18
+ end
19
+
20
+ let(:links) do
21
+ [
22
+ {"@type"=>"Link", "rel"=>"self", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}"},
23
+ {"@type"=>"Link", "rel"=>"machines", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/machines"},
24
+ {"@type"=>"Link", "rel"=>"wdtCapableMachines", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/machines?capability=wdt"},
25
+ {"@type"=>"Link", "rel"=>"files", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/files"},
26
+ {"@type"=>"Link", "rel"=>"transferableFiles", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/files?transferable=true"},
27
+ {"@type"=>"Link", "rel"=>"uploadFile", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/files"},
28
+ {"@type"=>"Link", "rel"=>"sendFileToMachine", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fileTransfers"},
29
+ {"@type"=>"Link", "rel"=>"addMachine", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/machines"},
30
+ {"@type"=>"Link", "rel"=>"addField", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields"},
31
+ {"@type"=>"Link", "rel"=>"assets", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/assets"},
32
+ {"@type"=>"Link", "rel"=>"fields", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/fields"},
33
+ {"@type"=>"Link", "rel"=>"farms", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/farms"},
34
+ {"@type"=>"Link", "rel"=>"boundaries", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/boundaries"},
35
+ {"@type"=>"Link", "rel"=>"clients", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/clients"},
36
+ {"@type"=>"Link", "rel"=>"controllers", "uri"=>"https://sandboxapi.deere.com/platform/organizations/#{organization_id}/orgController"}
37
+ ]
38
+ end
39
+
40
+ let(:connections_link) do
41
+ {
42
+ "@type"=>"Link",
43
+ "rel"=>"connections",
44
+ "uri"=>"https://connections.deere.com/connections/johndeere-0000000000000000000000000000000000000000/organizations"
32
45
  }
33
46
  end
34
47
 
35
48
  describe '#initialize(record, client = nil)' do
36
49
  it 'sets the attributes from the given record' do
37
- organization = klass.new(client, record)
38
-
39
- assert_equal client, organization.client
40
- assert_equal accessor, organization.accessor
50
+ assert_equal client, object.client
51
+ assert_equal accessor, object.accessor
41
52
 
42
53
  # basic attributes
43
- assert_equal record['name'], organization.name
44
- assert_equal record['type'], organization.type
45
- assert_equal record['member'], organization.member?
46
- assert_equal record['id'], organization.id
54
+ assert_equal record['name'], object.name
55
+ assert_equal record['type'], object.type
56
+ assert_equal record['member'], object.member?
57
+ assert_equal record['id'], object.id
47
58
 
48
59
  # links to other things
49
- assert_kind_of Hash, organization.links
60
+ assert_kind_of Hash, object.links
50
61
 
51
62
  ['fields', 'machines', 'files', 'assets', 'farms', 'boundaries', 'clients', 'controllers'].each do |association|
52
- assert_link_for(organization, association)
63
+ assert_link_for(object, association)
53
64
  end
54
65
  end
55
66
  end
@@ -79,4 +90,40 @@ describe 'MyJohnDeereApi::Model::Organization' do
79
90
  end
80
91
  end
81
92
  end
93
+
94
+ describe '#needs_connection?' do
95
+ subject { object.needs_connection? }
96
+
97
+ describe 'when user needs to connect organization within JD platform' do
98
+ let(:links) { [connections_link] }
99
+
100
+ it 'returns true' do
101
+ assert subject
102
+ end
103
+ end
104
+
105
+ describe "when user doesn't need to connect org" do
106
+ it 'returns false' do
107
+ refute subject
108
+ end
109
+ end
110
+ end
111
+
112
+ describe '#connections_uri' do
113
+ subject { object.connections_uri }
114
+
115
+ describe 'when user needs to connect organization within JD platform' do
116
+ let(:links) { [connections_link] }
117
+
118
+ it 'returns the URI for JD connections' do
119
+ assert_includes subject, 'https://connections.deere.com/connections'
120
+ end
121
+ end
122
+
123
+ describe "when user doesn't need to connect org" do
124
+ it 'returns nil' do
125
+ assert_nil subject
126
+ end
127
+ end
128
+ end
82
129
  end
@@ -170,13 +170,23 @@ class VcrSetup
170
170
  def token_hash
171
171
  return @token_hash if defined?(@token_hash)
172
172
 
173
- @token_hash = JSON.parse(File.read(token_file))
173
+ if File.exist?(token_file)
174
+ @token_hash = JSON.parse(File.read(token_file))
174
175
 
175
- token = OAuth2::AccessToken.from_hash(auth_client, @token_hash)
176
+ token = OAuth2::AccessToken.from_hash(auth_client, @token_hash)
176
177
 
177
- if token.expired?
178
- new_token = token.refresh!
179
- set_token_hash(new_token)
178
+ if token.expired?
179
+ new_token = token.refresh!
180
+ set_token_hash(new_token)
181
+ end
182
+ else
183
+ @token_hash = {
184
+ "token_type": "Bearer",
185
+ "scope": "ag2 ag1 offline_access ag3",
186
+ "access_token": access_token,
187
+ "refresh_token": refresh_token,
188
+ "expires_at": 1616484631
189
+ }
180
190
  end
181
191
 
182
192
  @token_hash
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: my_john_deere_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime Bellmyer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-03-22 00:00:00.000000000 Z
12
+ date: 2021-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: vcr