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”
63
64
65
|
# File 'lib/omniai/chat/media.rb', line 63
def data
Base64.strict_encode64(fetch!)
end
|
#data_uri ⇒ String
e.g. “data:text/html;base64,…”
70
71
72
|
# File 'lib/omniai/chat/media.rb', line 70
def data_uri
"data:#{@type};base64,#{data}"
end
|
#document? ⇒ Boolean
44
45
46
|
# File 'lib/omniai/chat/media.rb', line 44
def document?
@type.match?(%r{^application/pdf$})
end
|
#fetch! ⇒ String
75
76
77
|
# File 'lib/omniai/chat/media.rb', line 75
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 ⇒ :text, ...
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/omniai/chat/media.rb', line 49
def kind
if text? then :text
elsif audio? then :audio
elsif image? then :image
elsif video? then :video
elsif document? then :document
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
|