Class: OmniAI::Chat::Media
Overview
An abstract class that represents audio / image / video and is used for both files and urls.
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.
13
14
15
16
|
# File 'lib/omniai/chat/media.rb', line 13
def initialize(type)
super()
@type = type
end
|
Instance Attribute Details
#type ⇒ Symbol, String
10
11
12
|
# File 'lib/omniai/chat/media.rb', line 10
def type
@type
end
|
Instance Method Details
#audio? ⇒ Boolean
29
30
31
|
# File 'lib/omniai/chat/media.rb', line 29
def audio?
@type.match?(%r{^audio/})
end
|
#data ⇒ String
e.g. “Hello” -> “SGVsbG8h”
57
58
59
|
# File 'lib/omniai/chat/media.rb', line 57
def data
Base64.strict_encode64(fetch!)
end
|
#data_uri ⇒ String
e.g. “data:text/html;base64,…”
64
65
66
|
# File 'lib/omniai/chat/media.rb', line 64
def data_uri
"data:#{@type};base64,#{data}"
end
|
#fetch! ⇒ String
69
70
71
|
# File 'lib/omniai/chat/media.rb', line 69
def fetch!
raise NotImplementedError, "#{self.class}#fetch! undefined"
end
|
#image? ⇒ Boolean
34
35
36
|
# File 'lib/omniai/chat/media.rb', line 34
def image?
@type.match?(%r{^image/})
end
|
#kind ⇒ :video, ...
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
|
#summarize ⇒ String
19
20
21
|
# File 'lib/omniai/chat/media.rb', line 19
def summarize
"[#{filename}]"
end
|
#text? ⇒ Boolean
24
25
26
|
# File 'lib/omniai/chat/media.rb', line 24
def text?
@type.match?(%r{^text/})
end
|
#video? ⇒ Boolean
39
40
41
|
# File 'lib/omniai/chat/media.rb', line 39
def video?
@type.match?(%r{^video/})
end
|