Class: PdfServices::Base::Operation

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

Constant Summary collapse

BASE_ENDPOINT =
'https://pdf-services-ue1.adobe.io/operation/'
ASSETS_ENDPOINT =
'https://pdf-services-ue1.adobe.io/assets/'
STATUS =
{
  in_progress: 'in progress',
  done: 'done',
  failed: 'failed'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Operation

Returns a new instance of Operation.



14
15
16
# File 'lib/pdfservices/operations/base.rb', line 14

def initialize(api)
  @api = api
end

Instance Method Details

#handle_response(response, asset, &block) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/pdfservices/operations/base.rb', line 36

def handle_response(response, asset, &block)
  unless response.status == 201
    raise "Unexpected response status from operation endpoint: #{response.status}, #{response.body}"
  end

  document_url = response.headers['location']
  poll_document_result document_url, asset, &block
end

#poll_document_result(url, original_asset, &block) ⇒ Object



30
31
32
33
34
# File 'lib/pdfservices/operations/base.rb', line 30

def poll_document_result(url, original_asset, &block)
  response = @api.get(url)
  json_response = JSON.parse(response.body.to_s)
  handle_polling_result(url, json_response, original_asset, &block)
end

#upload_asset(asset) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pdfservices/operations/base.rb', line 18

def upload_asset(asset)
  if asset.respond_to?(:read) && asset.respond_to?(:eof?)
    tempfile = Tempfile.new(['blob', SecureRandom.uuid])
    tempfile.write(asset.read(1024)) until asset.eof?
    tempfile.rewind
    asset = tempfile
  elsif asset.is_a?(String) && File.exist?(asset)
    asset = File.open(asset, 'rb')
  end
  Asset.new(@api).upload(asset)
end