Module: QboApi::Attachment

Included in:
QboApi
Defined in:
lib/qbo_api/attachment.rb

Instance Method Summary collapse

Instance Method Details

#attachment_connectionObject



36
37
38
# File 'lib/qbo_api/attachment.rb', line 36

def attachment_connection
  @attachment_connection ||= authorized_multipart_connection(endpoint_url)
end

#delete_attachment(attachable:) ⇒ Object

The ‘attachable` must be the full payload returned in the read response



27
28
29
30
31
32
33
34
# File 'lib/qbo_api/attachment.rb', line 27

def delete_attachment(attachable:)
  raw_response = connection.post do |request|
    request.url "#{realm_id}/attachable?operation=delete"
    request.body = attachable.to_json
  end

  response(raw_response, entity: :attachable)
end

#read_attachment(id:) ⇒ Object



4
5
6
7
8
9
# File 'lib/qbo_api/attachment.rb', line 4

def read_attachment(id:)
  raw_response = connection.get do |request|
    request.url "#{realm_id}/attachable/#{id}"
  end
  response(raw_response, entity: :attachable)
end

#upload_attachment(payload:, attachment:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/qbo_api/attachment.rb', line 11

def upload_attachment(payload:, attachment:)
  content_type = payload['ContentType'] || payload[:ContentType]
  file_name = payload['FileName'] || payload[:FileName]
  raw_response = attachment_connection.post do |request|
    request.url "#{realm_id}/upload"
    request.body = {
        'file_metadata_01':
            Faraday::UploadIO.new(StringIO.new(payload.to_json), 'application/json', 'attachment.json'),
        'file_content_01':
            Faraday::UploadIO.new(attachment, content_type, file_name)
    }
  end
  response(raw_response, entity: :attachable)
end