gds-api-adapters 3.4.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gds_api/base.rb CHANGED
@@ -3,6 +3,9 @@ require 'cgi'
3
3
  require 'null_logger'
4
4
 
5
5
  class GdsApi::Base
6
+ class InvalidAPIURL < StandardError
7
+ end
8
+
6
9
  extend Forwardable
7
10
 
8
11
  def client
@@ -30,16 +33,12 @@ class GdsApi::Base
30
33
  @logger ||= NullLogger.instance
31
34
  end
32
35
 
33
- def initialize(platform, options_or_endpoint_url=nil, maybe_options=nil)
34
- if options_or_endpoint_url.is_a?(String)
35
- options = maybe_options || {}
36
- options[:endpoint_url] = options_or_endpoint_url
37
- else
38
- options = options_or_endpoint_url || {}
39
- end
36
+ def initialize(endpoint_url, options={})
37
+ options[:endpoint_url] = endpoint_url
38
+ raise InvalidAPIURL unless endpoint_url =~ URI::regexp
40
39
  default_options = GdsApi::Base.default_options || {}
41
40
  @options = default_options.merge(options)
42
- self.endpoint = options[:endpoint_url] || endpoint_for_platform(adapter_name, platform)
41
+ self.endpoint = options[:endpoint_url]
43
42
  end
44
43
 
45
44
  def adapter_name
@@ -53,15 +52,6 @@ class GdsApi::Base
53
52
  private
54
53
  attr_accessor :endpoint
55
54
 
56
- # This should get simpler if we can be more consistent with our domain names
57
- def endpoint_for_platform(adapter_name, platform)
58
- if platform == 'development'
59
- "http://#{adapter_name}.dev.gov.uk"
60
- else
61
- "https://#{adapter_name}.#{platform}.alphagov.co.uk"
62
- end
63
- end
64
-
65
55
  def query_string(params)
66
56
  return "" if params.empty?
67
57
 
@@ -7,23 +7,23 @@ require 'gds_api/licence_application'
7
7
  module GdsApi
8
8
  module Helpers
9
9
  def content_api
10
- @content_api ||= GdsApi::ContentApi.new(Plek.current.environment)
10
+ @content_api ||= GdsApi::ContentApi.new(Plek.current.find("contentapi"))
11
11
  end
12
12
 
13
13
  def publisher_api
14
- @api ||= GdsApi::Publisher.new(Plek.current.environment)
14
+ @api ||= GdsApi::Publisher.new(Plek.current.find("publisher"))
15
15
  end
16
16
 
17
17
  def imminence_api
18
- @imminence_api ||= GdsApi::Imminence.new(Plek.current.environment)
18
+ @imminence_api ||= GdsApi::Imminence.new(Plek.current.find("imminence"))
19
19
  end
20
20
 
21
21
  def licence_application_api
22
- @licence_application_api ||= GdsApi::LicenceApplication.new(Plek.current.environment)
22
+ @licence_application_api ||= GdsApi::LicenceApplication.new(Plek.current.find("licensify"))
23
23
  end
24
24
 
25
25
  def panopticon_api
26
- @panopticon_api ||= GdsApi::Panopticon.new(Plek.current.environment, panopticon_api_credentials)
26
+ @panopticon_api ||= GdsApi::Panopticon.new(Plek.current.find("panopticon"), panopticon_api_credentials)
27
27
  end
28
28
 
29
29
  def panopticon_api_credentials
@@ -11,7 +11,7 @@ module GdsApi
11
11
  @rendering_app = options[:rendering_app]
12
12
  @kind = options[:kind] || 'custom-application'
13
13
  @panopticon = options[:panopticon]
14
- @platform = options[:platform] || Plek.current.environment
14
+ @endpoint_url = options[:endpoint_url] || Plek.current.find("panopticon")
15
15
  @timeout = options[:timeout] || 10
16
16
  end
17
17
 
@@ -62,7 +62,7 @@ module GdsApi
62
62
  options = {
63
63
  timeout: @timeout
64
64
  }
65
- @panopticon ||= GdsApi::Panopticon.new(@platform, options.merge(panopticon_api_credentials))
65
+ @panopticon ||= GdsApi::Panopticon.new(@endpoint_url, options.merge(panopticon_api_credentials))
66
66
  end
67
67
 
68
68
  def panopticon_api_credentials
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '3.4.1'
2
+ VERSION = '4.0.0'
3
3
  end
@@ -7,7 +7,7 @@ describe GdsApi::ContentApi do
7
7
 
8
8
  before do
9
9
  @base_api_url = "https://contentapi.test.alphagov.co.uk"
10
- @api = GdsApi::ContentApi.new('test')
10
+ @api = GdsApi::ContentApi.new(@base_api_url)
11
11
  end
12
12
 
13
13
  describe "sections" do
@@ -27,7 +27,7 @@ describe GdsApi::ContentApi do
27
27
  end
28
28
 
29
29
  it "should be able to fetch unpublished editions when authenticated" do
30
- api = GdsApi::ContentApi.new('test', { bearer_token: 'MY_BEARER_TOKEN' })
30
+ api = GdsApi::ContentApi.new(@base_api_url, { bearer_token: 'MY_BEARER_TOKEN' })
31
31
  content_api_has_unpublished_artefact("devolution-uk", 3)
32
32
  response = api.artefact("devolution-uk", edition: 3)
33
33
  assert_equal "https://contentapi.test.alphagov.co.uk/devolution-uk.json", response["id"]
@@ -102,7 +102,7 @@ describe GdsApi::ContentApi do
102
102
  url = "#{@base_api_url}/licence-example.json?snac=1234&edition=1"
103
103
  stub_request(:get, url).to_return(status: 200, body: body.to_json)
104
104
 
105
- api = GdsApi::ContentApi.new('test', { bearer_token: 'MY_BEARER_TOKEN' })
105
+ api = GdsApi::ContentApi.new(@base_api_url, { bearer_token: 'MY_BEARER_TOKEN' })
106
106
  response = api.artefact('licence-example', snac: '1234', edition: '1')
107
107
 
108
108
  assert_equal "Licence example", response["title"]
@@ -12,7 +12,6 @@ class GdsApiBaseTest < MiniTest::Unit::TestCase
12
12
 
13
13
  def setup
14
14
  @orig_cache = GdsApi::JsonClient.cache
15
- @api = ConcreteApi.new('test')
16
15
  end
17
16
 
18
17
  def teardown
@@ -20,69 +19,46 @@ class GdsApiBaseTest < MiniTest::Unit::TestCase
20
19
  end
21
20
 
22
21
  def test_should_construct_escaped_query_string
23
- api = ConcreteApi.new('test')
22
+ api = ConcreteApi.new('http://foo')
24
23
  url = api.url_for_slug("slug", "a" => " ", "b" => "/")
25
24
  u = URI.parse(url)
26
25
  assert_equal "a=+&b=%2F", u.query
27
26
  end
28
27
 
29
28
  def test_should_not_add_a_question_mark_if_there_are_no_parameters
30
- api = ConcreteApi.new('test')
29
+ api = ConcreteApi.new('http://foo')
31
30
  url = api.url_for_slug("slug")
32
31
  refute_match /\?/, url
33
32
  end
34
33
 
35
- def test_should_use_platform_in_url
36
- api = ConcreteApi.new("test")
34
+ def test_should_use_endpoint_in_url
35
+ api = ConcreteApi.new("http://foobarbaz")
37
36
  url = api.url_for_slug("slug")
38
37
  u = URI.parse(url)
39
- assert_match /test\.alphagov\.co\.uk$/, u.host
40
- end
41
-
42
- def test_should_override_platform_with_endpoint_url
43
- api = ConcreteApi.new("test", "http://foo.bar")
44
- url = api.url_for_slug("slug")
45
- u = URI.parse(url)
46
- assert_equal "foo.bar", u.host
47
- end
48
-
49
- def test_should_use_dev_for_development_platform
50
- api = ConcreteApi.new("development")
51
- url = api.url_for_slug("slug")
52
- u = URI.parse(url)
53
- assert_match /dev\.gov\.uk$/, u.host
54
- end
55
-
56
- def test_should_derive_adapter_name_from_class
57
- api = ConcreteApi.new("test")
58
- url = api.url_for_slug("slug")
59
- u = URI.parse(url)
60
- assert_match /^concreteapi\.test/, u.host
61
- end
62
-
63
- def test_should_treat_second_positional_arg_as_endpoint_url_if_string
64
- api = ConcreteApi.new("test", "endpoint")
65
- assert_equal "endpoint", api.options[:endpoint_url]
38
+ assert_match /foobarbaz$/, u.host
66
39
  end
67
40
 
68
41
  def test_should_accept_options_as_second_arg
69
- api = ConcreteApi.new("test", {endpoint_url: "endpoint", foo: "bar"})
70
- assert_equal "endpoint", api.options[:endpoint_url]
42
+ api = ConcreteApi.new("http://foo", {foo: "bar"})
71
43
  assert_equal "bar", api.options[:foo]
72
44
  end
73
45
 
74
46
  def test_setting_cache_size_from_options
75
47
  GdsApi::JsonClient.cache = false
76
- api = ConcreteApi.new("test", {endpoint_url: "endpoint", cache_size: 2})
48
+ api = ConcreteApi.new("https://foo", {cache_size: 2})
77
49
  assert_equal 2, api.client.cache.max_size
78
50
  end
79
51
 
80
52
  def test_setting_cache_size_from_default_options
81
53
  GdsApi::JsonClient.cache = false
82
54
  GdsApi::Base.default_options = {cache_size: 4}
83
- api = ConcreteApi.new("test", {endpoint_url: "endpoint"})
55
+ api = ConcreteApi.new("http://bar")
84
56
  assert_equal 4, api.client.cache.max_size
85
57
  end
86
58
 
87
-
59
+ def test_should_barf_if_not_given_valid_URL
60
+ proc do
61
+ ConcreteApi.new('invalid-url')
62
+ end.must_raise GdsApi::Base::InvalidAPIURL
63
+ end
88
64
  end
@@ -8,7 +8,7 @@ class ImminenceApiTest < MiniTest::Unit::TestCase
8
8
  LONGITUDE = -0.4702813074674147
9
9
 
10
10
  def api_client
11
- GdsApi::Imminence.new('test')
11
+ GdsApi::Imminence.new(ROOT)
12
12
  end
13
13
 
14
14
  def dummy_place
@@ -10,7 +10,7 @@ class LicenceApplicationApiTest < MiniTest::Unit::TestCase
10
10
  end
11
11
 
12
12
  def api
13
- GdsApi::LicenceApplication.new "test"
13
+ GdsApi::LicenceApplication.new LICENCE_APPLICATION_ENDPOINT
14
14
  end
15
15
 
16
16
  def test_should_not_be_nil
@@ -35,7 +35,7 @@ class PanopticonApiTest < MiniTest::Unit::TestCase
35
35
  end
36
36
 
37
37
  def api
38
- GdsApi::Panopticon.new('test')
38
+ GdsApi::Panopticon.new(PANOPTICON_ENDPOINT)
39
39
  end
40
40
 
41
41
  def test_given_a_slug__should_fetch_artefact_from_panopticon
@@ -106,7 +106,7 @@ class PanopticonApiTest < MiniTest::Unit::TestCase
106
106
 
107
107
  def test_can_use_basic_auth
108
108
  credentials = {user: 'fred', password: 'secret'}
109
- api = GdsApi::Panopticon.new('test', endpoint_url: 'http://some.url', basic_auth: credentials)
109
+ api = GdsApi::Panopticon.new('http://some.url', basic_auth: credentials)
110
110
  url = "http://#{credentials[:user]}:#{credentials[:password]}@some.url/artefacts/1.json"
111
111
  stub_request(:put, url)
112
112
  .to_return(status: 200, body: '{}')
@@ -115,7 +115,7 @@ class PanopticonApiTest < MiniTest::Unit::TestCase
115
115
  end
116
116
 
117
117
  def test_can_register_new_artefacts_en_masse
118
- r = GdsApi::Panopticon::Registerer.new(platform: "test", owning_app: 'my-app')
118
+ r = GdsApi::Panopticon::Registerer.new(endpoint_url: PANOPTICON_ENDPOINT, owning_app: 'my-app')
119
119
  artefact = registerable_artefact()
120
120
  panopticon_has_no_metadata_for('foo')
121
121
 
@@ -134,7 +134,7 @@ class PanopticonApiTest < MiniTest::Unit::TestCase
134
134
 
135
135
  def test_can_register_existing_artefacts_en_masse
136
136
  artefact = registerable_artefact()
137
- r = GdsApi::Panopticon::Registerer.new(platform: "test", owning_app: 'my-app')
137
+ r = GdsApi::Panopticon::Registerer.new(endpoint_url: PANOPTICON_ENDPOINT, owning_app: 'my-app')
138
138
 
139
139
  panopticon_has_metadata(artefact)
140
140
  url = "#{PANOPTICON_ENDPOINT}/artefacts/foo.json"
@@ -146,31 +146,6 @@ class PanopticonApiTest < MiniTest::Unit::TestCase
146
146
  r.register(record)
147
147
  end
148
148
 
149
- def test_registerer_should_use_plek_env_by_default
150
- # Plek.current returns a different object each time it's called.
151
- plek_current = Plek.current
152
- plek_current.stubs(:environment).returns("foo")
153
- Plek.stubs(:current).returns(plek_current)
154
-
155
- expected_endpoint = PANOPTICON_ENDPOINT.sub(/\.test\./, '.foo.')
156
-
157
- r = GdsApi::Panopticon::Registerer.new(owning_app: 'my-app')
158
- artefact = registerable_artefact()
159
- panopticon_has_no_metadata_for('foo')
160
-
161
- stub_request(:put, "#{expected_endpoint}/artefacts/foo.json")
162
- .with(body: artefact.to_json)
163
- .to_return(body: artefact.merge(id: 1).to_json)
164
-
165
- url = "#{expected_endpoint}/artefacts.json"
166
- stub_request(:post, url)
167
- .with(body: artefact.to_json)
168
- .to_return(body: artefact.merge(id: 1).to_json)
169
-
170
- record = OpenStruct.new(artefact.merge(title: artefact[:name]))
171
- r.register(record)
172
- end
173
-
174
149
  def test_should_be_able_to_fetch_curated_lists
175
150
  stub_request(:get, "#{PANOPTICON_ENDPOINT}/curated_lists.json").
176
151
  with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
@@ -7,18 +7,18 @@ describe GdsApi::Panopticon::Registerer do
7
7
  describe "creating an instance of the panopticon client" do
8
8
  describe "setting the platform" do
9
9
  it "should create an instance using the current Plek environment as the platform by default" do
10
- Plek.stubs(:current).returns(stub(environment: "Something"))
10
+ Plek.stubs(:current).returns(stub(find: "http://thisplace"))
11
11
 
12
- GdsApi::Panopticon.expects(:new).with("Something", anything()).returns(:panopticon_instance)
12
+ GdsApi::Panopticon.expects(:new).with("http://thisplace", anything()).returns(:panopticon_instance)
13
13
  r = GdsApi::Panopticon::Registerer.new({})
14
14
  assert_equal :panopticon_instance, r.send(:panopticon)
15
15
  end
16
16
 
17
- it "should allow overriding the platform" do
18
- Plek.stubs(:current).returns(stub(environment: "Something"))
17
+ it "should pass through the endpoint url" do
18
+ Plek.stubs(:current).returns(stub(find: "http://thisplace"))
19
19
 
20
- GdsApi::Panopticon.expects(:new).with("Something_else", anything()).returns(:panopticon_instance)
21
- r = GdsApi::Panopticon::Registerer.new({platform: "Something_else"})
20
+ GdsApi::Panopticon.expects(:new).with("http://otherplace", anything()).returns(:panopticon_instance)
21
+ r = GdsApi::Panopticon::Registerer.new({endpoint_url: "http://otherplace"})
22
22
  assert_equal :panopticon_instance, r.send(:panopticon)
23
23
  end
24
24
  end
@@ -52,7 +52,7 @@ describe GdsApi::Publisher do
52
52
  end
53
53
 
54
54
  def api
55
- GdsApi::Publisher.new("test")
55
+ GdsApi::Publisher.new(PUBLISHER_ENDPOINT)
56
56
  end
57
57
 
58
58
  it "should go get resource from publisher app given a slug" do
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.4.1
5
+ version: 4.0.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-11-07 00:00:00 Z
13
+ date: 2012-11-26 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: plek
@@ -166,43 +166,40 @@ extra_rdoc_files: []
166
166
 
167
167
  files:
168
168
  - lib/gds_api/version.rb
169
- - lib/gds_api/publisher.rb
170
- - lib/gds_api/panopticon/registerer.rb
171
169
  - lib/gds_api/typhoeus_client.rb
172
- - lib/gds_api/content_api.rb
170
+ - lib/gds_api/base.rb
171
+ - lib/gds_api/licence_application.rb
172
+ - lib/gds_api/panopticon/registerer.rb
173
+ - lib/gds_api/needotron.rb
173
174
  - lib/gds_api/imminence.rb
174
- - lib/gds_api/contactotron.rb
175
- - lib/gds_api/test_helpers/publisher.rb
176
- - lib/gds_api/test_helpers/content_api.rb
177
- - lib/gds_api/test_helpers/imminence.rb
178
- - lib/gds_api/test_helpers/contactotron.rb
175
+ - lib/gds_api/rummager.rb
176
+ - lib/gds_api/content_api.rb
177
+ - lib/gds_api/exceptions.rb
178
+ - lib/gds_api/json_client.rb
179
+ - lib/gds_api/core-ext/openstruct.rb
180
+ - lib/gds_api/response.rb
181
+ - lib/gds_api/publisher.rb
179
182
  - lib/gds_api/test_helpers/licence_application.rb
183
+ - lib/gds_api/test_helpers/imminence.rb
184
+ - lib/gds_api/test_helpers/content_api.rb
185
+ - lib/gds_api/test_helpers/publisher.rb
180
186
  - lib/gds_api/test_helpers/panopticon.rb
181
187
  - lib/gds_api/test_helpers/json_client_helper.rb
182
- - lib/gds_api/licence_application.rb
183
- - lib/gds_api/base.rb
184
- - lib/gds_api/json_client.rb
185
- - lib/gds_api/response.rb
186
- - lib/gds_api/rummager.rb
187
- - lib/gds_api/panopticon.rb
188
- - lib/gds_api/core-ext/openstruct.rb
189
188
  - lib/gds_api/part_methods.rb
190
- - lib/gds_api/needotron.rb
191
- - lib/gds_api/exceptions.rb
189
+ - lib/gds_api/panopticon.rb
192
190
  - lib/gds_api/helpers.rb
193
191
  - README.md
194
192
  - Rakefile
195
- - test/contactotron_api_test.rb
196
- - test/panopticon_api_test.rb
197
- - test/publisher_api_test.rb
198
193
  - test/rummager_test.rb
194
+ - test/publisher_api_test.rb
195
+ - test/licence_application_api_test.rb
196
+ - test/panopticon_api_test.rb
199
197
  - test/imminence_api_test.rb
200
- - test/panopticon_registerer_test.rb
201
- - test/content_api_test.rb
202
198
  - test/json_client_test.rb
203
- - test/gds_api_base_test.rb
204
- - test/licence_application_api_test.rb
205
199
  - test/test_helper.rb
200
+ - test/gds_api_base_test.rb
201
+ - test/content_api_test.rb
202
+ - test/panopticon_registerer_test.rb
206
203
  homepage: http://github.com/alphagov/gds-api-adapters
207
204
  licenses: []
208
205
 
@@ -216,7 +213,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
216
213
  requirements:
217
214
  - - ">="
218
215
  - !ruby/object:Gem::Version
219
- hash: -1606313441068628012
216
+ hash: -3063781964090340583
220
217
  segments:
221
218
  - 0
222
219
  version: "0"
@@ -225,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
222
  requirements:
226
223
  - - ">="
227
224
  - !ruby/object:Gem::Version
228
- hash: -1606313441068628012
225
+ hash: -3063781964090340583
229
226
  segments:
230
227
  - 0
231
228
  version: "0"
@@ -237,14 +234,13 @@ signing_key:
237
234
  specification_version: 3
238
235
  summary: Adapters to work with GDS APIs
239
236
  test_files:
240
- - test/contactotron_api_test.rb
241
- - test/panopticon_api_test.rb
242
- - test/publisher_api_test.rb
243
237
  - test/rummager_test.rb
238
+ - test/publisher_api_test.rb
239
+ - test/licence_application_api_test.rb
240
+ - test/panopticon_api_test.rb
244
241
  - test/imminence_api_test.rb
245
- - test/panopticon_registerer_test.rb
246
- - test/content_api_test.rb
247
242
  - test/json_client_test.rb
248
- - test/gds_api_base_test.rb
249
- - test/licence_application_api_test.rb
250
243
  - test/test_helper.rb
244
+ - test/gds_api_base_test.rb
245
+ - test/content_api_test.rb
246
+ - test/panopticon_registerer_test.rb
@@ -1,11 +0,0 @@
1
- require_relative 'base'
2
-
3
- # This adapter's a bit different from the others as it assumes we know
4
- # the full URI for a contact and just want to grab its json serialization
5
- # and convert it to an ostruct.
6
- class GdsApi::Contactotron < GdsApi::Base
7
-
8
- def contact_for_uri(uri)
9
- get_json(uri)
10
- end
11
- end
@@ -1,14 +0,0 @@
1
- require 'gds_api/test_helpers/json_client_helper'
2
-
3
- module GdsApi
4
- module TestHelpers
5
- module Contactotron
6
-
7
- def contactotron_has_contact(uri, details)
8
- json = JSON.dump(details)
9
- stub_request(:get, uri).to_return(:status => 200, :body => json, :headers => {})
10
- return uri
11
- end
12
- end
13
- end
14
- end
@@ -1,17 +0,0 @@
1
- require 'test_helper'
2
- require 'gds_api/contactotron'
3
- require 'gds_api/test_helpers/contactotron'
4
-
5
- class ContactotronApiTest < MiniTest::Unit::TestCase
6
- include GdsApi::TestHelpers::Contactotron
7
-
8
- def api
9
- GdsApi::Contactotron.new "test"
10
- end
11
-
12
- def test_should_fetch_and_parse_JSON_into_ostruct
13
- uri = "http://contactotron.platform/contacts/1"
14
- contactotron_has_contact(uri, {details: 'value'})
15
- assert_equal "value", api.contact_for_uri(uri).details
16
- end
17
- end