Class: TurnKit::MediaInput

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#filenameObject (readonly)

Returns the value of attribute filename.



25
26
27
# File 'lib/turnkit/media_input.rb', line 25

def filename
  @filename
end

#metadataObject (readonly)

Returns the value of attribute metadata.



25
26
27
# File 'lib/turnkit/media_input.rb', line 25

def 
  @metadata
end

#mime_typeObject (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

#sourceObject (readonly)

Returns the value of attribute source.



25
26
27
# File 'lib/turnkit/media_input.rb', line 25

def source
  @source
end

#source_typeObject (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, **options)
  value.is_a?(self) && options.empty? ? value : new(value, **options)
end

Instance Method Details

#attachment_sourceObject



75
76
77
78
79
80
81
82
# File 'lib/turnkit/media_input.rb', line 75

def attachment_source
  case source_type
  when "bytes"
    StringIO.new(source)
  else
    source
  end
end

#byte_sizeObject



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

#kindObject



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

#pathObject



71
72
73
# File 'lib/turnkit/media_input.rb', line 71

def path
  source.to_s if source_type == "path"
end

#to_hObject



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

#urlObject



67
68
69
# File 'lib/turnkit/media_input.rb', line 67

def url
  source.to_s if source_type == "url"
end