Module: OmniAI::Google::Chat::MediaSerializer

Defined in:
lib/omniai/google/chat/media_serializer.rb

Overview

Overrides media serialize / deserialize.

Class Method Summary collapse

Class Method Details

.serialize(media) ⇒ Hash

Parameters:

  • media (OmniAI::Chat::Media)

Returns:

  • (Hash)


35
36
37
38
39
40
41
# File 'lib/omniai/google/chat/media_serializer.rb', line 35

def self.serialize(media, *)
  if media.is_a?(OmniAI::Chat::URL) && use_file_data?(URI.parse(media.uri))
    serialize_as_file_data(media)
  else
    serialize_as_inline_data(media)
  end
end

.serialize_as_file_data(media) ⇒ hash

Parameters:

  • media (OmniAI::Chat::Media)

Returns:

  • (hash)


11
12
13
14
15
16
17
18
# File 'lib/omniai/google/chat/media_serializer.rb', line 11

def self.serialize_as_file_data(media)
  {
    fileData: {
      mimeType: media.type,
      fileUri: media.uri,
    },
  }
end

.serialize_as_inline_data(media) ⇒ hash

Parameters:

  • media (OmniAI::Chat::Media)

Returns:

  • (hash)


23
24
25
26
27
28
29
30
# File 'lib/omniai/google/chat/media_serializer.rb', line 23

def self.serialize_as_inline_data(media)
  {
    inlineData: {
      mimeType: media.type,
      data: media.data,
    },
  }
end

.use_file_data?(uri) ⇒ Boolean

Parameters:

  • uri (URI)

Returns:

  • (Boolean)


46
47
48
# File 'lib/omniai/google/chat/media_serializer.rb', line 46

def self.use_file_data?(uri)
  uri.host.eql?('generativelanguage.googleapis.com') || uri.scheme.eql?('gs')
end