Class: Itrp::Attachments

Inherits:
Object
  • Object
show all
Defined in:
lib/itrp/client/attachments.rb

Constant Summary collapse

AWS_PROVIDER =
'aws'
FILENAME_TEMPLATE =
'${filename}'

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Attachments

Returns a new instance of Attachments.



7
8
9
# File 'lib/itrp/client/attachments.rb', line 7

def initialize(client)
  @client = client
end

Instance Method Details

#upload_attachments!(path, data) ⇒ Object

upload the attachments in :attachments to ITRP and return the data with the uploaded attachment info



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/itrp/client/attachments.rb', line 12

def upload_attachments!(path, data)
  raise_exceptions = !!data.delete(:attachments_exception)
  attachments = [data.delete(:attachments)].flatten.compact
  return if attachments.empty?

  # retrieve the upload configuration for this record from ITRP
  storage = @client.get(path =~ /\d+$/ ? path : "#{path}/new", {attachment_upload_token: true}, @client.send(:expand_header))[:storage_upload]
  report_error("Attachments not allowed for #{path}", raise_exceptions) and return unless storage

  # upload each attachment and store the {key, filesize} has in the note_attachments parameter
  data[attachments_field(path)] = attachments.map {|attachment| upload_attachment(storage, attachment, raise_exceptions) }.compact.to_json
end