Module: Readymade::Model::ApiAttachable

Extended by:
ActiveSupport::Concern
Defined in:
lib/readymade/model/api_attachable.rb

Instance Method Summary collapse

Instance Method Details

#api_attachable_format?(attachment_file) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/readymade/model/api_attachable.rb', line 44

def api_attachable_format?(attachment_file)
  attachment_file.respond_to?(:key?) && attachment_file.key?('base64') && attachment_file.key?('filename')
end

#api_attachment_to_uploaded(attachment_file) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/readymade/model/api_attachable.rb', line 48

def api_attachment_to_uploaded(attachment_file)
  ActionDispatch::Http::UploadedFile.new(
    tempfile: Tempfile.new(attachment_file[:filename]).tap do |tf|
      tf.binmode
      tf.write(Base64.decode64(attachment_file[:base64].split('base64,')[-1]))
    end,
    filename: attachment_file[:filename],
    type: Mime::Type.lookup_by_extension(File.extname(attachment_file[:filename])[1..]).to_s
  )
end