gds-api-adapters 10.8.0 → 10.9.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.
@@ -19,6 +19,22 @@ class GdsApi::AssetManager < GdsApi::Base
19
19
  post_multipart("#{base_url}/assets", { :asset => asset })
20
20
  end
21
21
 
22
+ # Updates an asset given attributes
23
+ #
24
+ # Makes a `PUT` request to the asset manager api to update an asset.
25
+ # The api accepts the following attributes:
26
+ #
27
+ # * `file` - a File object
28
+ #
29
+ # @param id [String] The asset identifier
30
+ # @param asset [Hash] The attributes for the asset to send to the api.
31
+ # @return [Net::HTTPResponse] The raw http response from the api.
32
+ #
33
+ # @raise [HTTPErrorResponse] if the request returns an error
34
+ def update_asset(id, asset)
35
+ put_multipart("#{base_url}/assets/#{id}", { :asset => asset })
36
+ end
37
+
22
38
  # Fetches an asset given the id
23
39
  #
24
40
  # @param id [String] The asset identifier
data/lib/gds_api/base.rb CHANGED
@@ -23,6 +23,7 @@ class GdsApi::Base
23
23
  :put_json, :put_json!,
24
24
  :delete_json!,
25
25
  :get_raw, :get_raw!,
26
+ :put_multipart,
26
27
  :post_multipart
27
28
 
28
29
  attr_reader :options
@@ -99,6 +99,13 @@ module GdsApi
99
99
  Response.new(r)
100
100
  end
101
101
 
102
+ def put_multipart(url, params)
103
+ r = do_raw_request(:put, url, params.merge({
104
+ :multipart => true
105
+ }))
106
+ Response.new(r)
107
+ end
108
+
102
109
  private
103
110
  def do_raw_request(method, url, params = nil)
104
111
  response = do_request(method, url, params)
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '10.8.0'
2
+ VERSION = '10.9.0'
3
3
  end
@@ -1,39 +1,72 @@
1
1
  require 'test_helper'
2
2
  require 'gds_api/asset_manager'
3
3
  require 'gds_api/test_helpers/asset_manager'
4
+ require 'json'
4
5
 
5
6
  describe GdsApi::AssetManager do
6
7
  include GdsApi::TestHelpers::AssetManager
7
8
 
8
- before do
9
- @base_api_url = Plek.current.find("asset-manager")
10
- @api = GdsApi::AssetManager.new(@base_api_url)
11
- end
9
+ let(:base_api_url) { Plek.current.find('asset-manager') }
10
+ let(:api) { GdsApi::AssetManager.new(base_api_url) }
12
11
 
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)
12
+ let(:file_fixture) { load_fixture_file("hello.txt") }
17
13
 
18
- file = load_fixture_file("hello.txt")
19
- response = @api.create_asset(:file => file)
14
+ let(:asset_url) { [base_api_url, "assets", asset_id].join("/") }
15
+ let(:asset_id) { "new-asset-id" }
20
16
 
21
- assert_equal "http://asset-manager.dev.gov.uk/assets/51278b2b686c82076c000003", response.asset.id
22
- end
17
+ let(:asset_manager_response) {
18
+ {
19
+ asset: {
20
+ id: asset_url,
21
+ }
22
+ }
23
+ }
23
24
 
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" })
25
+ it "creates an asset with a file" do
26
+ req = stub_request(:post, "#{base_api_url}/assets").
27
+ with(:body => %r{Content\-Disposition: form\-data; name="asset\[file\]"; filename="hello\.txt"\r\nContent\-Type: text/plain}).
28
+ to_return(:body => JSON.dump(asset_manager_response), :status => 201)
26
29
 
27
- asset = @api.asset("test-id")
30
+ response = api.create_asset(:file => file_fixture)
28
31
 
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
+ assert_equal asset_url, response.asset.id
33
+ assert_requested(req)
32
34
  end
33
35
 
34
36
  it "returns nil when an asset does not exist" do
35
37
  asset_manager_does_not_have_an_asset("not-really-here")
36
38
 
37
- assert_nil @api.asset("not-really-here")
39
+ assert_nil api.asset("not-really-here")
40
+ end
41
+
42
+ describe "an asset exists" do
43
+ before do
44
+ asset_manager_has_an_asset(
45
+ asset_id,
46
+ "name" => "photo.jpg",
47
+ "content_type" => "image/jpeg",
48
+ "file_url" => "http://fooey.gov.uk/media/photo.jpg",
49
+ )
50
+ end
51
+
52
+ let(:asset_id) { "test-id" }
53
+
54
+ it "updates an asset with a file" do
55
+ req = stub_request(:put, "http://asset-manager.dev.gov.uk/assets/test-id").
56
+ to_return(:body => JSON.dump(asset_manager_response), :status => 200)
57
+
58
+ response = api.update_asset(asset_id, :file => file_fixture)
59
+
60
+ assert_equal "#{base_api_url}/assets/#{asset_id}", response.asset.id
61
+ assert_requested(req)
62
+ end
63
+
64
+ it "retrieves an asset" do
65
+ asset = api.asset(asset_id)
66
+
67
+ assert_equal "photo.jpg", asset.name
68
+ assert_equal "image/jpeg", asset.content_type
69
+ assert_equal "http://fooey.gov.uk/media/photo.jpg", asset.file_url
70
+ end
38
71
  end
39
72
  end
@@ -695,6 +695,35 @@ class JsonClientTest < MiniTest::Spec
695
695
  end
696
696
  end
697
697
 
698
+ # EXACTLY the same as the post_multipart tests
699
+ def test_client_can_put_multipart_responses
700
+ url = "http://some.endpoint/some.json"
701
+ stub_request(:put, url).
702
+ with(:body => %r{Content\-Disposition: form\-data; name="a"\r\n\r\n123}, :headers => {'Content-Type' => %r{multipart/form-data; boundary=\d+}}).
703
+ to_return(:body => '{"b": "1"}', :status => 200)
704
+
705
+ response = @client.put_multipart("http://some.endpoint/some.json", {"a" => "123"})
706
+ assert_equal "1", response["b"]
707
+ end
708
+
709
+ def test_put_multipart_should_raise_exception_if_not_found
710
+ url = "http://some.endpoint/some.json"
711
+ stub_request(:put, url).to_return(:body => '', :status => 404)
712
+
713
+ assert_raises GdsApi::HTTPNotFound do
714
+ @client.put_multipart("http://some.endpoint/some.json", {"a" => "123"})
715
+ end
716
+ end
717
+
718
+ def test_put_multipart_should_raise_error_responses
719
+ url = "http://some.endpoint/some.json"
720
+ stub_request(:put, url).to_return(:body => '', :status => 500)
721
+
722
+ assert_raises GdsApi::HTTPErrorResponse do
723
+ @client.put_multipart("http://some.endpoint/some.json", {"a" => "123"})
724
+ end
725
+ end
726
+
698
727
  def test_should_raise_error_if_attempting_to_disable_timeout
699
728
  assert_raises RuntimeError do
700
729
  GdsApi::JsonClient.new(:disable_timeout => true)
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.8.0
4
+ version: 10.9.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-04-14 00:00:00.000000000 Z
12
+ date: 2014-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: plek
@@ -376,7 +376,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
376
376
  version: '0'
377
377
  segments:
378
378
  - 0
379
- hash: 1895053804403919519
379
+ hash: -553660564906139153
380
380
  required_rubygems_version: !ruby/object:Gem::Requirement
381
381
  none: false
382
382
  requirements:
@@ -385,7 +385,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
385
385
  version: '0'
386
386
  segments:
387
387
  - 0
388
- hash: 1895053804403919519
388
+ hash: -553660564906139153
389
389
  requirements: []
390
390
  rubyforge_project:
391
391
  rubygems_version: 1.8.23