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
28
29
# File 'lib/discorb/image.rb', line 17

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

Instance Method Details

#to_s -> String

Formats the image as a Discord style.

Returns:

  • (String)

    The image as a Discord style.



36
37
38
# File 'lib/discorb/image.rb', line 36

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