Class: Intelligence::MessageContent::File

Inherits:
Base
  • Object
show all
Defined in:
lib/intelligence/message_content/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ File

Returns a new instance of File.



11
12
13
14
# File 'lib/intelligence/message_content/file.rb', line 11

def initialize( attributes )
  @uri = URI( attributes[ :uri ] ) if attributes[ :uri ]
  @content_type = attributes[ :content_type ]
end

Instance Method Details

#content_typeObject



16
17
18
# File 'lib/intelligence/message_content/file.rb', line 16

def content_type 
  @content_type ||= valid_uri? ? MIME::Types.type_for( @uri.path )&.first&.content_type : nil
end

#to_hObject



28
29
30
# File 'lib/intelligence/message_content/file.rb', line 28

def to_h
  { type: :file, content_type: content_type, uri: @uri.to_s }
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/intelligence/message_content/file.rb', line 24

def valid?
  valid_uri? && !MIME::Types[ content_type ].empty?
end

#valid_uri?(schemes = [ 'http', 'https' ]) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/intelligence/message_content/file.rb', line 20

def valid_uri?( schemes = [ 'http', 'https' ] )
  !!( @uri && schemes.include?( @uri.scheme ) && @uri.path && !@uri.path.empty? )      
end