gds-api-adapters 0.2.3 → 1.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 +2 -1
- data/lib/gds_api/json_client.rb +5 -0
- data/lib/gds_api/licence_application.rb +23 -0
- data/lib/gds_api/panopticon/registerer.rb +2 -3
- data/lib/gds_api/panopticon.rb +4 -0
- data/lib/gds_api/test_helpers/imminence.rb +4 -5
- data/lib/gds_api/version.rb +1 -1
- data/test/licence_application_api_test.rb +77 -0
- data/test/panopticon_api_test.rb +10 -1
- metadata +28 -27
- data/test/panopticon_registerer_test.rb +0 -55
data/lib/gds_api/base.rb
CHANGED
data/lib/gds_api/json_client.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require_relative 'response'
|
2
2
|
require_relative 'exceptions'
|
3
3
|
require_relative 'version'
|
4
|
+
require 'net/http'
|
4
5
|
require 'lrucache'
|
5
6
|
|
6
7
|
module GdsApi
|
@@ -67,6 +68,10 @@ module GdsApi
|
|
67
68
|
do_json_request(Net::HTTP::Put, url, params)
|
68
69
|
end
|
69
70
|
|
71
|
+
def delete_json!(url, params = nil)
|
72
|
+
do_request(Net::HTTP::Delete, url, params)
|
73
|
+
end
|
74
|
+
|
70
75
|
private
|
71
76
|
def do_raw_request(method_class, url, params = nil)
|
72
77
|
response, loggable = do_request(method_class, url, params)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
|
3
|
+
class GdsApi::LicenceApplication < GdsApi::Base
|
4
|
+
def details_for_licence(id, snac_code = nil)
|
5
|
+
return nil if id.nil?
|
6
|
+
|
7
|
+
if response = get_json(build_url(id, snac_code))
|
8
|
+
response.to_hash
|
9
|
+
else
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def build_url(id, snac_code)
|
17
|
+
if snac_code
|
18
|
+
"#{@endpoint}/api/#{id}/#{snac_code}"
|
19
|
+
else
|
20
|
+
"#{@endpoint}/api/#{id}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -12,7 +12,6 @@ module GdsApi
|
|
12
12
|
@kind = options[:kind] || 'custom-application'
|
13
13
|
@panopticon = options[:panopticon]
|
14
14
|
@platform = options[:platform] || Plek.current.environment
|
15
|
-
@timeout = options[:timeout] || 10
|
16
15
|
end
|
17
16
|
|
18
17
|
def record_to_artefact(record)
|
@@ -22,7 +21,7 @@ module GdsApi
|
|
22
21
|
kind: kind,
|
23
22
|
name: record.title,
|
24
23
|
description: record.description,
|
25
|
-
|
24
|
+
state: record.state
|
26
25
|
}
|
27
26
|
if rendering_app
|
28
27
|
hash[:rendering_app] = rendering_app
|
@@ -60,7 +59,7 @@ module GdsApi
|
|
60
59
|
|
61
60
|
def panopticon
|
62
61
|
options = {
|
63
|
-
timeout:
|
62
|
+
timeout: 5
|
64
63
|
}
|
65
64
|
@panopticon ||= GdsApi::Panopticon.new(@platform, options.merge(panopticon_api_credentials))
|
66
65
|
end
|
data/lib/gds_api/panopticon.rb
CHANGED
@@ -45,6 +45,10 @@ class GdsApi::Panopticon < GdsApi::Base
|
|
45
45
|
put_artefact(id_or_slug, artefact)
|
46
46
|
end
|
47
47
|
|
48
|
+
def delete_artefact!(id_or_slug)
|
49
|
+
delete_json!("#{base_url}/#{id_or_slug}.json")
|
50
|
+
end
|
51
|
+
|
48
52
|
def curated_lists
|
49
53
|
get_json("#{@endpoint}/curated_lists.json").to_hash
|
50
54
|
end
|
@@ -5,13 +5,12 @@ module GdsApi
|
|
5
5
|
module Imminence
|
6
6
|
def imminence_has_places(latitude, longitude, details)
|
7
7
|
response = JSON.dump(details['details'])
|
8
|
-
stub_request(:get, "http://imminence.test.alphagov.co.uk/places/#{details['slug']}.json").
|
9
|
-
with(:query => {"lat" => latitude, "lng" => longitude, "limit" => "5"}).
|
10
|
-
to_return(:status => 200, :body => response, :headers => {})
|
11
8
|
|
12
|
-
|
9
|
+
["http", "https"].each do |protocol|
|
10
|
+
stub_request(:get, "#{protocol}://imminence.test.alphagov.co.uk/places/#{details['slug']}.json").
|
13
11
|
with(:query => {"lat" => latitude, "lng" => longitude, "limit" => "5"}).
|
14
|
-
|
12
|
+
to_return(:status => 200, :body => response, :headers => {})
|
13
|
+
end
|
15
14
|
end
|
16
15
|
end
|
17
16
|
end
|
data/lib/gds_api/version.rb
CHANGED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "gds_api/licence_application"
|
3
|
+
|
4
|
+
class LicenceApplicationApiTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def api
|
7
|
+
GdsApi::LicenceApplication.new "test"
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_should_not_be_nil
|
11
|
+
assert_equal false, api.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_should_return_nil_if_id_nil
|
15
|
+
assert_equal nil, api.details_for_licence(nil)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_return_an_error_message_if_licence_is_unrecognised
|
19
|
+
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/bloop").
|
20
|
+
with(headers: GdsApi::JsonClient::REQUEST_HEADERS).
|
21
|
+
to_return(status: 404, body: "{\"error\": [\"Unrecognised Licence Id: bloop\"]}")
|
22
|
+
|
23
|
+
assert_equal nil, api.details_for_licence("bloop")
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_provide_full_licence_details_for_canonical_id
|
27
|
+
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/590001").
|
28
|
+
with(headers: GdsApi::JsonClient::REQUEST_HEADERS).
|
29
|
+
to_return(status: 200, body: "{\"issuingAuthorityType\":\"non-geographical\",\"geographicalAvailability\":\"England\",\"issuingAuthorities\":[{\"authorityName\":\"Authority Name\",\"interactions\":{\"apply\":{\"url\":\"www.gov.uk\",\"usesLicensify\":true,\"description\":\"Custom description\",\"payment\":\"none\",\"paymentAmount\":0}}}]}")
|
30
|
+
|
31
|
+
expected = {
|
32
|
+
"issuingAuthorityType" => "non-geographical",
|
33
|
+
"geographicalAvailability" => "England",
|
34
|
+
"issuingAuthorities" => [{"authorityName" => "Authority Name",
|
35
|
+
"interactions" => {
|
36
|
+
"apply" => {
|
37
|
+
"url" => "www.gov.uk",
|
38
|
+
"usesLicensify" => true,
|
39
|
+
"description" => "Custom description",
|
40
|
+
"payment" => "none",
|
41
|
+
"paymentAmount" => 0}
|
42
|
+
}
|
43
|
+
}]
|
44
|
+
}
|
45
|
+
|
46
|
+
assert_equal expected, api.details_for_licence("590001")
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_return_an_error_message_for_bad_snac_code_entry
|
50
|
+
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/590001/bleep").
|
51
|
+
with(headers: GdsApi::JsonClient::REQUEST_HEADERS).
|
52
|
+
to_return(status: 404, body: "{\"error\": [\"Unrecognised SNAC: bleep\"]}")
|
53
|
+
|
54
|
+
assert_equal nil, api.details_for_licence("590001", "bleep")
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_return_error_messages_for_bad_licence_id_and_snac_code
|
58
|
+
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/bloop/bleep").
|
59
|
+
with(headers: GdsApi::JsonClient::REQUEST_HEADERS).
|
60
|
+
to_return(status: 404, body: "{\"error\": [\"Unrecognised Licence Id: bloop\", \"Unrecognised SNAC: bleep\"]}")
|
61
|
+
|
62
|
+
assert_equal nil, api.details_for_licence("bloop", "bleep")
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_should_return_error_message_to_pick_a_relevant_snac_code_for_the_provided_licence_id
|
66
|
+
stub_request(:get, "https://licenceapplication.test.alphagov.co.uk/api/590001/sw10").
|
67
|
+
with(headers: GdsApi::JsonClient::REQUEST_HEADERS).
|
68
|
+
to_return(status: 200, body: "{\"error\": [\"Licence not available in the provided snac area\"], \"geographicalAvailability\": [\"Scotland\", \"NI\"]}")
|
69
|
+
|
70
|
+
expected = {
|
71
|
+
"error" => ["Licence not available in the provided snac area"],
|
72
|
+
"geographicalAvailability" => ["Scotland", "NI"]
|
73
|
+
}
|
74
|
+
|
75
|
+
assert_equal expected, api.details_for_licence("590001", "sw10")
|
76
|
+
end
|
77
|
+
end
|
data/test/panopticon_api_test.rb
CHANGED
@@ -30,7 +30,7 @@ class PanopticonApiTest < MiniTest::Unit::TestCase
|
|
30
30
|
kind: 'custom-application',
|
31
31
|
name: 'MyFoo',
|
32
32
|
description: 'A custom foo of great customness.',
|
33
|
-
|
33
|
+
state: 'live'
|
34
34
|
}
|
35
35
|
end
|
36
36
|
|
@@ -95,6 +95,15 @@ class PanopticonApiTest < MiniTest::Unit::TestCase
|
|
95
95
|
api.update_artefact(1, basic_artefact)
|
96
96
|
end
|
97
97
|
|
98
|
+
def test_can_delete_an_artefact
|
99
|
+
url = "#{PANOPTICON_ENDPOINT}/artefacts/1.json"
|
100
|
+
stub_request(:delete, url)
|
101
|
+
.with(body: "")
|
102
|
+
.to_return(status: 200, body: '{}')
|
103
|
+
|
104
|
+
api.delete_artefact!(1)
|
105
|
+
end
|
106
|
+
|
98
107
|
def test_can_use_basic_auth
|
99
108
|
credentials = {user: 'fred', password: 'secret'}
|
100
109
|
api = GdsApi::Panopticon.new('test', endpoint_url: 'http://some.url', basic_auth: credentials)
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: gds-api-adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 1.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-08-
|
13
|
+
date: 2012-08-15 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: plek
|
@@ -143,36 +143,37 @@ extensions: []
|
|
143
143
|
extra_rdoc_files: []
|
144
144
|
|
145
145
|
files:
|
146
|
-
- lib/gds_api/
|
147
|
-
- lib/gds_api/
|
146
|
+
- lib/gds_api/version.rb
|
147
|
+
- lib/gds_api/publisher.rb
|
148
|
+
- lib/gds_api/panopticon/registerer.rb
|
149
|
+
- lib/gds_api/typhoeus_client.rb
|
150
|
+
- lib/gds_api/imminence.rb
|
151
|
+
- lib/gds_api/contactotron.rb
|
148
152
|
- lib/gds_api/test_helpers/publisher.rb
|
149
|
-
- lib/gds_api/test_helpers/panopticon.rb
|
150
153
|
- lib/gds_api/test_helpers/imminence.rb
|
151
|
-
- lib/gds_api/
|
152
|
-
- lib/gds_api/
|
153
|
-
- lib/gds_api/
|
154
|
+
- lib/gds_api/test_helpers/contactotron.rb
|
155
|
+
- lib/gds_api/test_helpers/panopticon.rb
|
156
|
+
- lib/gds_api/test_helpers/json_client_helper.rb
|
157
|
+
- lib/gds_api/licence_application.rb
|
158
|
+
- lib/gds_api/base.rb
|
154
159
|
- lib/gds_api/json_client.rb
|
155
|
-
- lib/gds_api/contactotron.rb
|
156
|
-
- lib/gds_api/oauth2_client.rb
|
157
160
|
- lib/gds_api/response.rb
|
158
|
-
- lib/gds_api/panopticon/registerer.rb
|
159
|
-
- lib/gds_api/publisher.rb
|
160
|
-
- lib/gds_api/exceptions.rb
|
161
|
-
- lib/gds_api/version.rb
|
162
|
-
- lib/gds_api/helpers.rb
|
163
|
-
- lib/gds_api/base.rb
|
164
161
|
- lib/gds_api/panopticon.rb
|
165
162
|
- lib/gds_api/core-ext/openstruct.rb
|
166
|
-
- lib/gds_api/
|
163
|
+
- lib/gds_api/oauth2_client.rb
|
164
|
+
- lib/gds_api/part_methods.rb
|
165
|
+
- lib/gds_api/needotron.rb
|
166
|
+
- lib/gds_api/exceptions.rb
|
167
|
+
- lib/gds_api/helpers.rb
|
167
168
|
- README.md
|
168
169
|
- Rakefile
|
169
170
|
- test/contactotron_api_test.rb
|
170
|
-
- test/json_client_test.rb
|
171
|
-
- test/publisher_api_test.rb
|
172
|
-
- test/panopticon_registerer_test.rb
|
173
171
|
- test/panopticon_api_test.rb
|
174
|
-
- test/
|
172
|
+
- test/publisher_api_test.rb
|
173
|
+
- test/json_client_test.rb
|
175
174
|
- test/gds_api_base_test.rb
|
175
|
+
- test/licence_application_api_test.rb
|
176
|
+
- test/test_helper.rb
|
176
177
|
homepage: http://github.com/alphagov/gds-api-adapters
|
177
178
|
licenses: []
|
178
179
|
|
@@ -186,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
186
187
|
requirements:
|
187
188
|
- - ">="
|
188
189
|
- !ruby/object:Gem::Version
|
189
|
-
hash:
|
190
|
+
hash: -2151341373007717106
|
190
191
|
segments:
|
191
192
|
- 0
|
192
193
|
version: "0"
|
@@ -195,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
195
196
|
requirements:
|
196
197
|
- - ">="
|
197
198
|
- !ruby/object:Gem::Version
|
198
|
-
hash:
|
199
|
+
hash: -2151341373007717106
|
199
200
|
segments:
|
200
201
|
- 0
|
201
202
|
version: "0"
|
@@ -208,9 +209,9 @@ specification_version: 3
|
|
208
209
|
summary: Adapters to work with GDS APIs
|
209
210
|
test_files:
|
210
211
|
- test/contactotron_api_test.rb
|
211
|
-
- test/json_client_test.rb
|
212
|
-
- test/publisher_api_test.rb
|
213
|
-
- test/panopticon_registerer_test.rb
|
214
212
|
- test/panopticon_api_test.rb
|
215
|
-
- test/
|
213
|
+
- test/publisher_api_test.rb
|
214
|
+
- test/json_client_test.rb
|
216
215
|
- test/gds_api_base_test.rb
|
216
|
+
- test/licence_application_api_test.rb
|
217
|
+
- test/test_helper.rb
|
@@ -1,55 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
require 'gds_api/panopticon'
|
3
|
-
|
4
|
-
describe GdsApi::Panopticon::Registerer do
|
5
|
-
|
6
|
-
|
7
|
-
describe "creating an instance of the panopticon client" do
|
8
|
-
describe "setting the platform" do
|
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"))
|
11
|
-
|
12
|
-
GdsApi::Panopticon.expects(:new).with("Something", anything()).returns(:panopticon_instance)
|
13
|
-
r = GdsApi::Panopticon::Registerer.new({})
|
14
|
-
assert_equal :panopticon_instance, r.send(:panopticon)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should allow overriding the platform" do
|
18
|
-
Plek.stubs(:current).returns(stub(environment: "Something"))
|
19
|
-
|
20
|
-
GdsApi::Panopticon.expects(:new).with("Something_else", anything()).returns(:panopticon_instance)
|
21
|
-
r = GdsApi::Panopticon::Registerer.new({platform: "Something_else"})
|
22
|
-
assert_equal :panopticon_instance, r.send(:panopticon)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "setting other options" do
|
27
|
-
it "should create an instance with a default timeout of 10 seconds" do
|
28
|
-
GdsApi::Panopticon.expects(:new).with(anything(), {timeout: 10}).returns(:panopticon_instance)
|
29
|
-
r = GdsApi::Panopticon::Registerer.new({})
|
30
|
-
assert_equal :panopticon_instance, r.send(:panopticon)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should allow overriding the timeout" do
|
34
|
-
GdsApi::Panopticon.expects(:new).with(anything(), {timeout: 15}).returns(:panopticon_instance)
|
35
|
-
r = GdsApi::Panopticon::Registerer.new({timeout: 15})
|
36
|
-
assert_equal :panopticon_instance, r.send(:panopticon)
|
37
|
-
end
|
38
|
-
|
39
|
-
it "shoule merge in the api credentials" do
|
40
|
-
GdsApi::Panopticon::Registerer.any_instance.stubs(:panopticon_api_credentials).returns({foo: "Bar", baz: "kablooie"})
|
41
|
-
GdsApi::Panopticon.expects(:new).with(anything(), {timeout: 10, foo: "Bar", baz: "kablooie"}).returns(:panopticon_instance)
|
42
|
-
r = GdsApi::Panopticon::Registerer.new({})
|
43
|
-
assert_equal :panopticon_instance, r.send(:panopticon)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should memoize the panopticon instance" do
|
48
|
-
GdsApi::Panopticon.expects(:new).once.returns(:panopticon_instance)
|
49
|
-
r = GdsApi::Panopticon::Registerer.new({})
|
50
|
-
|
51
|
-
assert_equal :panopticon_instance, r.send(:panopticon)
|
52
|
-
assert_equal :panopticon_instance, r.send(:panopticon)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|