gds-api-adapters 10.2.1 → 10.3.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/base.rb CHANGED
@@ -45,7 +45,7 @@ class GdsApi::Base
45
45
  end
46
46
 
47
47
  def url_for_slug(slug, options={})
48
- base = "#{base_url}/#{slug}.json#{query_string(options)}"
48
+ "#{base_url}/#{slug}.json#{query_string(options)}"
49
49
  end
50
50
 
51
51
  def get_list!(url)
@@ -60,8 +60,17 @@ private
60
60
  def query_string(params)
61
61
  return "" if params.empty?
62
62
 
63
- "?" << params.sort.map { |kv|
64
- kv.map { |a| CGI.escape(a.to_s) }.join("=")
65
- }.join("&")
63
+ param_pairs = params.sort.map { |key, value|
64
+ case value
65
+ when Array
66
+ value.map { |v|
67
+ "#{CGI.escape(key+'[]')}=#{CGI.escape(v.to_s)}"
68
+ }
69
+ else
70
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
71
+ end
72
+ }.flatten
73
+
74
+ "?#{param_pairs.join("&")}"
66
75
  end
67
76
  end
@@ -23,13 +23,19 @@ module GdsApi
23
23
  class HTTPNotFound < HTTPErrorResponse
24
24
  end
25
25
 
26
+ class HTTPGone < HTTPErrorResponse; end
27
+
26
28
  class NoBearerToken < BaseError; end
27
29
 
28
30
  module ExceptionHandling
29
- def ignoring(exception, &block)
31
+ def ignoring(exception_or_exceptions, &block)
30
32
  yield
31
- rescue exception
33
+ rescue *exception_or_exceptions
32
34
  # Discard the exception
33
35
  end
36
+
37
+ def ignoring_missing(&block)
38
+ ignoring([HTTPNotFound, HTTPGone], &block)
39
+ end
34
40
  end
35
41
  end
@@ -0,0 +1,18 @@
1
+ require_relative 'base'
2
+
3
+ module GdsApi
4
+ class FinderApi < Base
5
+ def get_documents(finder_slug, options = {})
6
+ get_json(documents_url(finder_slug, options))
7
+ end
8
+
9
+ private
10
+ def documents_url(finder_slug, options = {})
11
+ finder_url(finder_slug, 'documents', options)
12
+ end
13
+
14
+ def finder_url(finder_slug, action, options = {})
15
+ "#{endpoint}/finders/#{CGI.escape(finder_slug)}/#{action}.json#{query_string(options)}"
16
+ end
17
+ end
18
+ end
@@ -57,7 +57,7 @@ module GdsApi
57
57
  end
58
58
 
59
59
  def get_raw(url)
60
- ignoring GdsApi::HTTPNotFound do
60
+ ignoring_missing do
61
61
  get_raw!(url)
62
62
  end
63
63
  end
@@ -70,7 +70,7 @@ module GdsApi
70
70
  [:get, :post, :put, :delete].each do |http_method|
71
71
  method_name = "#{http_method}_json"
72
72
  define_method method_name do |url, *args, &block|
73
- ignoring GdsApi::HTTPNotFound do
73
+ ignoring_missing do
74
74
  send (method_name + "!"), url, *args, &block
75
75
  end
76
76
  end
@@ -106,6 +106,9 @@ module GdsApi
106
106
  rescue RestClient::ResourceNotFound => e
107
107
  raise GdsApi::HTTPNotFound.new(e.http_code)
108
108
 
109
+ rescue RestClient::Gone => e
110
+ raise GdsApi::HTTPGone.new(e.http_code)
111
+
109
112
  rescue RestClient::Exception => e
110
113
  raise GdsApi::HTTPErrorResponse.new(e.response.code.to_i), e.response.body
111
114
  end
@@ -124,6 +127,9 @@ module GdsApi
124
127
  rescue RestClient::ResourceNotFound => e
125
128
  raise GdsApi::HTTPNotFound.new(e.http_code)
126
129
 
130
+ rescue RestClient::Gone => e
131
+ raise GdsApi::HTTPGone.new(e.http_code)
132
+
127
133
  rescue RestClient::Exception => e
128
134
  # Attempt to parse the body as JSON if possible
129
135
  error_details = begin
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '10.2.1'
2
+ VERSION = '10.3.0'
3
3
  end
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+ require 'gds_api/finder_api'
3
+
4
+ describe GdsApi::FinderApi do
5
+ before do
6
+ @base_api_url = Plek.current.find('finder-api')
7
+ @api = GdsApi::FinderApi.new(@base_api_url)
8
+ end
9
+
10
+ describe "get_documents" do
11
+ it "should return all documents" do
12
+ documents_hash = {
13
+ 'documents' => [
14
+ {
15
+ 'title' => 'A document',
16
+ 'date' => '2014-01-24 00:00:00 +0000',
17
+ 'case_type' => 'market-investigations'
18
+ },
19
+ {
20
+ 'title' => 'Blah blah',
21
+ 'date' => '2014-01-25 00:00:00 +0000',
22
+ 'case_type' => 'merger-inquiries'
23
+ }
24
+ ]
25
+ }
26
+
27
+ req = WebMock.stub_request(:get, "#{@base_api_url}/finders/some-finder-slug/documents.json").
28
+ to_return(:body => documents_hash.to_json,
29
+ :headers => {"Content-type" => "application/json"})
30
+
31
+ response = @api.get_documents("some-finder-slug")
32
+ assert_equal 200, response.code
33
+ assert_equal documents_hash, response.to_hash
34
+
35
+ assert_requested(req)
36
+ end
37
+
38
+ it "should forward query parameters" do
39
+ documents_hash = {
40
+ 'documents' => [
41
+ {
42
+ 'title' => 'A document',
43
+ 'date' => '2014-01-24 00:00:00 +0000',
44
+ 'case_type' => 'market-investigations'
45
+ }
46
+ ]
47
+ }
48
+
49
+ req = WebMock.stub_request(:get, "#{@base_api_url}/finders/some-finder-slug/documents.json").
50
+ with(query: {case_type: 'market-investigations'}).
51
+ to_return(:body => documents_hash.to_json,
52
+ :headers => {"Content-type" => "application/json"})
53
+
54
+ response = @api.get_documents("some-finder-slug", case_type: 'market-investigations')
55
+ assert_equal 200, response.code
56
+ assert_equal documents_hash, response.to_hash
57
+
58
+ assert_requested(req)
59
+ end
60
+ end
61
+ end
@@ -25,6 +25,18 @@ class GdsApiBaseTest < MiniTest::Unit::TestCase
25
25
  assert_equal "a=+&b=%2F", u.query
26
26
  end
27
27
 
28
+ def test_should_construct_escaped_query_string_for_rails
29
+ api = ConcreteApi.new('http://foo')
30
+
31
+ url = api.url_for_slug("slug", "b" => ['123'])
32
+ u = URI.parse(url)
33
+ assert_equal "b%5B%5D=123", u.query
34
+
35
+ url = api.url_for_slug("slug", "b" => ['123', '456'])
36
+ u = URI.parse(url)
37
+ assert_equal "b%5B%5D=123&b%5B%5D=456", u.query
38
+ end
39
+
28
40
  def test_should_not_add_a_question_mark_if_there_are_no_parameters
29
41
  api = ConcreteApi.new('http://foo')
30
42
  url = api.url_for_slug("slug")
@@ -310,7 +310,7 @@ class JsonClientTest < MiniTest::Spec
310
310
  end
311
311
  end
312
312
 
313
- def test_should_raise_http_not_found_if_404_returned_from_endpoint
313
+ def test_get_bang_should_raise_http_not_found_if_404_returned_from_endpoint
314
314
  url = "http://some.endpoint/some.json"
315
315
  stub_request(:get, url).to_return(:body => "{}", :status => 404)
316
316
  assert_raises GdsApi::HTTPNotFound do
@@ -318,13 +318,27 @@ class JsonClientTest < MiniTest::Spec
318
318
  end
319
319
  end
320
320
 
321
+ def test_get_bang_should_raise_http_gone_if_410_returned_from_endpoint
322
+ url = "http://some.endpoint/some.json"
323
+ stub_request(:get, url).to_return(:body => "{}", :status => 410)
324
+ assert_raises GdsApi::HTTPGone do
325
+ @client.get_json!(url)
326
+ end
327
+ end
328
+
321
329
  def test_get_should_be_nil_if_404_returned_from_endpoint
322
330
  url = "http://some.endpoint/some.json"
323
331
  stub_request(:get, url).to_return(:body => "{}", :status => 404)
324
332
  assert_nil @client.get_json(url)
325
333
  end
326
334
 
327
- def test_get_should_raise_error_if_non_404_error_code_returned_from_endpoint
335
+ def test_get_should_be_nil_if_410_returned_from_endpoint
336
+ url = "http://some.endpoint/some.json"
337
+ stub_request(:get, url).to_return(:body => "{}", :status => 410)
338
+ assert_nil @client.get_json(url)
339
+ end
340
+
341
+ def test_get_should_raise_error_if_non_404_non_410_error_code_returned_from_endpoint
328
342
  url = "http://some.endpoint/some.json"
329
343
  stub_request(:get, url).to_return(:body => "{}", :status => 500)
330
344
  assert_raises GdsApi::HTTPErrorResponse do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.2.1
4
+ version: 10.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-25 00:00:00.000000000 Z
12
+ date: 2014-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: plek
@@ -279,6 +279,7 @@ files:
279
279
  - lib/gds_api/rummager.rb
280
280
  - lib/gds_api/part_methods.rb
281
281
  - lib/gds_api/need_api.rb
282
+ - lib/gds_api/finder_api.rb
282
283
  - lib/gds_api/railtie.rb
283
284
  - lib/gds_api/exceptions.rb
284
285
  - lib/gds_api/performance_platform/data_in.rb
@@ -335,6 +336,7 @@ files:
335
336
  - test/need_api_test.rb
336
337
  - test/publisher_api_test.rb
337
338
  - test/fact_cave_test.rb
339
+ - test/finder_api_test.rb
338
340
  - test/licence_application_api_test.rb
339
341
  - test/gov_uk_delivery_test.rb
340
342
  - test/panopticon_api_test.rb
@@ -370,7 +372,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
370
372
  version: '0'
371
373
  segments:
372
374
  - 0
373
- hash: 1654531338748998219
375
+ hash: 988974832367169730
374
376
  required_rubygems_version: !ruby/object:Gem::Requirement
375
377
  none: false
376
378
  requirements:
@@ -379,7 +381,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
379
381
  version: '0'
380
382
  segments:
381
383
  - 0
382
- hash: 1654531338748998219
384
+ hash: 988974832367169730
383
385
  requirements: []
384
386
  rubyforge_project:
385
387
  rubygems_version: 1.8.23
@@ -393,6 +395,7 @@ test_files:
393
395
  - test/need_api_test.rb
394
396
  - test/publisher_api_test.rb
395
397
  - test/fact_cave_test.rb
398
+ - test/finder_api_test.rb
396
399
  - test/licence_application_api_test.rb
397
400
  - test/gov_uk_delivery_test.rb
398
401
  - test/panopticon_api_test.rb