Class: Discorb::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/discorb/image.rb

Overview

Represents an image.

Instance Method Summary collapse

Constructor Details

#initialize(source, type = nil) ⇒ Image

Initializes a new Image.

Parameters:

  • source (#read, String)

    The IO source or path of the image.

  • type (String) (defaults to: nil)

    The MIME type of the image.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/discorb/image.rb', line 17

def initialize(source, type = nil)
  if source.respond_to?(:read)
    @io = source
    @type = type || MIME::Types.type_for(source.path).first.content_type
  elsif ::File.exist?(source)
    @io = ::File.open(source, "rb")
    @type = MIME::Types.type_for(source).first.to_s
  else
    raise ArgumentError, "Couldn't read file."
  end
end

Instance Method Details

#inspectObject



38
39
40
# File 'lib/discorb/image.rb', line 38

def inspect
  "#<#{self.class} #{@type}>"
end

#to_sString

Formats the image as a Discord style.

Returns:

  • (String)

    The image as a Discord style.



34
35
36
# File 'lib/discorb/image.rb', line 34

def to_s
  "data:#{@type};base64,#{Base64.strict_encode64(@io.read)}"
end