gds-api-adapters 37.5.1 → 38.0.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.
@@ -1,76 +0,0 @@
1
- module GdsApi
2
- def self.configure
3
- yield(config)
4
- end
5
-
6
- def self.config
7
- @config ||= Config.new
8
- end
9
-
10
- class Config
11
- # Always raise an `HTTPNotFound` exception when the server returns 404 or an
12
- # `HTTPGone` when the server returns 410. This avoids nil-errors in your
13
- # code and makes debugging easier.
14
- #
15
- # Currently defaults to true.
16
- #
17
- # This configuration will be removed on December 1st, 2016. Please make sure
18
- # you upgrade gds-api-adapters to the latest version and avoid configuring
19
- # it on your client application.
20
- def always_raise_for_not_found
21
- return true if @always_raise_for_not_found.nil?
22
-
23
- @always_raise_for_not_found
24
- end
25
-
26
- def always_raise_for_not_found=(value)
27
- warn <<-doc
28
-
29
- DEPRECATION NOTICE: Please delete any instances of
30
- `GdsApi.config.always_raise_for_not_found=` from your codebase to make
31
- sure all 404 or 410 responses raise an exception.
32
-
33
- This configuration option will be be removed. Raising exceptions is now
34
- the default behaviour and it won't be possible to opt-out from December
35
- 1st, 2016.
36
-
37
- Called from: #{caller[2]}
38
-
39
- doc
40
-
41
- @always_raise_for_not_found = value
42
- end
43
-
44
- # Set to true to make `GdsApi::Response` behave like a simple hash, instead
45
- # of an OpenStruct. This will prevent nil-errors.
46
- #
47
- # Currently defaults to true.
48
- #
49
- # This configuration will be removed on December 1st, 2016. Please make sure
50
- # you upgrade gds-api-adapters to the latest version and avoid configuring
51
- # it on your client application.
52
- def hash_response_for_requests
53
- return true if @hash_response_for_requests.nil?
54
-
55
- @hash_response_for_requests
56
- end
57
-
58
- def hash_response_for_requests=(value)
59
- warn <<-doc
60
-
61
- DEPRECATION NOTICE: Please delete any instances of
62
- `GdsApi.config.hash_response_for_requests=` from your codebase to make
63
- sure responses behave like a Hash instead of an OpenStruct.
64
-
65
- This configuration option will be be removed. Returning responses that
66
- behave like a Hash is the default behaviour and it won't be possible to
67
- opt-out from December 1st, 2016.
68
-
69
- Called from: #{caller[2]}
70
-
71
- doc
72
-
73
- @hash_response_for_requests = value
74
- end
75
- end
76
- end
@@ -1,7 +0,0 @@
1
- require_relative 'base'
2
-
3
- class GdsApi::ExternalLinkTracker < GdsApi::Base
4
- def add_external_link(url)
5
- put_json!("#{endpoint}/url?url=#{CGI.escape(url)}", {})
6
- end
7
- end
@@ -1,19 +0,0 @@
1
- require_relative 'base'
2
-
3
- class GdsApi::Needotron < GdsApi::Base
4
- def need_by_id(id, opts = {})
5
- response = get_json("#{base_url}/#{id}.json")
6
- return nil if response.nil? or response['need'].nil?
7
-
8
- if opts[:as_hash]
9
- response.to_hash
10
- else
11
- response.to_ostruct.need
12
- end
13
- end
14
-
15
- private
16
- def base_url
17
- "#{@endpoint}/needs"
18
- end
19
- end
@@ -1,44 +0,0 @@
1
- module GdsApi::PartMethods
2
-
3
- def part_index(slug)
4
- parsed_content['parts'].index { |p| p['slug'] == slug }
5
- end
6
-
7
- def find_part(slug)
8
- return nil unless index = part_index(slug)
9
- parsed_content['parts'][index]
10
- end
11
-
12
- def has_parts?(part)
13
- !! (has_previous_part?(part) || has_next_part?(part))
14
- end
15
-
16
- def has_previous_part?(part)
17
- index = part_index(part['slug'])
18
- !! (index && index > 0)
19
- end
20
-
21
- def has_next_part?(part)
22
- index = part_index(part['slug'])
23
- !! (index && (index + 1) < parsed_content['parts'].length)
24
- end
25
-
26
- def part_after(part)
27
- part_at(part, 1)
28
- end
29
-
30
- def part_before(part)
31
- part_at(part, -1)
32
- end
33
-
34
- private
35
- def part_at(part, relative_offset)
36
- current_index = part_index(part['slug'])
37
- return nil unless current_index
38
-
39
- other_index = current_index + relative_offset
40
- return nil unless (0...parsed_content['parts'].length).cover?(other_index)
41
-
42
- parsed_content['parts'][other_index]
43
- end
44
- end
@@ -1,51 +0,0 @@
1
- require_relative 'base'
2
- require_relative 'part_methods'
3
-
4
- class GdsApi::Publisher < GdsApi::Base
5
- def publication_for_slug(slug, options = {})
6
- return nil if slug.nil? or slug == ''
7
-
8
- response = get_json(url_for_slug(slug, options))
9
- if response
10
- container = response.dup
11
- container.extend(GdsApi::PartMethods) if container['parts']
12
- convert_updated_date(container)
13
- container
14
- else
15
- nil
16
- end
17
- end
18
-
19
- def council_for_slug(slug, snac_codes)
20
- json = post_json(
21
- "#{@endpoint}/local_transactions/#{slug}/verify_snac.json",
22
- {'snac_codes' => snac_codes}
23
- )
24
- json['snac'] if json
25
- end
26
-
27
- def council_for_snac_code(snac)
28
- json = get_json("#{@endpoint}/local_transactions/find_by_snac?snac=#{snac}")
29
- json.to_hash if json
30
- end
31
-
32
- def council_for_name(name)
33
- name = URI.escape(name)
34
- json = get_json(
35
- "#{@endpoint}/local_transactions/find_by_council_name?name=#{name}"
36
- )
37
- json.to_hash if json
38
- end
39
-
40
- private
41
- def convert_updated_date(container)
42
- if container['updated_at'] && container['updated_at'].is_a?(String)
43
- container.parsed_content['updated_at'] =
44
- Time.parse(container['updated_at'])
45
- end
46
- end
47
-
48
- def base_url
49
- "#{@endpoint}/publications"
50
- end
51
- end
@@ -1,60 +0,0 @@
1
- require 'gds_api/test_helpers/json_client_helper'
2
-
3
- module GdsApi
4
- module TestHelpers
5
- module Publisher
6
- # Generally true. If you are initializing the client differently,
7
- # you could redefine/override the constant or stub directly.
8
- PUBLISHER_ENDPOINT = Plek.current.find("publisher")
9
-
10
- def stub_all_publisher_api_requests
11
- stub_request(:any, %r|^#{PUBLISHER_ENDPOINT}/api|)
12
- end
13
-
14
- def assert_publisher_received_reindex_request_for(slug)
15
- assert_requested(
16
- :post,
17
- "#{PUBLISHER_ENDPOINT}/api/reindex-topic-editions/#{slug}"
18
- )
19
- end
20
-
21
- def publication_exists(details, options = {})
22
- json = JSON.dump(details)
23
- uri = "#{PUBLISHER_ENDPOINT}/publications/#{details['slug']}.json"
24
- if options[:edition]
25
- uri += "?edition=#{options[:edition]}"
26
- end
27
- stub_request(:get, uri).to_return(:body => json, :status => 200)
28
- return uri
29
- end
30
-
31
- def publication_exists_for_snac(snac, details)
32
- json = JSON.dump(details)
33
- uri = "#{PUBLISHER_ENDPOINT}/publications/#{details['slug']}.json"
34
- stub_request(:get, uri).to_return(:body => json, :status => 200)
35
- stub_request(:get, uri).with(:query => {:snac => snac.to_s}).to_return(:body => json, :status => 200)
36
- return uri
37
- end
38
-
39
- def publication_does_not_exist(details)
40
- uri = "#{PUBLISHER_ENDPOINT}/publications/#{details['slug']}.json"
41
- stub_request(:get, uri).to_return(:body => 'Not Found', :status => 404)
42
- return uri
43
- end
44
-
45
- def council_exists_for_slug(input_details, output_details)
46
- json = JSON.dump(output_details)
47
- slug = input_details.delete('slug')
48
- uri = "#{PUBLISHER_ENDPOINT}/local_transactions/#{slug}/verify_snac.json"
49
- stub_request(:post, uri).with(:body => JSON.dump(input_details),
50
- :headers => GdsApi::JsonClient.default_request_headers).
51
- to_return(:body => json, :status => 200)
52
- end
53
-
54
- def no_council_for_slug(slug)
55
- uri = "#{PUBLISHER_ENDPOINT}/local_transactions/#{slug}.json"
56
- stub_request(:post, uri).to_return(:body => "", :status => 404)
57
- end
58
- end
59
- end
60
- end
@@ -1,40 +0,0 @@
1
- require 'test_helper'
2
- require 'gds_api/external_link_tracker'
3
-
4
- describe GdsApi::ExternalLinkTracker do
5
-
6
- before do
7
- @base_api_url = "http://link-tracker-api.example.com"
8
- @api = GdsApi::ExternalLinkTracker.new(@base_api_url)
9
- end
10
-
11
- describe "managing links" do
12
- it "should allow creating an external link" do
13
- req = WebMock.stub_request(:put, "#{@base_api_url}/url?url=http%3A%2F%2Ffoo.example.com%2F").
14
- to_return(:status => 201,
15
- :headers => {"Content-type" => "application/json"})
16
-
17
- response = @api.add_external_link("http://foo.example.com/")
18
- assert_equal 201, response.code
19
-
20
- assert_requested(req)
21
- end
22
-
23
- it "should raise an error if creating an external link fails" do
24
- req = WebMock.stub_request(:put, "#{@base_api_url}/url?url=invalid").
25
- to_return(:status => 400, :headers => {"Content-type" => "application/json"})
26
-
27
- e = nil
28
- begin
29
- @api.add_external_link("invalid")
30
- rescue GdsApi::HTTPErrorResponse => ex
31
- e = ex
32
- end
33
-
34
- refute_nil e
35
- assert_equal 400, e.code
36
-
37
- assert_requested(req)
38
- end
39
- end
40
- end
@@ -1,109 +0,0 @@
1
- require 'test_helper'
2
- require 'gds_api/publisher'
3
- require 'gds_api/json_client'
4
- require 'gds_api/test_helpers/publisher'
5
-
6
- describe GdsApi::Publisher do
7
- include GdsApi::TestHelpers::Publisher
8
- PUBLISHER_ENDPOINT = GdsApi::TestHelpers::Publisher::PUBLISHER_ENDPOINT
9
-
10
- def basic_answer
11
- {
12
- "audiences" => [""],
13
- "slug" => "a-publication",
14
- "tags" => "",
15
- "updated_at" => "2011-07-28T11:53:03+00:00",
16
- "type" => "answer",
17
- "body" => "Something",
18
- "title" => "A publication"
19
- }
20
- end
21
-
22
- def publication_with_parts
23
- {
24
- "audiences" => [""],
25
- "slug" => "a-publication",
26
- "tags" => "",
27
- "updated_at" => "2011-07-28T11:53:03+00:00",
28
- "type" => "guide",
29
- "body" => "Something",
30
- "parts" => [
31
- {
32
- "body" => "You may be financially protected",
33
- "number" => 1,
34
- "slug" => "introduction",
35
- "title" => "Introduction"
36
- },
37
- {
38
- "body" => "All companies selling packag",
39
- "number" => 2,
40
- "slug" => "if-you-booked-a-package-holiday",
41
- "title" => "If you booked a package holiday"
42
- },
43
- {
44
- "body" => "##Know your rights when you b",
45
- "number" => 3,
46
- "slug" => "if-you-booked-your-trip-independently",
47
- "title" => "If you booked your trip independently"
48
- }
49
- ],
50
- "title" => "A publication"
51
- }
52
- end
53
-
54
- def api
55
- @api ||= GdsApi::Publisher.new(PUBLISHER_ENDPOINT)
56
- end
57
-
58
- it "should go get resource from publisher app given a slug" do
59
- publication_exists(basic_answer)
60
- pub = api.publication_for_slug(basic_answer['slug'])
61
-
62
- assert_equal "Something", pub['body']
63
- end
64
-
65
- it "should optionally accept an edition id" do
66
- slug = "a-publication"
67
- publication = %@{"audiences":[""],
68
- "slug":"#{slug}",
69
- "tags":"",
70
- "updated_at":"2011-07-28T11:53:03+00:00",
71
- "type":"answer",
72
- "body":"Something",
73
- "title":"A publication"}@
74
- stub_request(:get, "#{PUBLISHER_ENDPOINT}/publications/#{slug}.json?edition=678").to_return(
75
- :body => publication,:status=>200)
76
-
77
- pub = api.publication_for_slug(slug,{:edition => 678})
78
- end
79
-
80
- it "should construct correct url for a slug" do
81
- assert_equal "#{PUBLISHER_ENDPOINT}/publications/slug.json", api.url_for_slug("slug")
82
- end
83
-
84
- it "should deserialise parts into whole objects" do
85
- publication_exists(publication_with_parts)
86
- pub = api.publication_for_slug(publication_with_parts['slug'])
87
- assert_equal 3, pub['parts'].size
88
- assert_equal "introduction", pub['parts'].first['slug']
89
- end
90
-
91
- it "should have part specific methods for a publication with parts" do
92
- publication_exists(publication_with_parts)
93
- pub = api.publication_for_slug(publication_with_parts['slug'])
94
- assert_equal pub.part_index("introduction"),0
95
- end
96
-
97
- it "should deserialise updated at as a time" do
98
- publication_exists(publication_with_parts)
99
- pub = api.publication_for_slug(publication_with_parts['slug'])
100
- assert_equal Time, pub['updated_at'].class
101
- end
102
-
103
- it "should be able to retrieve local transaction details" do
104
- stub_request(:post, "#{PUBLISHER_ENDPOINT}/local_transactions/fake-transaction/verify_snac.json").
105
- with(:body => "{\"snac_codes\":[12345]}", :headers => GdsApi::JsonClient.default_request_headers).
106
- to_return(:status => 200, :body => '{"snac": "12345"}', :headers => {})
107
- assert_equal '12345', api.council_for_slug('fake-transaction', [12345])
108
- end
109
- end