gds-api-adapters 4.7.0 → 4.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ require 'rack/utils'
2
+
1
3
  module GdsApi
2
4
  class Rummager
3
5
  class SearchUriNotSpecified < RuntimeError; end
@@ -22,11 +24,25 @@ module GdsApi
22
24
  search_response(:autocomplete, query, format_filter).body
23
25
  end
24
26
 
27
+ def advanced_search(args)
28
+ return [] if args.nil? || args.empty?
29
+ JSON.parse(advanced_search_response(args).body)
30
+ end
31
+
25
32
  private
26
33
 
34
+ def advanced_search_response(args)
35
+ request_path = "/advanced_search?#{Rack::Utils.build_nested_query(args)}"
36
+ get_response(request_path)
37
+ end
38
+
27
39
  def search_response(type, query, format_filter = nil)
28
40
  request_path = "/#{type}?q=#{CGI.escape(query)}"
29
41
  request_path << "&format_filter=#{CGI.escape(format_filter)}" if format_filter
42
+ get_response(request_path)
43
+ end
44
+
45
+ def get_response(request_path)
30
46
  uri = URI("#{search_uri}#{request_path}")
31
47
  http = Net::HTTP.new(uri.host, uri.port)
32
48
  http.use_ssl = true if uri.scheme == 'https'
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '4.7.0'
2
+ VERSION = '4.8.0'
3
3
  end
@@ -5,6 +5,7 @@ describe GdsApi::Rummager do
5
5
  before(:each) do
6
6
  stub_request(:get, /example.com\/search/).to_return(body: "[]")
7
7
  stub_request(:get, /example.com\/autocomplete/).to_return(body: "[]")
8
+ stub_request(:get, /example.com\/advanced_search/).to_return(body: "[]")
8
9
  end
9
10
 
10
11
  it "should raise an exception if the search service uri is not set" do
@@ -101,7 +102,7 @@ describe GdsApi::Rummager do
101
102
  it "should escape characters that would otherwise be invalid in a URI" do
102
103
  GdsApi::Rummager.new("http://example.com").search "search term with spaces"
103
104
 
104
- # FYI: the actual request is "?q=search+term+with+spaces", but Webmock appears to be re-escaping.
105
+ #the actual request is "?q=search+term+with+spaces", but Webmock appears to be re-escaping.
105
106
  assert_requested :get, /\?q=search%20term%20with%20spaces/
106
107
  end
107
108
 
@@ -112,4 +113,65 @@ describe GdsApi::Rummager do
112
113
 
113
114
  assert_equal search_results_json, results
114
115
  end
116
+
117
+ # tests for #advanced_search
118
+
119
+ it "#advanced_search should raise an exception if the service at the search URI returns a 500" do
120
+ stub_request(:get, /example.com\/advanced_search/).to_return(status: [500, "Internal Server Error"])
121
+ assert_raises(GdsApi::Rummager::SearchServiceError) do
122
+ GdsApi::Rummager.new("http://example.com").advanced_search({keywords: "query"})
123
+ end
124
+ end
125
+
126
+ it "#advanced_search should raise an exception if the service at the search URI returns a 404" do
127
+ stub_request(:get, /example.com\/advanced_search/).to_return(status: [404, "Not Found"])
128
+ assert_raises(GdsApi::Rummager::SearchServiceError) do
129
+ GdsApi::Rummager.new("http://example.com").advanced_search({keywords: "query"})
130
+ end
131
+ end
132
+
133
+ it "#advanced_search should raise an exception if the service at the search URI times out" do
134
+ stub_request(:get, /example.com\/advanced_search/).to_timeout
135
+ assert_raises(GdsApi::Rummager::SearchTimeout) do
136
+ GdsApi::Rummager.new("http://example.com").advanced_search({keywords: "query"})
137
+ end
138
+ end
139
+
140
+ it "#advanced_search should return the search deserialized from json" do
141
+ search_results = [{"title" => "document-title"}]
142
+ stub_request(:get, /example.com\/advanced_search/).to_return(body: search_results.to_json)
143
+ results = GdsApi::Rummager.new("http://example.com").advanced_search({keywords: "query"})
144
+
145
+ assert_equal search_results, results
146
+ end
147
+
148
+ it "#advanced_search should return an empty set of results without making request if arguments are empty" do
149
+ results = GdsApi::Rummager.new("http://example.com").advanced_search({})
150
+
151
+ assert_equal [], results
152
+ assert_not_requested :get, /example.com/
153
+ end
154
+
155
+ it "#advanced_search should return an empty set of results without making request if arguments is nil" do
156
+ results = GdsApi::Rummager.new("http://example.com").advanced_search(nil)
157
+
158
+ assert_equal [], results
159
+ assert_not_requested :get, /example.com/
160
+ end
161
+
162
+ it "#advanced_search should request the search results in JSON format" do
163
+ GdsApi::Rummager.new("http://example.com").advanced_search({keywords: "query"})
164
+
165
+ assert_requested :get, /.*/, headers: {"Accept" => "application/json"}
166
+ end
167
+
168
+ it "#advanced_search should issue a request for all the params supplied" do
169
+ GdsApi::Rummager.new("http://example.com").advanced_search({keywords: "query & stuff", topics: ["1","2"], order: {public_timestamp: "desc"}})
170
+
171
+ #the actual request is "?keywords=query+%26+stuff&topic[0]=1&topic[1]=2&order[public_timestamp]=desc", but Webmock appears to be re-escaping.
172
+ assert_requested :get, /keywords=query%20%26%20stuff/
173
+ assert_requested :get, /topics%5B0%5D=1&topics%5B1%5D=2/
174
+ assert_requested :get, /order%5Bpublic_timestamp%5D=desc/
175
+ end
176
+
115
177
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 4.7.0
5
+ version: 4.8.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Stewart
@@ -239,7 +239,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
239
  requirements:
240
240
  - - ">="
241
241
  - !ruby/object:Gem::Version
242
- hash: 2908169980141436257
242
+ hash: 1465373721783648305
243
243
  segments:
244
244
  - 0
245
245
  version: "0"
@@ -248,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
248
248
  requirements:
249
249
  - - ">="
250
250
  - !ruby/object:Gem::Version
251
- hash: 2908169980141436257
251
+ hash: 1465373721783648305
252
252
  segments:
253
253
  - 0
254
254
  version: "0"