myjohndeere 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/myjohndeere/list_object.rb +10 -38
- data/lib/myjohndeere/rest_methods.rb +1 -0
- data/lib/myjohndeere/version.rb +1 -1
- data/test/test_list_object.rb +17 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5764e00147e38d5c7c39529bab16636c2c21e18
|
4
|
+
data.tar.gz: 41edd73dbc9702062cfa032d1d0d0fe0297a022d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 013bb1c5a4f0bec6c514c7e36f8da925d67c37b1baab996af584c8c7472d30fc2bc74dbab315aec191cb5fa45ace6005c2972f36dac1825886c051a46dfcd4c7
|
7
|
+
data.tar.gz: d388153e644e20bd0bff01a04f566aaf68a303c1c128ff0a7cf37633ccff30f2e86f4563f41f998e4473f34c142a9b5cd2157cc738c781be20b5f152f022184a
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module MyJohnDeere
|
2
2
|
class ListObject < Requestable
|
3
3
|
OPTION_ATTRIBUTES = [:count, :start, :etag]
|
4
|
-
attr_reader :data, :listable, :total, :options
|
4
|
+
attr_reader :data, :listable, :total, :options, :last_response_code
|
5
5
|
include Enumerable
|
6
6
|
|
7
7
|
OPTION_ATTRIBUTES.each do |attribute|
|
@@ -13,12 +13,14 @@ module MyJohnDeere
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def initialize(listable, access_token, json_data,
|
16
|
+
def initialize(listable, access_token, json_data, last_response_code,
|
17
17
|
options: {})
|
18
|
+
@last_response_code = last_response_code
|
18
19
|
@options = options
|
19
20
|
# Confirm object is listable?
|
20
21
|
@listable = listable
|
21
|
-
|
22
|
+
json_data = {} if not_modified?
|
23
|
+
@data = (json_data["values"] || []).collect { |i| listable.new(i, access_token) }
|
22
24
|
if self.using_etag?
|
23
25
|
MyJohnDeere.logger.info("Using etag, ignoring any specification about start/count")
|
24
26
|
self.start = 0
|
@@ -52,43 +54,13 @@ module MyJohnDeere
|
|
52
54
|
return !self.using_etag? && self.start + self.data.length < self.total
|
53
55
|
end
|
54
56
|
|
57
|
+
def not_modified?
|
58
|
+
return self.last_response_code == 304
|
59
|
+
end
|
60
|
+
|
55
61
|
def using_etag?
|
56
62
|
# will be equal "" or some other string
|
57
63
|
return !self.etag.nil?
|
58
64
|
end
|
59
65
|
end
|
60
|
-
end
|
61
|
-
# def iterate_through_john_deere_resource(john_deere_resource_path, options={})
|
62
|
-
# options = {
|
63
|
-
# headers: nil,
|
64
|
-
# body: ""
|
65
|
-
# }.merge(options)
|
66
|
-
# loop do
|
67
|
-
# response = self.request_against_access_token(:get, john_deere_resource_path, options)
|
68
|
-
|
69
|
-
# case response.code.to_s
|
70
|
-
# when "304" # Not modified, the headers won't change either
|
71
|
-
# logger.info("JohnDeere iteration not modified: #{john_deere_resource_path}")
|
72
|
-
# break
|
73
|
-
# when "200" # Success
|
74
|
-
# # now make the json and yield it to the block
|
75
|
-
# response_json = JSON.parse(response.body)
|
76
|
-
|
77
|
-
# yield(response_json, response.to_hash)
|
78
|
-
|
79
|
-
# # see if we have another page, will either be nil or the hash, which will have ["uri"]
|
80
|
-
# next_uri = response_json["links"].find { |link| link["rel"] == "nextPage" }
|
81
|
-
# full_resource_uri = next_uri.try(:[], "uri")
|
82
|
-
# break if full_resource_uri.nil?
|
83
|
-
|
84
|
-
# # convert it to a regular path so we don't have to mess with the
|
85
|
-
# # full route
|
86
|
-
# john_deere_resource_path = URI.parse(full_resource_uri).path
|
87
|
-
|
88
|
-
# # one final check
|
89
|
-
# break if john_deere_resource_path.nil?
|
90
|
-
# else
|
91
|
-
# break
|
92
|
-
# end
|
93
|
-
# end
|
94
|
-
# end
|
66
|
+
end
|
data/lib/myjohndeere/version.rb
CHANGED
data/test/test_list_object.rb
CHANGED
@@ -5,7 +5,8 @@ class TestListObject < Minitest::Test
|
|
5
5
|
data = Array.new(3, API_FIXTURES["organization"])
|
6
6
|
list = MyJohnDeere::ListObject.new(MyJohnDeere::Organization,
|
7
7
|
default_access_token,
|
8
|
-
{"values" => data}
|
8
|
+
{"values" => data},
|
9
|
+
200)
|
9
10
|
|
10
11
|
list.each_with_index do |x, i|
|
11
12
|
assert_equal data[i]["id"], x.id
|
@@ -31,12 +32,25 @@ class TestListObject < Minitest::Test
|
|
31
32
|
assert !list.has_more?, "The data is equal to the total"
|
32
33
|
end
|
33
34
|
|
35
|
+
def test_not_modified_with_etag
|
36
|
+
etag_val = "something"
|
37
|
+
new_etag = "something2"
|
38
|
+
# Validate that the etag header is getting propagated to the next request
|
39
|
+
stub_request(:get, /organizations/).
|
40
|
+
with(headers: {MyJohnDeere::ETAG_HEADER_KEY=>etag_val}).
|
41
|
+
to_return(status: 304, body: '', headers: {MyJohnDeere::ETAG_HEADER_KEY=>new_etag})
|
42
|
+
organizations = MyJohnDeere::Organization.list(default_access_token, etag: etag_val)
|
43
|
+
|
44
|
+
assert_equal 0, organizations.data.count
|
45
|
+
assert organizations.not_modified?
|
46
|
+
end
|
47
|
+
|
34
48
|
def test_get_next_page_with_etag
|
35
49
|
# etag behavior gets the entire list.
|
36
50
|
test_json = API_FIXTURES["organizations"]
|
37
51
|
existing_data = Array.new(test_json["total"]-1, test_json["values"][0])
|
38
52
|
list = MyJohnDeere::ListObject.new(MyJohnDeere::Organization, default_access_token,
|
39
|
-
{"values" => existing_data}, options: {
|
53
|
+
{"values" => existing_data}, 200, options: {
|
40
54
|
start: 0, count: existing_data.length, etag: ""})
|
41
55
|
assert list.using_etag?
|
42
56
|
assert !list.has_more?
|
@@ -54,12 +68,11 @@ class TestListObject < Minitest::Test
|
|
54
68
|
test_json = API_FIXTURES["organizations"]
|
55
69
|
existing_data = (1..(test_json["total"]-1)).to_a
|
56
70
|
list = MyJohnDeere::ListObject.new(MyJohnDeere::Organization, default_access_token,
|
57
|
-
test_json, options: {start: 0,
|
71
|
+
test_json, 200, options: {start: 0,
|
58
72
|
count: existing_data.length})
|
59
73
|
assert list.has_more?
|
60
74
|
assert !list.using_etag?
|
61
75
|
|
62
|
-
# Validate that the etag header is getting propagated to the next request
|
63
76
|
stub_request(:get, /organizations;start=#{existing_data.count};count=#{existing_data.count}/).
|
64
77
|
with(headers: {'Accept'=>'application/vnd.deere.axiom.v3+json'}).
|
65
78
|
to_return(status: 200, body: test_json.to_json())
|