Class: TurnKit::MediaInput
- Inherits:
-
Object
- Object
- TurnKit::MediaInput
- Defined in:
- lib/turnkit/media_input.rb
Constant Summary collapse
- SUPPORTED_MIME_TYPES =
%w[image/png image/jpeg image/webp image/gif application/pdf].freeze
- EXTENSION_MIME_TYPES =
{ ".png" => "image/png", ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".webp" => "image/webp", ".gif" => "image/gif", ".pdf" => "application/pdf", ".mp3" => "audio/mpeg", ".wav" => "audio/wav", ".m4a" => "audio/mp4", ".mp4" => "video/mp4", ".mov" => "video/quicktime", ".webm" => "video/webm" }.freeze
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#mime_type ⇒ Object
readonly
Returns the value of attribute mime_type.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#source_type ⇒ Object
readonly
Returns the value of attribute source_type.
Class Method Summary collapse
Instance Method Summary collapse
- #attachment_source ⇒ Object
- #byte_size ⇒ Object
-
#initialize(source, mime_type: nil, filename: nil, metadata: {}, source_type: nil) ⇒ MediaInput
constructor
A new instance of MediaInput.
- #kind ⇒ Object
- #path ⇒ Object
- #to_h ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(source, mime_type: nil, filename: nil, metadata: {}, source_type: nil) ⇒ MediaInput
Returns a new instance of MediaInput.
35 36 37 38 39 40 41 42 43 |
# File 'lib/turnkit/media_input.rb', line 35 def initialize(source, mime_type: nil, filename: nil, metadata: {}, source_type: nil) @source = source @source_type = (source_type || infer_source_type).to_s @filename = filename || infer_filename @mime_type = mime_type || infer_mime_type @metadata = || {} validate! end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
25 26 27 |
# File 'lib/turnkit/media_input.rb', line 25 def filename @filename end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
25 26 27 |
# File 'lib/turnkit/media_input.rb', line 25 def @metadata end |
#mime_type ⇒ Object (readonly)
Returns the value of attribute mime_type.
25 26 27 |
# File 'lib/turnkit/media_input.rb', line 25 def mime_type @mime_type end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
25 26 27 |
# File 'lib/turnkit/media_input.rb', line 25 def source @source end |
#source_type ⇒ Object (readonly)
Returns the value of attribute source_type.
25 26 27 |
# File 'lib/turnkit/media_input.rb', line 25 def source_type @source_type end |
Class Method Details
.bytes(data, mime_type:, filename: nil, metadata: {}) ⇒ Object
31 32 33 |
# File 'lib/turnkit/media_input.rb', line 31 def self.bytes(data, mime_type:, filename: nil, metadata: {}) new(data, source_type: :bytes, mime_type: mime_type, filename: filename, metadata: ) end |
.wrap(value, **options) ⇒ Object
27 28 29 |
# File 'lib/turnkit/media_input.rb', line 27 def self.wrap(value, **) value.is_a?(self) && .empty? ? value : new(value, **) end |
Instance Method Details
#attachment_source ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/turnkit/media_input.rb', line 75 def case source_type when "bytes" StringIO.new(source) else source end end |
#byte_size ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/turnkit/media_input.rb', line 54 def byte_size case source_type when "path" File.size(source.to_s) if File.file?(source.to_s) when "bytes" source.bytesize when "io" source.size if source.respond_to?(:size) when "active_storage" active_storage_byte_size end end |
#kind ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/turnkit/media_input.rb', line 45 def kind return "image" if mime_type&.start_with?("image/") return "audio" if mime_type&.start_with?("audio/") return "video" if mime_type&.start_with?("video/") return "pdf" if mime_type == "application/pdf" nil end |
#path ⇒ Object
71 72 73 |
# File 'lib/turnkit/media_input.rb', line 71 def path source.to_s if source_type == "path" end |
#to_h ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/turnkit/media_input.rb', line 84 def to_h { "kind" => kind, "mime_type" => mime_type, "filename" => filename, "byte_size" => byte_size, "url" => url, "path" => path, "metadata" => }.compact end |
#url ⇒ Object
67 68 69 |
# File 'lib/turnkit/media_input.rb', line 67 def url source.to_s if source_type == "url" end |