Class: SynapsePayRest::PhysicalDocument

Inherits:
Document
  • Object
show all
Defined in:
lib/synapse_pay_rest/models/user/physical_document.rb

Overview

Represents physical documents that can be added to a base document.

Instance Attribute Summary

Attributes inherited from Document

#base_document, #document_value, #id, #last_updated, #meta, #status, #type, #value

Class Method Summary collapse

Methods inherited from Document

#==, from_response, #initialize, #to_hash

Constructor Details

This class inherits a constructor from SynapsePayRest::Document

Class Method Details

.byte_stream_to_base64(byte_stream, mime_type) ⇒ Object

Converts the supplied image byte stream to padded base64



64
65
66
67
# File 'lib/synapse_pay_rest/models/user/physical_document.rb', line 64

def byte_stream_to_base64(byte_stream, mime_type)
  base64 = Base64.encode64(byte_stream)
  "data:#{mime_type};base64,#{base64}"
end

.create(type:, **options) ⇒ SynapsePayRest::Document

Note:

This should only be called on subclasses of Document, not on Document itself.

Creates a document instance but does not submit it to the API. Use BaseDocument#create/#update/#add_physical_documents or related methods to submit the document to the API.

Parameters:

  • type (String)
  • value (String)

    (optional) padded base64-encoded image (“data:#mime_type;base64,#base64”)

  • file_path (String)

    (optional) path to image file

  • url (String)

    (optional) image file url

  • byte_stream (String)

    (optional) byte representation of image

  • mime_type (String)

    (optional) mime type of byte_stream (e.g. ‘image/png’)

Returns:

See Also:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/synapse_pay_rest/models/user/physical_document.rb', line 30

def create(type:, **options)
  if options[:file_path]
    value = self.file_to_base64(options[:file_path])
  elsif options[:url]
    value = self.url_to_base64(options[:url])
  elsif options[:byte_stream]
    value = self.byte_stream_to_base64(options[:byte_stream], options[:mime_type])
  elsif options[:value]
    value = options[:value]
  end

  super(type: type, value: value)
end

.file_to_base64(file_path) ⇒ Object

Converts the supplied image file to padded base64

Raises:

  • (ArgumentError)


58
59
60
61
# File 'lib/synapse_pay_rest/models/user/physical_document.rb', line 58

def file_to_base64(file_path)
  raise ArgumentError, 'file_path must be a String' unless file_path.is_a?(String)
  url_to_base64(file_path)
end

.url_to_base64(url) ⇒ Object

Converts the supplied image url to padded base64

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/synapse_pay_rest/models/user/physical_document.rb', line 45

def url_to_base64(url)
  raise ArgumentError, 'url must be a String' unless url.is_a?(String)
  byte_stream = open(url).read
  begin
    # remove any query params to get the mime type
    mime_type = MIME::Types.type_for(url.gsub(/\?.*$/, '')).first.content_type
  rescue
    mime_type = nil
  end
  byte_stream_to_base64(byte_stream, mime_type)
end