class Discorb::Image

Represents an image.

Public Class Methods

new(source, type = nil) click to toggle source

Initializes a new Image.

@param [#read, String] source The IO source or path of the image. @param [String] type The MIME type of the image.

# 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

Public Instance Methods

to_s() click to toggle source

Formats the image as a Discord style.

@return [String] The image as a Discord style.

# File lib/discorb/image.rb, line 36
def to_s
  "data:#{@type};base64,#{Base64.strict_encode64(@bytes)}"
end