gds-api-adapters 3.3.1 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/gds_api/json_client.rb +5 -3
- data/lib/gds_api/version.rb +1 -1
- data/test/json_client_test.rb +102 -1
- data/test/test_helper.rb +8 -1
- metadata +46 -35
data/lib/gds_api/json_client.rb
CHANGED
@@ -9,8 +9,8 @@ module GdsApi
|
|
9
9
|
|
10
10
|
include GdsApi::ExceptionHandling
|
11
11
|
|
12
|
-
def self.cache(size=DEFAULT_CACHE_SIZE)
|
13
|
-
@cache ||= LRUCache.new(max_size: size)
|
12
|
+
def self.cache(size=DEFAULT_CACHE_SIZE, ttl=DEFAULT_CACHE_TTL)
|
13
|
+
@cache ||= LRUCache.new(max_size: size, ttl: ttl)
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.cache=(c)
|
@@ -22,7 +22,8 @@ module GdsApi
|
|
22
22
|
def initialize(options = {})
|
23
23
|
@logger = options[:logger] || GdsApi::Base.logger
|
24
24
|
cache_size = options[:cache_size] || DEFAULT_CACHE_SIZE
|
25
|
-
|
25
|
+
cache_ttl = options[:cache_ttl] || DEFAULT_CACHE_TTL
|
26
|
+
@cache = JsonClient.cache(cache_size, cache_ttl)
|
26
27
|
@options = options
|
27
28
|
end
|
28
29
|
|
@@ -33,6 +34,7 @@ module GdsApi
|
|
33
34
|
}
|
34
35
|
DEFAULT_TIMEOUT_IN_SECONDS = 4
|
35
36
|
DEFAULT_CACHE_SIZE = 10
|
37
|
+
DEFAULT_CACHE_TTL = 15 * 60 # 15 minutes
|
36
38
|
|
37
39
|
def get_raw(url)
|
38
40
|
do_raw_request(Net::HTTP::Get, url)
|
data/lib/gds_api/version.rb
CHANGED
data/test/json_client_test.rb
CHANGED
@@ -11,6 +11,7 @@ class JsonClientTest < MiniTest::Spec
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def teardown
|
14
|
+
super
|
14
15
|
GdsApi::JsonClient.cache = @json_client_cache
|
15
16
|
end
|
16
17
|
|
@@ -57,12 +58,112 @@ class JsonClientTest < MiniTest::Spec
|
|
57
58
|
end
|
58
59
|
|
59
60
|
def test_should_cache_multiple_requests_to_same_url_across_instances
|
61
|
+
GdsApi::JsonClient.cache = nil # clear the stubbed cache instance
|
62
|
+
|
60
63
|
url = "http://some.endpoint/some.json"
|
61
64
|
result = {"foo" => "bar"}
|
62
|
-
stub_request(:get, url).to_return(:body => JSON.dump(result), :status => 200)
|
65
|
+
stub_request(:get, url).to_return(:body => JSON.dump(result), :status => 200)
|
63
66
|
response_a = GdsApi::JsonClient.new.get_json(url)
|
64
67
|
response_b = GdsApi::JsonClient.new.get_json(url)
|
65
68
|
assert_equal response_a.object_id, response_b.object_id
|
69
|
+
assert_requested :get, url, times: 1
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_should_cache_up_to_10_items_by_default
|
73
|
+
GdsApi::JsonClient.cache = nil # clear the stubbed cache instance
|
74
|
+
|
75
|
+
url = "http://some.endpoint/"
|
76
|
+
result = {"foo" => "bar"}
|
77
|
+
stub_request(:get, %r{\A#{url}}).to_return do |request|
|
78
|
+
{:body => {"url" => request.uri}.to_json, :status => 200}
|
79
|
+
end
|
80
|
+
|
81
|
+
response_a = GdsApi::JsonClient.new.get_json("#{url}/first.json")
|
82
|
+
response_b = GdsApi::JsonClient.new.get_json("#{url}/second.json")
|
83
|
+
9.times { |n| GdsApi::JsonClient.new.get_json("#{url}/#{n}.json") }
|
84
|
+
|
85
|
+
response_c = GdsApi::JsonClient.new.get_json("#{url}/second.json")
|
86
|
+
response_d = GdsApi::JsonClient.new.get_json("#{url}/first.json")
|
87
|
+
|
88
|
+
assert_requested :get, "#{url}/second.json", times: 1
|
89
|
+
assert_requested :get, "#{url}/first.json", times: 2
|
90
|
+
assert_equal response_b.to_hash, response_c.to_hash
|
91
|
+
assert_equal response_a.to_hash, response_d.to_hash
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_allow_overriding_the_number_of_cached_items
|
95
|
+
GdsApi::JsonClient.cache = nil # clear the stubbed cache instance
|
96
|
+
|
97
|
+
url = "http://some.endpoint/"
|
98
|
+
result = {"foo" => "bar"}
|
99
|
+
stub_request(:get, %r{\A#{url}}).to_return do |request|
|
100
|
+
{:body => {"url" => request.uri}.to_json, :status => 200}
|
101
|
+
end
|
102
|
+
|
103
|
+
response_a = GdsApi::JsonClient.new(:cache_size => 5).get_json("#{url}/first.json")
|
104
|
+
response_b = GdsApi::JsonClient.new.get_json("#{url}/second.json")
|
105
|
+
4.times { |n| GdsApi::JsonClient.new.get_json("#{url}/#{n}.json") }
|
106
|
+
|
107
|
+
response_c = GdsApi::JsonClient.new.get_json("#{url}/second.json")
|
108
|
+
response_d = GdsApi::JsonClient.new.get_json("#{url}/first.json")
|
109
|
+
|
110
|
+
assert_requested :get, "#{url}/second.json", times: 1
|
111
|
+
assert_requested :get, "#{url}/first.json", times: 2
|
112
|
+
assert_equal response_b.to_hash, response_c.to_hash
|
113
|
+
assert_equal response_a.to_hash, response_d.to_hash
|
114
|
+
end
|
115
|
+
def test_should_cache_requests_for_15_mins_by_default
|
116
|
+
GdsApi::JsonClient.cache = nil # cause it to contruct a new cache instance.
|
117
|
+
|
118
|
+
url = "http://some.endpoint/some.json"
|
119
|
+
result = {"foo" => "bar"}
|
120
|
+
stub_request(:get, url).to_return(:body => JSON.dump(result), :status => 200)#.times(1)
|
121
|
+
response_a = GdsApi::JsonClient.new.get_json(url)
|
122
|
+
response_b = GdsApi::JsonClient.new.get_json(url)
|
123
|
+
|
124
|
+
assert_requested :get, url, times: 1
|
125
|
+
assert_equal response_a.object_id, response_b.object_id
|
126
|
+
|
127
|
+
Timecop.travel( 15 * 60 - 30) do # now + 14 mins 30 secs
|
128
|
+
response_c = GdsApi::JsonClient.new.get_json(url)
|
129
|
+
|
130
|
+
assert_requested :get, url, times: 1
|
131
|
+
assert_same response_a, response_c
|
132
|
+
end
|
133
|
+
|
134
|
+
Timecop.travel( 15 * 60 + 30) do # now + 15 mins 30 secs
|
135
|
+
response_d = GdsApi::JsonClient.new.get_json(url)
|
136
|
+
|
137
|
+
assert_requested :get, url, times: 2
|
138
|
+
assert_equal response_a.to_hash, response_d.to_hash
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_should_allow_overriding_cache_ttl
|
143
|
+
GdsApi::JsonClient.cache = nil # cause it to contruct a new cache instance.
|
144
|
+
|
145
|
+
url = "http://some.endpoint/some.json"
|
146
|
+
result = {"foo" => "bar"}
|
147
|
+
stub_request(:get, url).to_return(:body => JSON.dump(result), :status => 200)#.times(1)
|
148
|
+
response_a = GdsApi::JsonClient.new(:cache_ttl => 5 * 60).get_json(url)
|
149
|
+
response_b = GdsApi::JsonClient.new.get_json(url)
|
150
|
+
|
151
|
+
assert_requested :get, url, times: 1
|
152
|
+
assert_equal response_a.object_id, response_b.object_id
|
153
|
+
|
154
|
+
Timecop.travel( 5 * 60 - 30) do # now + 4 mins 30 secs
|
155
|
+
response_c = GdsApi::JsonClient.new.get_json(url)
|
156
|
+
|
157
|
+
assert_requested :get, url, times: 1
|
158
|
+
assert_same response_a, response_c
|
159
|
+
end
|
160
|
+
|
161
|
+
Timecop.travel( 5 * 60 + 30) do # now + 5 mins 30 secs
|
162
|
+
response_d = GdsApi::JsonClient.new.get_json(url)
|
163
|
+
|
164
|
+
assert_requested :get, url, times: 2
|
165
|
+
assert_equal response_a.to_hash, response_d.to_hash
|
166
|
+
end
|
66
167
|
end
|
67
168
|
|
68
169
|
def test_should_raise_http_not_found_if_404_returned_from_endpoint
|
data/test/test_helper.rb
CHANGED
@@ -2,11 +2,11 @@ require 'bundler'
|
|
2
2
|
Bundler.setup :default, :development, :test
|
3
3
|
|
4
4
|
require 'minitest/autorun'
|
5
|
-
require 'webmock/minitest'
|
6
5
|
require 'rack/utils'
|
7
6
|
require 'simplecov'
|
8
7
|
require 'simplecov-rcov'
|
9
8
|
require 'mocha'
|
9
|
+
require 'timecop'
|
10
10
|
|
11
11
|
SimpleCov.start do
|
12
12
|
add_filter "/test/"
|
@@ -14,4 +14,11 @@ SimpleCov.start do
|
|
14
14
|
formatter SimpleCov::Formatter::RcovFormatter
|
15
15
|
end
|
16
16
|
|
17
|
+
class MiniTest::Unit::TestCase
|
18
|
+
def teardown
|
19
|
+
Timecop.return
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'webmock/minitest'
|
17
24
|
WebMock.disable_net_connect!
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: gds-api-adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 3.
|
5
|
+
version: 3.4.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- James Stewart
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-10-
|
13
|
+
date: 2012-10-26 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: plek
|
@@ -144,6 +144,17 @@ dependencies:
|
|
144
144
|
type: :development
|
145
145
|
prerelease: false
|
146
146
|
version_requirements: *id012
|
147
|
+
- !ruby/object:Gem::Dependency
|
148
|
+
name: timecop
|
149
|
+
requirement: &id013 !ruby/object:Gem::Requirement
|
150
|
+
none: false
|
151
|
+
requirements:
|
152
|
+
- - ~>
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: 0.5.1
|
155
|
+
type: :development
|
156
|
+
prerelease: false
|
157
|
+
version_requirements: *id013
|
147
158
|
description: A set of adapters providing easy access to the GDS gov.uk APIs
|
148
159
|
email:
|
149
160
|
- jystewart@gmail.com
|
@@ -155,43 +166,43 @@ extra_rdoc_files: []
|
|
155
166
|
|
156
167
|
files:
|
157
168
|
- lib/gds_api/version.rb
|
158
|
-
- lib/gds_api/publisher.rb
|
159
|
-
- lib/gds_api/panopticon/registerer.rb
|
160
169
|
- lib/gds_api/typhoeus_client.rb
|
161
|
-
- lib/gds_api/
|
162
|
-
- lib/gds_api/
|
170
|
+
- lib/gds_api/base.rb
|
171
|
+
- lib/gds_api/licence_application.rb
|
163
172
|
- lib/gds_api/contactotron.rb
|
164
|
-
- lib/gds_api/
|
165
|
-
- lib/gds_api/
|
166
|
-
- lib/gds_api/
|
167
|
-
- lib/gds_api/
|
173
|
+
- lib/gds_api/panopticon/registerer.rb
|
174
|
+
- lib/gds_api/needotron.rb
|
175
|
+
- lib/gds_api/imminence.rb
|
176
|
+
- lib/gds_api/rummager.rb
|
177
|
+
- lib/gds_api/content_api.rb
|
178
|
+
- lib/gds_api/exceptions.rb
|
179
|
+
- lib/gds_api/json_client.rb
|
180
|
+
- lib/gds_api/core-ext/openstruct.rb
|
181
|
+
- lib/gds_api/response.rb
|
182
|
+
- lib/gds_api/publisher.rb
|
168
183
|
- lib/gds_api/test_helpers/licence_application.rb
|
184
|
+
- lib/gds_api/test_helpers/contactotron.rb
|
185
|
+
- lib/gds_api/test_helpers/imminence.rb
|
186
|
+
- lib/gds_api/test_helpers/content_api.rb
|
187
|
+
- lib/gds_api/test_helpers/publisher.rb
|
169
188
|
- lib/gds_api/test_helpers/panopticon.rb
|
170
189
|
- lib/gds_api/test_helpers/json_client_helper.rb
|
171
|
-
- lib/gds_api/licence_application.rb
|
172
|
-
- lib/gds_api/base.rb
|
173
|
-
- lib/gds_api/json_client.rb
|
174
|
-
- lib/gds_api/response.rb
|
175
|
-
- lib/gds_api/rummager.rb
|
176
|
-
- lib/gds_api/panopticon.rb
|
177
|
-
- lib/gds_api/core-ext/openstruct.rb
|
178
190
|
- lib/gds_api/part_methods.rb
|
179
|
-
- lib/gds_api/
|
180
|
-
- lib/gds_api/exceptions.rb
|
191
|
+
- lib/gds_api/panopticon.rb
|
181
192
|
- lib/gds_api/helpers.rb
|
182
193
|
- README.md
|
183
194
|
- Rakefile
|
184
|
-
- test/contactotron_api_test.rb
|
185
|
-
- test/panopticon_api_test.rb
|
186
|
-
- test/publisher_api_test.rb
|
187
195
|
- test/rummager_test.rb
|
196
|
+
- test/publisher_api_test.rb
|
197
|
+
- test/licence_application_api_test.rb
|
198
|
+
- test/panopticon_api_test.rb
|
188
199
|
- test/imminence_api_test.rb
|
189
|
-
- test/panopticon_registerer_test.rb
|
190
|
-
- test/content_api_test.rb
|
191
200
|
- test/json_client_test.rb
|
192
|
-
- test/gds_api_base_test.rb
|
193
|
-
- test/licence_application_api_test.rb
|
194
201
|
- test/test_helper.rb
|
202
|
+
- test/gds_api_base_test.rb
|
203
|
+
- test/content_api_test.rb
|
204
|
+
- test/panopticon_registerer_test.rb
|
205
|
+
- test/contactotron_api_test.rb
|
195
206
|
homepage: http://github.com/alphagov/gds-api-adapters
|
196
207
|
licenses: []
|
197
208
|
|
@@ -205,7 +216,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
205
216
|
requirements:
|
206
217
|
- - ">="
|
207
218
|
- !ruby/object:Gem::Version
|
208
|
-
hash:
|
219
|
+
hash: 1331475186711709986
|
209
220
|
segments:
|
210
221
|
- 0
|
211
222
|
version: "0"
|
@@ -214,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
214
225
|
requirements:
|
215
226
|
- - ">="
|
216
227
|
- !ruby/object:Gem::Version
|
217
|
-
hash:
|
228
|
+
hash: 1331475186711709986
|
218
229
|
segments:
|
219
230
|
- 0
|
220
231
|
version: "0"
|
@@ -226,14 +237,14 @@ signing_key:
|
|
226
237
|
specification_version: 3
|
227
238
|
summary: Adapters to work with GDS APIs
|
228
239
|
test_files:
|
229
|
-
- test/contactotron_api_test.rb
|
230
|
-
- test/panopticon_api_test.rb
|
231
|
-
- test/publisher_api_test.rb
|
232
240
|
- test/rummager_test.rb
|
241
|
+
- test/publisher_api_test.rb
|
242
|
+
- test/licence_application_api_test.rb
|
243
|
+
- test/panopticon_api_test.rb
|
233
244
|
- test/imminence_api_test.rb
|
234
|
-
- test/panopticon_registerer_test.rb
|
235
|
-
- test/content_api_test.rb
|
236
245
|
- test/json_client_test.rb
|
237
|
-
- test/gds_api_base_test.rb
|
238
|
-
- test/licence_application_api_test.rb
|
239
246
|
- test/test_helper.rb
|
247
|
+
- test/gds_api_base_test.rb
|
248
|
+
- test/content_api_test.rb
|
249
|
+
- test/panopticon_registerer_test.rb
|
250
|
+
- test/contactotron_api_test.rb
|