Class: SynapsePayRest::PhysicalDocument
- 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
-
.byte_stream_to_base64(byte_stream, mime_type) ⇒ Object
Converts the supplied image byte stream to padded base64.
-
.create(type:, **options) ⇒ SynapsePayRest::Document
Creates a document instance but does not submit it to the API.
-
.file_to_base64(file_path) ⇒ Object
Converts the supplied image file to padded base64.
-
.url_to_base64(url) ⇒ Object
Converts the supplied image url to padded base64.
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
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.
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:, **) if [:file_path] value = self.file_to_base64([:file_path]) elsif [:url] value = self.url_to_base64([:url]) elsif [:byte_stream] value = self.byte_stream_to_base64([:byte_stream], [:mime_type]) elsif [:value] value = [:value] end super(type: type, value: value) end |
.file_to_base64(file_path) ⇒ Object
Converts the supplied image file to padded base64
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
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 |