gds-api-adapters 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,6 +12,10 @@ class GdsApi::ContentApi < GdsApi::Base
12
12
  get_json!("#{base_url}/with_tag.json?tag=#{tag}&include_children=1")
13
13
  end
14
14
 
15
+ def curated_list(tag)
16
+ get_json("#{base_url}/with_tag.json?tag=#{tag}&sort=curated")
17
+ end
18
+
15
19
  def artefact(slug)
16
20
  get_json("#{base_url}/#{slug}.json")
17
21
  end
@@ -0,0 +1,39 @@
1
+ module GdsApi
2
+ class Rummager
3
+ class SearchUriNotSpecified < RuntimeError; end
4
+ class SearchError < RuntimeError; end
5
+ class SearchServiceError < SearchError; end
6
+ class SearchTimeout < SearchError; end
7
+
8
+ attr_accessor :search_uri
9
+
10
+ def initialize(search_uri)
11
+ raise SearchUriNotSpecified unless search_uri
12
+ self.search_uri = search_uri
13
+ end
14
+
15
+ def search(query, format_filter = nil)
16
+ return [] if query.nil? || query == ""
17
+ JSON.parse(search_response(:search, query, format_filter).body)
18
+ end
19
+
20
+ def autocomplete(query, format_filter = nil)
21
+ return [] if query.nil? || query == ""
22
+ search_response(:autocomplete, query, format_filter).body
23
+ end
24
+
25
+ private
26
+
27
+ def search_response(type, query, format_filter = nil)
28
+ request_path = "/#{type}?q=#{CGI.escape(query)}"
29
+ request_path << "&format_filter=#{CGI.escape(format_filter)}" if format_filter
30
+ uri = URI("#{search_uri}#{request_path}")
31
+ http = Net::HTTP.new(uri.host, uri.port)
32
+ response = http.get(uri.request_uri, {"Accept" => "application/json"})
33
+ raise SearchServiceError.new("#{response.code}: #{response.body}") unless response.code == "200"
34
+ response
35
+ rescue Timeout::Error
36
+ raise SearchTimeout
37
+ end
38
+ end
39
+ end
@@ -54,6 +54,7 @@ module GdsApi
54
54
  def artefact_for_slug(slug)
55
55
  singular_response_base.merge(
56
56
  "title" => titleize_slug(slug),
57
+ "format" => "guide",
57
58
  "id" => "http://contentapi.test.gov.uk/#{slug}.json",
58
59
  "web_url" => "http://frontend.test.gov.uk/#{slug}",
59
60
  "details" => {
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '2.4.0'
2
+ VERSION = '2.5.0'
3
3
  end
@@ -0,0 +1,95 @@
1
+ require "test_helper"
2
+ require "gds_api/rummager"
3
+
4
+ describe GdsApi::Rummager do
5
+ before(:each) do
6
+ stub_request(:get, /example.com\/search/).to_return(body: "[]")
7
+ stub_request(:get, /example.com\/autocomplete/).to_return(body: "[]")
8
+ end
9
+
10
+ it "should raise an exception if the search service uri is not set" do
11
+ assert_raises(GdsApi::Rummager::SearchUriNotSpecified) { GdsApi::Rummager.new(nil) }
12
+ end
13
+
14
+ it "should raise an exception if the service at the search URI returns a 500" do
15
+ stub_request(:get, /example.com\/search/).to_return(status: [500, "Internal Server Error"])
16
+ assert_raises(GdsApi::Rummager::SearchServiceError) do
17
+ GdsApi::Rummager.new("http://example.com").search("query")
18
+ end
19
+ end
20
+
21
+ it "should raise an exception if the service at the search URI returns a 404" do
22
+ stub_request(:get, /example.com\/search/).to_return(status: [404, "Not Found"])
23
+ assert_raises(GdsApi::Rummager::SearchServiceError) do
24
+ GdsApi::Rummager.new("http://example.com").search("query")
25
+ end
26
+ end
27
+
28
+ it "should raise an exception if the service at the search URI times out" do
29
+ stub_request(:get, /example.com\/search/).to_timeout
30
+ assert_raises(GdsApi::Rummager::SearchTimeout) do
31
+ GdsApi::Rummager.new("http://example.com").search("query")
32
+ end
33
+ end
34
+
35
+ it "should return the search deserialized from json" do
36
+ search_results = [{"title" => "document-title"}]
37
+ stub_request(:get, /example.com\/search/).to_return(body: search_results.to_json)
38
+ results = GdsApi::Rummager.new("http://example.com").search("query")
39
+
40
+ assert_equal search_results, results
41
+ end
42
+
43
+ it "should return an empty set of results without making request if query is empty" do
44
+ results = GdsApi::Rummager.new("http://example.com").search("")
45
+
46
+ assert_equal [], results
47
+ assert_not_requested :get, /example.com/
48
+ end
49
+
50
+ it "should return an empty set of results without making request if query is nil" do
51
+ results = GdsApi::Rummager.new("http://example.com").search(nil)
52
+
53
+ assert_equal [], results
54
+ assert_not_requested :get, /example.com/
55
+ end
56
+
57
+ it "should request the search results in JSON format" do
58
+ GdsApi::Rummager.new("http://example.com").search("query")
59
+
60
+ assert_requested :get, /.*/, headers: {"Accept" => "application/json"}
61
+ end
62
+
63
+ it "should issue a request for the search term specified" do
64
+ GdsApi::Rummager.new("http://example.com").search "search-term"
65
+
66
+ assert_requested :get, /\?q=search-term/
67
+ end
68
+
69
+ it "should add a format filter parameter to searches if provided" do
70
+ GdsApi::Rummager.new("http://example.com").search "search-term", "specialist_guidance"
71
+
72
+ assert_requested :get, /format_filter=specialist_guidance/
73
+ end
74
+
75
+ it "should add a format filter parameter to autocomplete if provided" do
76
+ GdsApi::Rummager.new("http://example.com").autocomplete "search-term", "specialist_guidance"
77
+
78
+ assert_requested :get, /format_filter=specialist_guidance/
79
+ end
80
+
81
+ it "should escape characters that would otherwise be invalid in a URI" do
82
+ GdsApi::Rummager.new("http://example.com").search "search term with spaces"
83
+
84
+ # FYI: the actual request is "?q=search+term+with+spaces", but Webmock appears to be re-escaping.
85
+ assert_requested :get, /\?q=search%20term%20with%20spaces/
86
+ end
87
+
88
+ it "should pass autocomplete responses back as-is" do
89
+ search_results_json = {"title" => "document-title"}.to_json
90
+ stub_request(:get, /example.com\/autocomplete/).to_return(body: search_results_json)
91
+ results = GdsApi::Rummager.new("http://example.com").autocomplete("test")
92
+
93
+ assert_equal search_results_json, results
94
+ end
95
+ end
metadata CHANGED
@@ -1,236 +1,292 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.5.0
4
5
  prerelease:
5
- version: 2.4.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - James Stewart
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-09-19 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-09-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: plek
17
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
18
17
  none: false
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
27
31
  name: null_logger
28
- requirement: &id002 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
29
33
  none: false
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
34
38
  type: :runtime
35
39
  prerelease: false
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
38
47
  name: lrucache
39
- requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
40
49
  none: false
41
- requirements:
50
+ requirements:
42
51
  - - ~>
43
- - !ruby/object:Gem::Version
52
+ - !ruby/object:Gem::Version
44
53
  version: 0.1.1
45
54
  type: :runtime
46
55
  prerelease: false
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.1
62
+ - !ruby/object:Gem::Dependency
49
63
  name: rdoc
50
- requirement: &id004 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
51
65
  none: false
52
- requirements:
53
- - - "="
54
- - !ruby/object:Gem::Version
55
- version: "3.12"
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: '3.12'
56
70
  type: :development
57
71
  prerelease: false
58
- version_requirements: *id004
59
- - !ruby/object:Gem::Dependency
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: '3.12'
78
+ - !ruby/object:Gem::Dependency
60
79
  name: rake
61
- requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
62
81
  none: false
63
- requirements:
82
+ requirements:
64
83
  - - ~>
65
- - !ruby/object:Gem::Version
84
+ - !ruby/object:Gem::Version
66
85
  version: 0.9.2.2
67
86
  type: :development
68
87
  prerelease: false
69
- version_requirements: *id005
70
- - !ruby/object:Gem::Dependency
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.9.2.2
94
+ - !ruby/object:Gem::Dependency
71
95
  name: webmock
72
- requirement: &id006 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
73
97
  none: false
74
- requirements:
98
+ requirements:
75
99
  - - ~>
76
- - !ruby/object:Gem::Version
77
- version: "1.8"
100
+ - !ruby/object:Gem::Version
101
+ version: '1.8'
78
102
  type: :development
79
103
  prerelease: false
80
- version_requirements: *id006
81
- - !ruby/object:Gem::Dependency
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '1.8'
110
+ - !ruby/object:Gem::Dependency
82
111
  name: mocha
83
- requirement: &id007 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
84
113
  none: false
85
- requirements:
114
+ requirements:
86
115
  - - ~>
87
- - !ruby/object:Gem::Version
116
+ - !ruby/object:Gem::Version
88
117
  version: 0.12.4
89
118
  type: :development
90
119
  prerelease: false
91
- version_requirements: *id007
92
- - !ruby/object:Gem::Dependency
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.12.4
126
+ - !ruby/object:Gem::Dependency
93
127
  name: minitest
94
- requirement: &id008 !ruby/object:Gem::Requirement
128
+ requirement: !ruby/object:Gem::Requirement
95
129
  none: false
96
- requirements:
130
+ requirements:
97
131
  - - ~>
98
- - !ruby/object:Gem::Version
132
+ - !ruby/object:Gem::Version
99
133
  version: 3.4.0
100
134
  type: :development
101
135
  prerelease: false
102
- version_requirements: *id008
103
- - !ruby/object:Gem::Dependency
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 3.4.0
142
+ - !ruby/object:Gem::Dependency
104
143
  name: rack
105
- requirement: &id009 !ruby/object:Gem::Requirement
144
+ requirement: !ruby/object:Gem::Requirement
106
145
  none: false
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: "0"
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
111
150
  type: :development
112
151
  prerelease: false
113
- version_requirements: *id009
114
- - !ruby/object:Gem::Dependency
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ - !ruby/object:Gem::Dependency
115
159
  name: simplecov
116
- requirement: &id010 !ruby/object:Gem::Requirement
160
+ requirement: !ruby/object:Gem::Requirement
117
161
  none: false
118
- requirements:
162
+ requirements:
119
163
  - - ~>
120
- - !ruby/object:Gem::Version
164
+ - !ruby/object:Gem::Version
121
165
  version: 0.5.4
122
166
  type: :development
123
167
  prerelease: false
124
- version_requirements: *id010
125
- - !ruby/object:Gem::Dependency
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ~>
172
+ - !ruby/object:Gem::Version
173
+ version: 0.5.4
174
+ - !ruby/object:Gem::Dependency
126
175
  name: simplecov-rcov
127
- requirement: &id011 !ruby/object:Gem::Requirement
176
+ requirement: !ruby/object:Gem::Requirement
128
177
  none: false
129
- requirements:
130
- - - ">="
131
- - !ruby/object:Gem::Version
132
- version: "0"
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
133
182
  type: :development
134
183
  prerelease: false
135
- version_requirements: *id011
136
- - !ruby/object:Gem::Dependency
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ - !ruby/object:Gem::Dependency
137
191
  name: gem_publisher
138
- requirement: &id012 !ruby/object:Gem::Requirement
192
+ requirement: !ruby/object:Gem::Requirement
139
193
  none: false
140
- requirements:
194
+ requirements:
141
195
  - - ~>
142
- - !ruby/object:Gem::Version
196
+ - !ruby/object:Gem::Version
143
197
  version: 1.1.1
144
198
  type: :development
145
199
  prerelease: false
146
- version_requirements: *id012
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ~>
204
+ - !ruby/object:Gem::Version
205
+ version: 1.1.1
147
206
  description: A set of adapters providing easy access to the GDS gov.uk APIs
148
- email:
207
+ email:
149
208
  - jystewart@gmail.com
150
209
  executables: []
151
-
152
210
  extensions: []
153
-
154
211
  extra_rdoc_files: []
155
-
156
- files:
212
+ files:
213
+ - lib/gds_api/base.rb
214
+ - lib/gds_api/contactotron.rb
157
215
  - lib/gds_api/content_api.rb
216
+ - lib/gds_api/core-ext/openstruct.rb
217
+ - lib/gds_api/exceptions.rb
218
+ - lib/gds_api/helpers.rb
219
+ - lib/gds_api/imminence.rb
220
+ - lib/gds_api/json_client.rb
158
221
  - lib/gds_api/licence_application.rb
159
- - lib/gds_api/test_helpers/content_api.rb
160
- - lib/gds_api/test_helpers/licence_application.rb
222
+ - lib/gds_api/needotron.rb
223
+ - lib/gds_api/panopticon/registerer.rb
224
+ - lib/gds_api/panopticon.rb
225
+ - lib/gds_api/part_methods.rb
226
+ - lib/gds_api/publisher.rb
227
+ - lib/gds_api/response.rb
228
+ - lib/gds_api/rummager.rb
161
229
  - lib/gds_api/test_helpers/contactotron.rb
230
+ - lib/gds_api/test_helpers/content_api.rb
231
+ - lib/gds_api/test_helpers/imminence.rb
162
232
  - lib/gds_api/test_helpers/json_client_helper.rb
163
- - lib/gds_api/test_helpers/publisher.rb
233
+ - lib/gds_api/test_helpers/licence_application.rb
164
234
  - lib/gds_api/test_helpers/panopticon.rb
165
- - lib/gds_api/test_helpers/imminence.rb
166
- - lib/gds_api/part_methods.rb
167
- - lib/gds_api/needotron.rb
235
+ - lib/gds_api/test_helpers/publisher.rb
168
236
  - lib/gds_api/typhoeus_client.rb
169
- - lib/gds_api/json_client.rb
170
- - lib/gds_api/contactotron.rb
171
- - lib/gds_api/response.rb
172
- - lib/gds_api/panopticon/registerer.rb
173
- - lib/gds_api/publisher.rb
174
- - lib/gds_api/exceptions.rb
175
237
  - lib/gds_api/version.rb
176
- - lib/gds_api/helpers.rb
177
- - lib/gds_api/base.rb
178
- - lib/gds_api/panopticon.rb
179
- - lib/gds_api/core-ext/openstruct.rb
180
- - lib/gds_api/imminence.rb
181
238
  - README.md
182
239
  - Rakefile
183
- - test/imminence_api_test.rb
184
240
  - test/contactotron_api_test.rb
185
- - test/licence_application_api_test.rb
241
+ - test/content_api_test.rb
242
+ - test/gds_api_base_test.rb
243
+ - test/imminence_api_test.rb
186
244
  - test/json_client_test.rb
187
- - test/publisher_api_test.rb
188
- - test/panopticon_registerer_test.rb
245
+ - test/licence_application_api_test.rb
189
246
  - test/panopticon_api_test.rb
247
+ - test/panopticon_registerer_test.rb
248
+ - test/publisher_api_test.rb
249
+ - test/rummager_test.rb
190
250
  - test/test_helper.rb
191
- - test/gds_api_base_test.rb
192
- - test/content_api_test.rb
193
251
  homepage: http://github.com/alphagov/gds-api-adapters
194
252
  licenses: []
195
-
196
253
  post_install_message:
197
254
  rdoc_options: []
198
-
199
- require_paths:
255
+ require_paths:
200
256
  - lib
201
- required_ruby_version: !ruby/object:Gem::Requirement
257
+ required_ruby_version: !ruby/object:Gem::Requirement
202
258
  none: false
203
- requirements:
204
- - - ">="
205
- - !ruby/object:Gem::Version
206
- hash: 1481182554123033119
207
- segments:
259
+ requirements:
260
+ - - ! '>='
261
+ - !ruby/object:Gem::Version
262
+ version: '0'
263
+ segments:
208
264
  - 0
209
- version: "0"
210
- required_rubygems_version: !ruby/object:Gem::Requirement
265
+ hash: -3693356680320886442
266
+ required_rubygems_version: !ruby/object:Gem::Requirement
211
267
  none: false
212
- requirements:
213
- - - ">="
214
- - !ruby/object:Gem::Version
215
- hash: 1481182554123033119
216
- segments:
268
+ requirements:
269
+ - - ! '>='
270
+ - !ruby/object:Gem::Version
271
+ version: '0'
272
+ segments:
217
273
  - 0
218
- version: "0"
274
+ hash: -3693356680320886442
219
275
  requirements: []
220
-
221
276
  rubyforge_project:
222
277
  rubygems_version: 1.8.24
223
278
  signing_key:
224
279
  specification_version: 3
225
280
  summary: Adapters to work with GDS APIs
226
- test_files:
227
- - test/imminence_api_test.rb
281
+ test_files:
228
282
  - test/contactotron_api_test.rb
229
- - test/licence_application_api_test.rb
283
+ - test/content_api_test.rb
284
+ - test/gds_api_base_test.rb
285
+ - test/imminence_api_test.rb
230
286
  - test/json_client_test.rb
231
- - test/publisher_api_test.rb
232
- - test/panopticon_registerer_test.rb
287
+ - test/licence_application_api_test.rb
233
288
  - test/panopticon_api_test.rb
289
+ - test/panopticon_registerer_test.rb
290
+ - test/publisher_api_test.rb
291
+ - test/rummager_test.rb
234
292
  - test/test_helper.rb
235
- - test/gds_api_base_test.rb
236
- - test/content_api_test.rb