my_john_deere_api 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/my_john_deere_api/authorize.rb +52 -6
- data/lib/my_john_deere_api/version.rb +1 -1
- data/test/lib/test_authorize.rb +30 -21
- data/test/support/vcr/get_access_token.yml +41 -0
- data/test/support/vcr/get_request_token.yml +12 -12
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9e4802c00252cf2770981858362b93cd8b97791e33107bcb7f5edb75c4466bc
|
4
|
+
data.tar.gz: bb0b07d289995ecd438978c6b5754e9e348e8f2cf7073d865f0e4b19c4afb934
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46140b980eff6e3c8c8299b290219e168c822b4b91a405a6685af3453717e0d93e49e0eb9fba2fc601022279488802d638d5416dc8a406dda7fce2035bb5e275
|
7
|
+
data.tar.gz: ad162f92e55a7012997a15ee91b1c6040af309455e78bf25fcc7114770534f53cceb9fe69d1def94cc6ff0ab053de7c7459efc8776100abac55192ecad5e165a
|
@@ -1,36 +1,84 @@
|
|
1
1
|
class MyJohnDeereApi::Authorize
|
2
|
-
attr_reader :api_key, :api_secret, :request_token, :request_secret
|
2
|
+
attr_reader :api_key, :api_secret, :request_token, :request_secret, :access_token, :access_secret, :environment
|
3
3
|
attr_accessor :base_url
|
4
4
|
|
5
|
+
URLS = {
|
6
|
+
sandbox: 'https://sandboxapi.deere.com',
|
7
|
+
production: 'https://api.soa-proxy.deere.com',
|
8
|
+
}
|
9
|
+
|
5
10
|
DEFAULTS = {
|
6
|
-
|
11
|
+
environment: :production,
|
7
12
|
}
|
8
13
|
|
14
|
+
##
|
15
|
+
# Create an Authorize object using your John Deere API key/secret.
|
16
|
+
#
|
17
|
+
# Options:
|
18
|
+
#
|
19
|
+
# [:environment] *:production* or *:sandbox*, determines the base url
|
20
|
+
# used to make oauth requests.
|
21
|
+
#
|
22
|
+
# [:base_url] set the base url directly, since in the future,
|
23
|
+
# *environment* may affect other things.
|
24
|
+
|
9
25
|
def initialize(api_key, api_secret, options={})
|
26
|
+
options = DEFAULTS.merge(options)
|
27
|
+
|
10
28
|
@api_key = api_key
|
11
29
|
@api_secret = api_secret
|
12
|
-
@
|
30
|
+
@environment = options[:environment]
|
31
|
+
@base_url = options[:base_url] || URLS[@environment]
|
13
32
|
end
|
14
33
|
|
34
|
+
##
|
35
|
+
# Option a url which may be used to obtain a verification
|
36
|
+
# code from the oauth server.
|
37
|
+
|
15
38
|
def authorize_url
|
16
39
|
return @authorize_url if defined?(@authorize_url)
|
17
40
|
|
18
41
|
requester = app_consumer.get_request_token
|
19
|
-
|
20
42
|
@request_token = requester.token
|
21
43
|
@request_secret = requester.secret
|
22
44
|
|
23
45
|
@authorize_url = requester.authorize_url
|
24
46
|
end
|
25
47
|
|
48
|
+
##
|
49
|
+
# Turn a verification code into access tokens. If this is
|
50
|
+
# run from a separate process than the one that created
|
51
|
+
# the initial RequestToken, the request token/secret
|
52
|
+
# can be passed in.
|
53
|
+
|
54
|
+
def verify(code, token=nil, secret=nil)
|
55
|
+
token ||= request_token
|
56
|
+
secret ||= request_secret
|
57
|
+
|
58
|
+
requester = OAuth::RequestToken.new(app_consumer, token, secret)
|
59
|
+
access_object = requester.get_access_token(oauth_verifier: code)
|
60
|
+
@access_token = access_object.token
|
61
|
+
@access_secret = access_object.secret
|
62
|
+
nil
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# oAuth Consumer which uses just the base url, for
|
67
|
+
# app-wide, non user-specific requests.
|
68
|
+
|
26
69
|
def app_consumer
|
27
70
|
@app_consumer ||= consumer(base_url)
|
28
71
|
end
|
29
72
|
|
73
|
+
##
|
74
|
+
# oAuth Consumer which uses the proper url for user-specific requests.
|
75
|
+
|
30
76
|
def user_consumer
|
31
77
|
@user_consumer ||= consumer("#{base_url}/platform")
|
32
78
|
end
|
33
79
|
|
80
|
+
private
|
81
|
+
|
34
82
|
def consumer(site)
|
35
83
|
OAuth::Consumer.new(
|
36
84
|
api_key,
|
@@ -68,8 +116,6 @@ class MyJohnDeereApi::Authorize
|
|
68
116
|
@links
|
69
117
|
end
|
70
118
|
|
71
|
-
private
|
72
|
-
|
73
119
|
def header
|
74
120
|
{accept: 'application/vnd.deere.axiom.v3+json'}
|
75
121
|
end
|
data/test/lib/test_authorize.rb
CHANGED
@@ -2,7 +2,6 @@ require 'uri'
|
|
2
2
|
require 'cgi'
|
3
3
|
require 'support/helper'
|
4
4
|
|
5
|
-
URL_ENV = 'MY_JOHN_DEERE_URL'
|
6
5
|
TOKEN_PATTERN = /^[0-9a-z\-]+$/
|
7
6
|
SECRET_PATTERN = /^[0-9A-Za-z\-+=\/]+$/
|
8
7
|
API_KEY = ENV['API_KEY']
|
@@ -13,7 +12,7 @@ def contains_parameters?(uri)
|
|
13
12
|
end
|
14
13
|
|
15
14
|
def create_authorize
|
16
|
-
JD::Authorize.new(API_KEY, API_SECRET,
|
15
|
+
JD::Authorize.new(API_KEY, API_SECRET, environment: :sandbox)
|
17
16
|
end
|
18
17
|
|
19
18
|
def fancy_url
|
@@ -29,18 +28,24 @@ class AuthorizeTest < MiniTest::Test
|
|
29
28
|
assert_equal 'secret', authorize.api_secret
|
30
29
|
end
|
31
30
|
|
32
|
-
it
|
33
|
-
authorize = JD::Authorize.new('key', 'secret'
|
34
|
-
assert_equal
|
31
|
+
it 'defaults to production environment' do
|
32
|
+
authorize = JD::Authorize.new('key', 'secret')
|
33
|
+
assert_equal :production, authorize.environment
|
35
34
|
end
|
36
35
|
|
37
|
-
it '
|
38
|
-
|
36
|
+
it 'defaults to production oauth url' do
|
37
|
+
authorize = JD::Authorize.new('key', 'secret')
|
38
|
+
assert_equal 'https://api.soa-proxy.deere.com', authorize.base_url
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
41
|
+
it "can set the environment" do
|
42
|
+
authorize = JD::Authorize.new('key', 'secret', environment: :sandbox)
|
43
|
+
assert_equal :sandbox, authorize.environment
|
44
|
+
end
|
42
45
|
|
43
|
-
|
46
|
+
it "can set the base_url via the environment" do
|
47
|
+
authorize = JD::Authorize.new('key', 'secret', environment: :sandbox)
|
48
|
+
assert_equal 'https://sandboxapi.deere.com', authorize.base_url
|
44
49
|
end
|
45
50
|
end
|
46
51
|
|
@@ -50,7 +55,7 @@ class AuthorizeTest < MiniTest::Test
|
|
50
55
|
|
51
56
|
url = VCR.use_cassette('get_request_token') { authorize.authorize_url }
|
52
57
|
|
53
|
-
assert_includes url, "#{authorize.links[:authorize_request_token]}?oauth_token="
|
58
|
+
assert_includes url, "#{authorize.send(:links)[:authorize_request_token]}?oauth_token="
|
54
59
|
|
55
60
|
query = URI.parse(url).query
|
56
61
|
params = CGI::parse(query)
|
@@ -68,11 +73,24 @@ class AuthorizeTest < MiniTest::Test
|
|
68
73
|
end
|
69
74
|
end
|
70
75
|
|
76
|
+
describe '#verify(code, token, secret)' do
|
77
|
+
it 'sets the access token/secret' do
|
78
|
+
authorize = create_authorize
|
79
|
+
code = 'VERIFY'
|
80
|
+
|
81
|
+
VCR.use_cassette('get_request_token') { authorize.authorize_url }
|
82
|
+
VCR.use_cassette('get_access_token') { authorize.verify(code) }
|
83
|
+
|
84
|
+
assert_match TOKEN_PATTERN, authorize.access_token
|
85
|
+
assert_match SECRET_PATTERN, authorize.access_secret
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
71
89
|
describe '#links' do
|
72
90
|
it "returns a list of catalog urls" do
|
73
91
|
authorize = create_authorize
|
74
92
|
|
75
|
-
links = VCR.use_cassette("catalog"){ authorize.links }
|
93
|
+
links = VCR.use_cassette("catalog"){ authorize.send(:links) }
|
76
94
|
|
77
95
|
assert_kind_of Hash, links
|
78
96
|
|
@@ -95,15 +113,6 @@ class AuthorizeTest < MiniTest::Test
|
|
95
113
|
|
96
114
|
assert_equal fancy_url, authorize.base_url
|
97
115
|
end
|
98
|
-
|
99
|
-
it 'can be set via environment variable' do
|
100
|
-
ENV[URL_ENV] = fancy_url
|
101
|
-
|
102
|
-
authorize = JD::Authorize.new('key', 'secret')
|
103
|
-
assert_equal fancy_url, authorize.base_url
|
104
|
-
|
105
|
-
ENV.delete(URL_ENV)
|
106
|
-
end
|
107
116
|
end
|
108
117
|
|
109
118
|
describe '#app_consumer' do
|
@@ -0,0 +1,41 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://sandboxapi.deere.com/platform/oauth/access_token
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept-Encoding:
|
11
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
12
|
+
Accept:
|
13
|
+
- "*/*"
|
14
|
+
User-Agent:
|
15
|
+
- OAuth gem v0.5.4
|
16
|
+
Authorization:
|
17
|
+
- OAuth oauth_consumer_key="johndeere-wgmADngYCRmfpEbVgSyc709wnyRux5J7PAv8SE7B",
|
18
|
+
oauth_nonce="MLB5WFGhx3XqZSEqSLLMqN0rmZzVrpIcJifmmkHY", oauth_signature="EQuzjWYlzL2BdLApWuDmSIGWsuU%3D",
|
19
|
+
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1578342382", oauth_token="e9d13b08-0c49-412c-8d65-b705b2a6172a",
|
20
|
+
oauth_verifier="VERIFY", oauth_version="1.0"
|
21
|
+
response:
|
22
|
+
status:
|
23
|
+
code: 200
|
24
|
+
message: OK
|
25
|
+
headers:
|
26
|
+
Date:
|
27
|
+
- Mon, 06 Jan 2020 20:26:22 GMT
|
28
|
+
Content-Type:
|
29
|
+
- text/plain;charset=UTF-8
|
30
|
+
X-Deere-Handling-Server:
|
31
|
+
- ip-10-214-44-17
|
32
|
+
Vary:
|
33
|
+
- Accept-Encoding
|
34
|
+
Transfer-Encoding:
|
35
|
+
- chunked
|
36
|
+
body:
|
37
|
+
encoding: ASCII-8BIT
|
38
|
+
string: oauth_token=47bbb9c9-41a8-4bec-8127-e3c5760af2f6&oauth_token_secret=ih3PKeoCJFbAeHcEpxn6l8cTAaQJSVzzRtemOvekXLGOsQCLUOifPOyQqZSrcYpiBXI3eJ2r0JUaCv79vsxTe1ngvrllBPjveSLg4%2Bsajmk%3D
|
39
|
+
http_version:
|
40
|
+
recorded_at: Mon, 06 Jan 2020 20:26:22 GMT
|
41
|
+
recorded_with: VCR 5.0.0
|
@@ -15,23 +15,23 @@ http_interactions:
|
|
15
15
|
- OAuth gem v0.5.4
|
16
16
|
Authorization:
|
17
17
|
- OAuth oauth_consumer_key="johndeere-wgmADngYCRmfpEbVgSyc709wnyRux5J7PAv8SE7B",
|
18
|
-
oauth_nonce="
|
19
|
-
oauth_signature_method="HMAC-SHA1", oauth_timestamp="
|
18
|
+
oauth_nonce="33cQ0E7MKtFZJ8sv9mSSQ9Yvmg1YaEV9fSbrZ2qLns", oauth_signature="0GPAESRHRjGfHXaZQ%2BYrOjn3%2BMc%3D",
|
19
|
+
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1578342367", oauth_version="1.0"
|
20
20
|
response:
|
21
21
|
status:
|
22
22
|
code: 200
|
23
23
|
message: OK
|
24
24
|
headers:
|
25
25
|
Date:
|
26
|
-
-
|
26
|
+
- Mon, 06 Jan 2020 20:26:07 GMT
|
27
27
|
Content-Type:
|
28
28
|
- application/vnd.deere.axiom.v3+json;charset=UTF-8
|
29
29
|
X-Deere-Handling-Server:
|
30
|
-
- ip-10-214-
|
30
|
+
- ip-10-214-44-57
|
31
31
|
X-Frame-Options:
|
32
32
|
- SAMEORIGIN
|
33
33
|
X-Deere-Elapsed-Ms:
|
34
|
-
- '
|
34
|
+
- '17'
|
35
35
|
Cache-Control:
|
36
36
|
- no-store
|
37
37
|
Content-Language:
|
@@ -42,7 +42,7 @@ http_interactions:
|
|
42
42
|
encoding: ASCII-8BIT
|
43
43
|
string: '{"@type":"ApiCatalog","links":[{"@type":"Link","rel":"oauthRequestToken","uri":"https://sandboxapi.deere.com/platform/oauth/request_token"},{"@type":"Link","rel":"oauthAuthorizeRequestToken","uri":"https://my.deere.com/consentToUseOfData?oauth_token={token}"},{"@type":"Link","rel":"oauthAccessToken","uri":"https://sandboxapi.deere.com/platform/oauth/access_token"},{"@type":"Link","rel":"agencies","uri":"https://sandboxapi.deere.com/platform/agencies"}]}'
|
44
44
|
http_version:
|
45
|
-
recorded_at:
|
45
|
+
recorded_at: Mon, 06 Jan 2020 20:26:08 GMT
|
46
46
|
- request:
|
47
47
|
method: get
|
48
48
|
uri: https://sandboxapi.deere.com/platform/oauth/request_token
|
@@ -58,26 +58,26 @@ http_interactions:
|
|
58
58
|
- OAuth gem v0.5.4
|
59
59
|
Authorization:
|
60
60
|
- OAuth oauth_callback="oob", oauth_consumer_key="johndeere-wgmADngYCRmfpEbVgSyc709wnyRux5J7PAv8SE7B",
|
61
|
-
oauth_nonce="
|
62
|
-
oauth_signature_method="HMAC-SHA1", oauth_timestamp="
|
61
|
+
oauth_nonce="U5e97z9K7RNHoENuAOeS5f2xK2m4znRsPh8Ynh2hQ", oauth_signature="eqQGoXU45sfdoiq%2BghH8xMjv8Ek%3D",
|
62
|
+
oauth_signature_method="HMAC-SHA1", oauth_timestamp="1578342368", oauth_version="1.0"
|
63
63
|
response:
|
64
64
|
status:
|
65
65
|
code: 200
|
66
66
|
message: OK
|
67
67
|
headers:
|
68
68
|
Date:
|
69
|
-
-
|
69
|
+
- Mon, 06 Jan 2020 20:26:08 GMT
|
70
70
|
Content-Type:
|
71
71
|
- text/plain;charset=UTF-8
|
72
72
|
X-Deere-Handling-Server:
|
73
|
-
- ip-10-214-
|
73
|
+
- ip-10-214-44-190
|
74
74
|
Vary:
|
75
75
|
- Accept-Encoding
|
76
76
|
Transfer-Encoding:
|
77
77
|
- chunked
|
78
78
|
body:
|
79
79
|
encoding: ASCII-8BIT
|
80
|
-
string: oauth_token=
|
80
|
+
string: oauth_token=e9d13b08-0c49-412c-8d65-b705b2a6172a&oauth_token_secret=IWI0xnmlsYi1bqgqXvrVlfWmC0d4teqtjs7W0fZT6ofObcOLcrLDxDnVE2gBYZUepY%2BPE6r97Li8bgbNWYauVMnnDMenFW93Twb%2B%2FVpaUjQ%3D&oauth_callback_confirmed=true
|
81
81
|
http_version:
|
82
|
-
recorded_at:
|
82
|
+
recorded_at: Mon, 06 Jan 2020 20:26:08 GMT
|
83
83
|
recorded_with: VCR 5.0.0
|
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: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jaime. Bellmyer
|
@@ -103,6 +103,7 @@ files:
|
|
103
103
|
- test/support/helper.rb
|
104
104
|
- test/support/vcr/app_consumer.yml
|
105
105
|
- test/support/vcr/catalog.yml
|
106
|
+
- test/support/vcr/get_access_token.yml
|
106
107
|
- test/support/vcr/get_request_token.yml
|
107
108
|
- test/test_my_john_deere_api.rb
|
108
109
|
homepage: https://github.com/Intellifarm/my_john_deere_api
|