gds-api-adapters 4.9.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ require_relative 'base'
2
+ require_relative 'exceptions'
3
+
4
+ class GdsApi::AssetManager < GdsApi::Base
5
+ include GdsApi::ExceptionHandling
6
+
7
+ # Creates an asset given attributes
8
+ #
9
+ # Makes a `POST` request to the asset manager api to create an asset. The api accepts
10
+ # the following attributes:
11
+ #
12
+ # * `file` - a File object
13
+ #
14
+ # @param asset [Hash] The attributes for the asset to send to the api.
15
+ # @return [Net::HTTPResponse] The raw http response from the api.
16
+ #
17
+ # @raise [HTTPErrorResponse] if the request returns an error
18
+ def create_asset(asset)
19
+ post_multipart("#{base_url}/assets", { :asset => asset })
20
+ end
21
+
22
+ # Fetches an asset given the id
23
+ #
24
+ # @param id [String] The asset identifier
25
+ # @return [Response, nil] A response object containing the parsed JSON response. If
26
+ # the asset cannot be found, nil wil be returned.
27
+ #
28
+ # @raise [HTTPErrorResponse] if the request returns an error
29
+ def asset(id)
30
+ get_json("#{base_url}/assets/#{id}")
31
+ end
32
+
33
+ private
34
+ def base_url
35
+ endpoint
36
+ end
37
+ end
data/lib/gds_api/base.rb CHANGED
@@ -21,7 +21,7 @@ class GdsApi::Base
21
21
  :post_json, :post_json!,
22
22
  :put_json, :put_json!,
23
23
  :delete_json!,
24
- :get_raw
24
+ :get_raw, :post_multipart
25
25
 
26
26
  attr_reader :options
27
27
 
@@ -45,16 +45,13 @@ class GdsApi::ContentApi < GdsApi::Base
45
45
  end
46
46
 
47
47
  def artefact(slug, params={})
48
- edition = params[:edition]
49
- snac = params[:snac]
50
-
51
48
  url = "#{base_url}/#{CGI.escape(slug)}.json"
52
49
  query = params.map { |k,v| "#{k}=#{v}" }
53
50
  if query.any?
54
51
  url += "?#{query.join("&")}"
55
52
  end
56
53
 
57
- if edition && ! options.include?(:bearer_token)
54
+ if params[:edition] && ! options.include?(:bearer_token)
58
55
  raise GdsApi::NoBearerToken
59
56
  end
60
57
  get_json(url)
@@ -122,10 +119,6 @@ class GdsApi::ContentApi < GdsApi::Base
122
119
  super(url, &create_response)
123
120
  end
124
121
 
125
- def countries
126
- parse_times(get_json!("#{base_url}/foreign-travel-advice.json"))
127
- end
128
-
129
122
  private
130
123
  def base_url
131
124
  endpoint
@@ -141,13 +134,4 @@ class GdsApi::ContentApi < GdsApi::Base
141
134
  batch_response
142
135
  end
143
136
  end
144
-
145
- def parse_times(response)
146
- response["results"].map do |result|
147
- if result.has_key?("updated_at")
148
- result["updated_at"] = Time.parse(result["updated_at"])
149
- end
150
- end
151
- response
152
- end
153
137
  end
@@ -19,9 +19,9 @@ class GdsApi::Imminence < GdsApi::Base
19
19
  end
20
20
 
21
21
  def places_kml(type)
22
- get_raw("#{@endpoint}/places/#{type}.kml")
22
+ get_raw("#{@endpoint}/places/#{type}.kml").body
23
23
  end
24
-
24
+
25
25
  def business_support_schemes(facets_hash)
26
26
  query = facets_hash.keys.sort.map { |k| "#{k.to_s}=#{facets_hash[k]}" }.join("&")
27
27
  query = "?#{query}" unless query.empty?
@@ -72,16 +72,22 @@ module GdsApi
72
72
  do_request(:delete, url, params)
73
73
  end
74
74
 
75
+ def post_multipart(url, params)
76
+ r = do_raw_request(:post, url, params.merge({
77
+ :multipart => true
78
+ }))
79
+ Response.new(r)
80
+ end
81
+
75
82
  private
76
83
  def do_raw_request(method, url, params = nil)
77
84
  response = do_request(method, url, params)
78
- response.body
79
85
 
80
86
  rescue RestClient::ResourceNotFound => e
81
87
  raise GdsApi::HTTPNotFound.new(e.http_code)
82
88
 
83
89
  rescue RestClient::Exception => e
84
- raise GdsApi::HTTPErrorResponse.new(response.code.to_i), e.response.body
90
+ raise GdsApi::HTTPErrorResponse.new(e.response.code.to_i), e.response.body
85
91
  end
86
92
 
87
93
  # method: the symbolic name of the method to use, e.g. :get, :post
@@ -92,7 +98,7 @@ module GdsApi
92
98
  def do_json_request(method, url, params = nil, &create_response)
93
99
 
94
100
  begin
95
- response = do_request_with_cache(method, url, params)
101
+ response = do_request_with_cache(method, url, params.to_json)
96
102
 
97
103
  rescue RestClient::ResourceNotFound => e
98
104
  raise GdsApi::HTTPNotFound.new(e.http_code)
@@ -169,7 +175,7 @@ module GdsApi
169
175
  url: url,
170
176
  headers: DEFAULT_REQUEST_HEADERS
171
177
  }
172
- method_params[:payload] = params.to_json if params
178
+ method_params[:payload] = params
173
179
  method_params = with_auth_options(method_params)
174
180
  method_params = with_timeout(method_params)
175
181
  if URI.parse(url).is_a? URI::HTTPS
@@ -0,0 +1,26 @@
1
+ module GdsApi
2
+ module TestHelpers
3
+ module AssetManager
4
+
5
+ ASSET_ENDPOINT = Plek.current.find('asset-manager')
6
+
7
+ def asset_manager_has_an_asset(id, atts)
8
+ response = atts.merge({
9
+ "_response_info" => { "status" => "ok" }
10
+ })
11
+
12
+ stub_request(:get, "#{ASSET_ENDPOINT}/assets/#{id}")
13
+ .to_return(:body => response.to_json, :status => 200)
14
+ end
15
+
16
+ def asset_manager_does_not_have_an_asset(id)
17
+ response = {
18
+ "_response_info" => { "status" => "not found" }
19
+ }
20
+
21
+ stub_request(:get, "#{ASSET_ENDPOINT}/assets/#{id}")
22
+ .to_return(:body => response.to_json, :status => 404)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -301,23 +301,6 @@ module GdsApi
301
301
  @stubbed_content_api_licences << details
302
302
  end
303
303
 
304
- def content_api_has_countries(countries)
305
- response = response_base.merge({
306
- "results" => countries.map { |slug, values|
307
- updated_at = values.has_key?(:updated_at) ? {"updated_at" => values[:updated_at]} : {}
308
- {
309
- "id" => "#{CONTENT_API_ENDPOINT}/foreign-travel-advice/#{slug}.json",
310
- "name" => values[:name],
311
- "identifier" => slug,
312
- "web_url" => "http://www.test.gov.uk/foreign-travel-advice/#{slug}",
313
- }.merge(updated_at)
314
- },
315
- "total" => countries.length
316
- })
317
-
318
- stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/foreign-travel-advice\.json}).to_return(status: 200, body: response.to_json, headers: { })
319
- end
320
-
321
304
  private
322
305
 
323
306
  def titleize_slug(slug)
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '4.9.0'
2
+ VERSION = '5.0.0'
3
3
  end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+ require 'gds_api/asset_manager'
3
+ require 'gds_api/test_helpers/asset_manager'
4
+
5
+ describe GdsApi::AssetManager do
6
+ include GdsApi::TestHelpers::AssetManager
7
+
8
+ before do
9
+ @base_api_url = Plek.current.find("asset-manager")
10
+ @api = GdsApi::AssetManager.new(@base_api_url)
11
+ end
12
+
13
+ it "can create an asset with a file" do
14
+ stub_request(:post, "#{@base_api_url}/assets").
15
+ with(:body => %r{Content\-Disposition: form\-data; name="asset\[file\]"; filename="hello\.txt"\r\nContent\-Type: text/plain}).
16
+ to_return(:body => '{"asset":{"id":"http://asset-manager.dev.gov.uk/assets/51278b2b686c82076c000003"}}', :status => 201)
17
+
18
+ file = load_fixture_file("hello.txt")
19
+ response = @api.create_asset(:file => file)
20
+
21
+ assert_equal "http://asset-manager.dev.gov.uk/assets/51278b2b686c82076c000003", response.asset.id
22
+ end
23
+
24
+ it "can get an asset" do
25
+ asset_manager_has_an_asset("test-id", { "name" => "photo.jpg", "content_type" => "image/jpeg", "file_url" => "http://fooey.gov.uk/media/photo.jpg" })
26
+
27
+ asset = @api.asset("test-id")
28
+
29
+ assert_equal "photo.jpg", asset.name
30
+ assert_equal "image/jpeg", asset.content_type
31
+ assert_equal "http://fooey.gov.uk/media/photo.jpg", asset.file_url
32
+ end
33
+
34
+ it "returns nil when an asset does not exist" do
35
+ asset_manager_does_not_have_an_asset("not-really-here")
36
+
37
+ assert_nil @api.asset("not-really-here")
38
+ end
39
+ end
@@ -556,32 +556,6 @@ describe GdsApi::ContentApi do
556
556
  end
557
557
  end
558
558
 
559
- describe "countries" do
560
- before(:each) do
561
- content_api_has_countries({
562
- 'afghanistan' => {:name => 'Afghanistan', :updated_at => "2013-02-11T16:12:22+00:00" },
563
- 'albania' => {:name => 'Albania'},
564
- 'algeria' => {:name => 'Algeria'}
565
- })
566
-
567
- @results = @api.countries['results']
568
- end
569
-
570
- it "should fetch the list of countries" do
571
- assert_equal 3, @results.length
572
-
573
- assert_equal ['afghanistan', 'albania', 'algeria'], @results.map {|c| c['identifier'] }
574
- assert_equal ['Afghanistan', 'Albania', 'Algeria'], @results.map {|c| c['name'] }
575
-
576
- assert_equal "#{@base_api_url}/foreign-travel-advice/afghanistan.json", @results.first['id']
577
- assert_equal 'http://www.test.gov.uk/foreign-travel-advice/afghanistan', @results.first['web_url']
578
- end
579
-
580
- it "should parse all updated_at values to Time objects" do
581
- assert_equal [Time.parse("2013-02-11T16:12:22+00:00"), nil, nil], @results.map {|c| c['updated_at'] }
582
- end
583
- end
584
-
585
559
  def api_response_for_results(results)
586
560
  {
587
561
  "_response_info" => {
@@ -0,0 +1 @@
1
+ Hello, world!
@@ -111,9 +111,36 @@ class ImminenceApiTest < MiniTest::Unit::TestCase
111
111
 
112
112
  schemes = c.business_support_schemes(sectors: "agriculture,healthcare,manufacturing",
113
113
  business_types: "private-company", stages: "grow-and-sustain", types: "award,loan")
114
-
114
+
115
115
  assert_equal 3, schemes.size
116
116
  assert_equal "bar-business-award", schemes.first["business_support_identifier"]
117
117
  assert_equal "Foo small business loan.", schemes.last["title"]
118
118
  end
119
+
120
+ def test_places_kml
121
+ kml_body = <<-EOS
122
+ <?xml version="1.0" encoding="UTF-8"?>
123
+ <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
124
+ <Document>
125
+ <name>DVLA Offices</name>
126
+ <Placemark>
127
+ <atom:link/>
128
+ <name>DVLA Aberdeen local office</name>
129
+ <description>For enquiries about vehicles: 0300 790 6802 (Textphone minicom users 0300 123 1279).For enquiries about driving licences: 0300 790 6801 (Textphone minicom users 0300 123 1278).Please note, all calls are handled initially by our call centre based in Swansea</description>
130
+ <address>Greyfriars House, Gallowgate, Aberdeen, AB10 1WG, UK</address>
131
+ <Point>
132
+ <coordinates>-2.0971999005177566,57.150739708305785,0</coordinates>
133
+ </Point>
134
+ </Placemark>
135
+ </document>
136
+ </kml>
137
+ EOS
138
+
139
+ stub_request(:get, "#{ROOT}/places/test.kml").
140
+ with(headers: GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
141
+ to_return(status: 200, body: kml_body )
142
+
143
+ response_body = api_client.places_kml("test")
144
+ assert_equal kml_body, response_body
145
+ end
119
146
  end
@@ -443,4 +443,32 @@ class JsonClientTest < MiniTest::Spec
443
443
 
444
444
  assert_equal "hello", response["test"]
445
445
  end
446
+
447
+ def test_client_can_post_multipart_responses
448
+ url = "http://some.endpoint/some.json"
449
+ stub_request(:post, url).
450
+ with(:body => %r{Content\-Disposition: form\-data; name="a"\r\n\r\n123}, :headers => {'Content-Type' => %r{multipart/form-data; boundary=\d+}}).
451
+ to_return(:body => '{"b": "1"}', :status => 200)
452
+
453
+ response = @client.post_multipart("http://some.endpoint/some.json", {"a" => "123"})
454
+ assert_equal "1", response["b"]
455
+ end
456
+
457
+ def test_post_multipart_should_raise_exception_if_not_found
458
+ url = "http://some.endpoint/some.json"
459
+ stub_request(:post, url).to_return(:body => '', :status => 404)
460
+
461
+ assert_raises GdsApi::HTTPNotFound do
462
+ @client.post_multipart("http://some.endpoint/some.json", {"a" => "123"})
463
+ end
464
+ end
465
+
466
+ def test_post_multipart_should_raise_error_responses
467
+ url = "http://some.endpoint/some.json"
468
+ stub_request(:post, url).to_return(:body => '', :status => 500)
469
+
470
+ assert_raises GdsApi::HTTPErrorResponse do
471
+ @client.post_multipart("http://some.endpoint/some.json", {"a" => "123"})
472
+ end
473
+ end
446
474
  end
data/test/test_helper.rb CHANGED
@@ -25,5 +25,9 @@ class MiniTest::Unit::TestCase
25
25
  end
26
26
  end
27
27
 
28
+ def load_fixture_file(filename)
29
+ File.open( File.join( File.dirname(__FILE__), "fixtures", filename ) )
30
+ end
31
+
28
32
  require 'webmock/minitest'
29
33
  WebMock.disable_net_connect!
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.9.0
5
+ version: 5.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: 2013-02-18 00:00:00 Z
13
+ date: 2013-02-25 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: plek
@@ -188,46 +188,50 @@ extra_rdoc_files: []
188
188
 
189
189
  files:
190
190
  - lib/gds_api/version.rb
191
- - lib/gds_api/base.rb
192
- - lib/gds_api/licence_application.rb
191
+ - lib/gds_api/publisher.rb
193
192
  - lib/gds_api/panopticon/registerer.rb
194
- - lib/gds_api/needotron.rb
195
- - lib/gds_api/imminence.rb
196
- - lib/gds_api/rummager.rb
193
+ - lib/gds_api/asset_manager.rb
197
194
  - lib/gds_api/content_api.rb
198
- - lib/gds_api/mapit.rb
199
- - lib/gds_api/exceptions.rb
200
- - lib/gds_api/json_client.rb
201
- - lib/gds_api/core-ext/openstruct.rb
202
- - lib/gds_api/response.rb
203
- - lib/gds_api/content_api/list_response.rb
204
- - lib/gds_api/content_api/response.rb
205
- - lib/gds_api/publisher.rb
206
- - lib/gds_api/test_helpers/licence_application.rb
207
- - lib/gds_api/test_helpers/imminence.rb
195
+ - lib/gds_api/imminence.rb
196
+ - lib/gds_api/test_helpers/publisher.rb
197
+ - lib/gds_api/test_helpers/asset_manager.rb
208
198
  - lib/gds_api/test_helpers/content_api.rb
199
+ - lib/gds_api/test_helpers/imminence.rb
200
+ - lib/gds_api/test_helpers/licence_application.rb
209
201
  - lib/gds_api/test_helpers/mapit.rb
210
202
  - lib/gds_api/test_helpers/content_api/artefact_stub.rb
211
- - lib/gds_api/test_helpers/publisher.rb
212
203
  - lib/gds_api/test_helpers/panopticon.rb
213
204
  - lib/gds_api/test_helpers/json_client_helper.rb
214
- - lib/gds_api/part_methods.rb
205
+ - lib/gds_api/licence_application.rb
206
+ - lib/gds_api/base.rb
207
+ - lib/gds_api/mapit.rb
208
+ - lib/gds_api/json_client.rb
209
+ - lib/gds_api/response.rb
210
+ - lib/gds_api/content_api/list_response.rb
211
+ - lib/gds_api/content_api/response.rb
212
+ - lib/gds_api/rummager.rb
215
213
  - lib/gds_api/panopticon.rb
214
+ - lib/gds_api/core-ext/openstruct.rb
215
+ - lib/gds_api/part_methods.rb
216
+ - lib/gds_api/needotron.rb
217
+ - lib/gds_api/exceptions.rb
216
218
  - lib/gds_api/helpers.rb
217
219
  - README.md
218
220
  - Rakefile
219
- - test/rummager_test.rb
221
+ - test/panopticon_api_test.rb
220
222
  - test/publisher_api_test.rb
221
- - test/content_api_response_test.rb
222
- - test/licence_application_api_test.rb
223
223
  - test/mapit_test.rb
224
- - test/panopticon_api_test.rb
224
+ - test/rummager_test.rb
225
225
  - test/imminence_api_test.rb
226
+ - test/panopticon_registerer_test.rb
227
+ - test/content_api_test.rb
228
+ - test/asset_manager_test.rb
229
+ - test/content_api_response_test.rb
226
230
  - test/json_client_test.rb
227
- - test/test_helper.rb
228
231
  - test/gds_api_base_test.rb
229
- - test/content_api_test.rb
230
- - test/panopticon_registerer_test.rb
232
+ - test/licence_application_api_test.rb
233
+ - test/fixtures/hello.txt
234
+ - test/test_helper.rb
231
235
  homepage: http://github.com/alphagov/gds-api-adapters
232
236
  licenses: []
233
237
 
@@ -241,7 +245,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
241
245
  requirements:
242
246
  - - ">="
243
247
  - !ruby/object:Gem::Version
244
- hash: -2287437040986270059
248
+ hash: 2029378621472477619
245
249
  segments:
246
250
  - 0
247
251
  version: "0"
@@ -250,7 +254,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
250
254
  requirements:
251
255
  - - ">="
252
256
  - !ruby/object:Gem::Version
253
- hash: -2287437040986270059
257
+ hash: 2029378621472477619
254
258
  segments:
255
259
  - 0
256
260
  version: "0"
@@ -262,15 +266,17 @@ signing_key:
262
266
  specification_version: 3
263
267
  summary: Adapters to work with GDS APIs
264
268
  test_files:
265
- - test/rummager_test.rb
269
+ - test/panopticon_api_test.rb
266
270
  - test/publisher_api_test.rb
267
- - test/content_api_response_test.rb
268
- - test/licence_application_api_test.rb
269
271
  - test/mapit_test.rb
270
- - test/panopticon_api_test.rb
272
+ - test/rummager_test.rb
271
273
  - test/imminence_api_test.rb
274
+ - test/panopticon_registerer_test.rb
275
+ - test/content_api_test.rb
276
+ - test/asset_manager_test.rb
277
+ - test/content_api_response_test.rb
272
278
  - test/json_client_test.rb
273
- - test/test_helper.rb
274
279
  - test/gds_api_base_test.rb
275
- - test/content_api_test.rb
276
- - test/panopticon_registerer_test.rb
280
+ - test/licence_application_api_test.rb
281
+ - test/fixtures/hello.txt
282
+ - test/test_helper.rb