class SynapsePayRest::PhysicalDocument

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

@see docs.synapsepay.com/docs/user-resources#section-physical-document-types

physical document types

Public Class Methods

byte_stream_to_base64(byte_stream, mime_type) click to toggle source

Converts the supplied image byte stream to padded base64

# 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) click to toggle source

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.

@note This should only be called on subclasses of Document, not on

Document itself.

@param type [String] @param value [String] (optional) padded base64-encoded image (“data:#{mime_type};base64,#{base64}”) @param file_path [String] (optional) path to image file @param url [String] (optional) image file url @param byte_stream [String] (optional) byte representation of image @param mime_type [String] (optional) mime type of byte_stream (e.g. 'image/png')

@return [SynapsePayRest::Document]

@see docs.synapsepay.com/docs/user-resources#section-physical-document-types physical document types @see docs.synapsepay.com/docs/user-resources#section-social-document-types social document types @see docs.synapsepay.com/docs/user-resources#section-virtual-document-types virtual document types

Calls superclass method SynapsePayRest::Document::create
# 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) click to toggle source

Converts the supplied image file to padded base64

# 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) click to toggle source

Converts the supplied image url to padded base64

# 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