Class: PdfServices::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfservices/asset.rb

Constant Summary collapse

ASSETS_ENDPOINT =
'https://pdf-services-ue1.adobe.io/assets'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, id = nil) ⇒ Asset

Returns a new instance of Asset.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/pdfservices/asset.rb', line 7

def initialize(api, id = nil)
  # MimeMagic can't detect docx files, and will return `application/zip` so we need to add it manually
  MimeMagic.add('application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                magic: [[0..2000, 'word/']])
  raise ArgumentError, 'Api is nil' unless api

  @api = api
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/pdfservices/asset.rb', line 5

def id
  @id
end

Instance Method Details

#delete_assetObject

Raises:



45
46
47
48
49
# File 'lib/pdfservices/asset.rb', line 45

def delete_asset
  raise AssetError, 'Asset ID is nil' unless @id

  @api.delete("#{ASSETS_ENDPOINT}/#{@id}")
end

#download(asset_id = nil) ⇒ Object

Raises:



34
35
36
37
38
39
40
41
42
43
# File 'lib/pdfservices/asset.rb', line 34

def download(asset_id = nil)
  raise AssetError, 'Asset ID is nil' unless @id || asset_id
  raise AssetError, "Asset ID is not a string, is a #{@id.class}" unless (@id || asset_id).respond_to?(:to_s)

  @id = asset_id if asset_id

  url = presigned_url(:download)
  download_uri = url['downloadUri']
  @api.get(download_uri)
end

#upload(file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pdfservices/asset.rb', line 17

def upload(file)
  url = presigned_url(file:)
  upload_uri = url['uploadUri']
  asset_id = url['assetID']

  response = @api.put(upload_uri, body: File.new(file), headers: upload_headers(File.new(file)))

  unless response.status == 200
    raise AssetError,
          "Something went wrong when trying to upload the file: #{response.body.inspect}"
  end

  @id = asset_id

  self
end