Class: OmniAI::Chat::Media

Inherits:
Content
  • Object
show all
Defined in:
lib/omniai/chat/media.rb

Overview

An abstract class that represents audio / image / video and is used for both files and urls.

Direct Known Subclasses

File, URL

Defined Under Namespace

Classes: TypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Content

deserialize, #serialize, summarize

Constructor Details

#initialize(type) ⇒ Media

Returns a new instance of Media.

Parameters:

  • type (String)

    “audio/flac”, “image/jpeg”, “video/mpeg”, etc.



13
14
15
16
# File 'lib/omniai/chat/media.rb', line 13

def initialize(type)
  super()
  @type = type
end

Instance Attribute Details

#typeSymbol, String

Returns:

  • (Symbol, String)


10
11
12
# File 'lib/omniai/chat/media.rb', line 10

def type
  @type
end

Instance Method Details

#audio?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/omniai/chat/media.rb', line 29

def audio?
  @type.match?(%r{^audio/})
end

#dataString

e.g. “Hello” -> “SGVsbG8h”

Returns:

  • (String)


57
58
59
# File 'lib/omniai/chat/media.rb', line 57

def data
  Base64.strict_encode64(fetch!)
end

#data_uriString

e.g. “data:text/html;base64,…”

Returns:

  • (String)


64
65
66
# File 'lib/omniai/chat/media.rb', line 64

def data_uri
  "data:#{@type};base64,#{data}"
end

#fetch!String

Returns:

  • (String)

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/omniai/chat/media.rb', line 69

def fetch!
  raise NotImplementedError, "#{self.class}#fetch! undefined"
end

#image?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/omniai/chat/media.rb', line 34

def image?
  @type.match?(%r{^image/})
end

#kind:video, ...

Returns:

  • (:video, :audio, :image, :text)


44
45
46
47
48
49
50
51
52
# File 'lib/omniai/chat/media.rb', line 44

def kind
  if text? then :text
  elsif audio? then :audio
  elsif image? then :image
  elsif video? then :video
  else
    raise(TypeError, "unsupported type=#{@type}")
  end
end

#summarizeString

Returns:

  • (String)


19
20
21
# File 'lib/omniai/chat/media.rb', line 19

def summarize
  "[#{filename}]"
end

#text?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/omniai/chat/media.rb', line 24

def text?
  @type.match?(%r{^text/})
end

#video?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/omniai/chat/media.rb', line 39

def video?
  @type.match?(%r{^video/})
end