Class: GdsApi::AssetManager

Inherits:
Base
  • Object
show all
Defined in:
lib/gds_api/asset_manager.rb

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#client, #create_client, #get_list, #initialize, #url_for_slug

Constructor Details

This class inherits a constructor from GdsApi::Base

Instance Method Details

#asset(id) ⇒ GdsApi::Response?

Fetches an asset’s metadata given the id

Parameters:

  • id (String)

    The asset identifier (a UUID).

Returns:

  • (GdsApi::Response, nil)

    A response object containing the parsed JSON response. If the asset cannot be found, nil wil be returned.

Raises:



89
90
91
# File 'lib/gds_api/asset_manager.rb', line 89

def asset(id)
  get_json("#{base_url}/assets/#{id}")
end

#create_asset(asset) ⇒ GdsApi::Response

Creates an asset given a hash with one file attribute

Makes a POST request to the asset manager api to create an asset.

The asset must be provided as a Hash with a file attribute that behaves like a File object. The content-type that the asset manager will subsequently serve will be based on the file’s extension (derived from #path). If you supply a content-type via, for example ActionDispatch::Http::UploadedFile or another multipart wrapper, it will be ignored. To provide a content-type directly you must be specify it as a content_type attribute of the hash.

Examples:

Upload a file from disk

response = asset_manager.create_asset(file: File.new('image.jpg', 'r'))
response['id']           #=> "http://asset-manager.dev.gov.uk/assets/576bbc52759b74196b000012"
response['content_type'] #=> "image/jpeg"

Upload a file from a Rails param, (typically a multipart wrapper)

params[:file] #=> #<ActionDispatch::Http::UploadedFile:0x007fc60b43c5c8
                  # @content_type="application/foofle",
                  # @original_filename="cma_case_image.jpg",
                  # @tempfile="spec/support/images/cma_case_image.jpg">

# Though we sent a file with a +content_type+ of 'application/foofle',
# this was ignored
response = asset_manager.create_asset(file: params[:file])
response['content_type'] #=> "image/jpeg"

Parameters:

  • asset (Hash)

    The attributes for the asset to send to the api. Must contain file, which behaves like a File. All other attributes will be ignored.

Returns:

  • (GdsApi::Response)

    The wrapped http response from the api. Behaves both as a Hash and an OpenStruct, and responds to the following:

    :id           the URL of the asset
    :name         the filename of the asset that will be served
    :content_type the content_type of the asset
    :file_url     the URL from which the asset will be served when it has
                  passed a virus scan
    :state        One of 'unscanned', 'clean', or 'infected'. Unless the state is
                  'clean' the asset at the :file_url will 404
    

Raises:



47
48
49
# File 'lib/gds_api/asset_manager.rb', line 47

def create_asset(asset)
  post_multipart("#{base_url}/assets", asset:)
end

#delete_asset(id) ⇒ GdsApi::Response

Deletes an asset given an id

Makes a DELETE request to the asset manager api to delete an asset.

Examples:

Delete a file from disk

uuid = '594602dd-75b3-4e6f-b5d1-cacf8c4d4164'
asset_manager.delete_asset(uuid)

Parameters:

  • id (String)

    The asset identifier (a UUID).

Returns:

  • (GdsApi::Response)

    The wrapped http response from the api. Behaves both as a Hash and an OpenStruct, and responds to the following:

    :id           the URL of the asset
    :name         the filename of the asset that will be served
    :content_type the content_type of the asset
    :file_url     the URL from which the asset will be served when it has
                  passed a virus scan
    :state        One of 'unscanned', 'clean', or 'infected'. Unless the state is
                  'clean' the asset at the :file_url will 404
    

Raises:



112
113
114
# File 'lib/gds_api/asset_manager.rb', line 112

def delete_asset(id)
  delete_json("#{base_url}/assets/#{id}")
end

#media(id, filename) ⇒ GdsApi::Response

Fetches an asset given the id and filename

Parameters:

  • id (String)

    The asset identifier.

  • filename (String)

    Filename of the asset.

Returns:

  • (GdsApi::Response)

    A response object containing the raw asset. If the asset cannot be found, GdsApi::HTTPNotFound will be raised.

Raises:



158
159
160
# File 'lib/gds_api/asset_manager.rb', line 158

def media(id, filename)
  get_raw("#{base_url}/media/#{id}/#{filename}")
end

#restore_asset(id) ⇒ GdsApi::Response

Restores an asset given an id

Makes a POST request to the asset manager api to restore an asset.

Examples:

Restore a deleted file

uuid = '594602dd-75b3-4e6f-b5d1-cacf8c4d4164'
asset_manager.restore_asset(uuid)

Parameters:

  • id (String)

    The asset identifier (a UUID).

Returns:

  • (GdsApi::Response)

    The wrapped http response from the api. Behaves both as a Hash and an OpenStruct, and responds to the following:

    :id           the URL of the asset
    :name         the filename of the asset that will be served
    :content_type the content_type of the asset
    :file_url     the URL from which the asset will be served when it has
                  passed a virus scan
    :state        One of 'unscanned', 'clean', or 'infected'. Unless the state is
                  'clean' the asset at the :file_url will 404
    

Raises:



135
136
137
# File 'lib/gds_api/asset_manager.rb', line 135

def restore_asset(id)
  post_json("#{base_url}/assets/#{id}/restore")
end

#update_asset(id, asset) ⇒ GdsApi::Response

Updates an asset given a hash with one file attribute

Makes a PUT request to the asset manager api to update an asset.

The asset must be provided as a Hash with a file attribute that behaves like a File object. The content-type of the file will be based on the files extension unless you specify a content_type attribute of the hash to set it.

Examples:

Update a file from disk

uuid = '594602dd-75b3-4e6f-b5d1-cacf8c4d4164'
asset_manager.update_asset(uuid, file: File.new('image.jpg', 'r'))

Parameters:

  • id (String)

    The asset identifier (a UUID).

  • asset (Hash)

    The attributes for the asset to send to the api. Must contain file, which behaves like a File. All other attributes will be ignored.

Returns:

  • (GdsApi::Response)

    The wrapped http response from the api. Behaves both as a Hash and an OpenStruct, and responds to the following:

    :id           the URL of the asset
    :name         the filename of the asset that will be served
    :content_type the content_type of the asset
    :file_url     the URL from which the asset will be served when it has
                  passed a virus scan
    :state        One of 'unscanned', 'clean', or 'infected'. Unless the state is
                  'clean' the asset at the :file_url will 404
    

Raises:



78
79
80
# File 'lib/gds_api/asset_manager.rb', line 78

def update_asset(id, asset)
  put_multipart("#{base_url}/assets/#{id}", asset:)
end

#whitehall_media(legacy_url_path) ⇒ GdsApi::Response

Fetches a Whitehall asset given the legacy URL path

Parameters:

  • legacy_url_path (String)

    The Whitehall asset identifier.

Returns:

  • (GdsApi::Response)

    A response object containing the raw asset. If the asset cannot be found, GdsApi::HTTPNotFound will be raised.

Raises:



146
147
148
# File 'lib/gds_api/asset_manager.rb', line 146

def whitehall_media(legacy_url_path)
  get_raw("#{base_url}/#{uri_encode(legacy_url_path)}")
end