Sha256: dd9ab4df544a70af12a07e35cb28aa245fea29bc6f6a8cc0d841da1aa58e7a77

Contents?: true

Size: 961 Bytes

Versions: 14

Compression:

Stored size: 961 Bytes

Contents

# frozen_string_literal: true

require "base64"
require "mime/types"

module Discorb
  #
  # Represents an image.
  #
  class Image
    #
    # 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.
    #
    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

    #
    # Formats the image as a Discord style.
    #
    # @return [String] The image as a Discord style.
    #
    def to_s
      "data:#{@type};base64,#{Base64.strict_encode64(@io.read)}"
    end

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
discorb-0.19.0 lib/discorb/image.rb
discorb-0.18.1 lib/discorb/image.rb
discorb-0.18.0 lib/discorb/image.rb
discorb-0.17.1 lib/discorb/image.rb
discorb-0.17.0 lib/discorb/image.rb
discorb-0.16.0 lib/discorb/image.rb
discorb-0.15.1 lib/discorb/image.rb
discorb-0.15.0 lib/discorb/image.rb
discorb-0.14.0 lib/discorb/image.rb
discorb-0.13.4 lib/discorb/image.rb
discorb-0.13.3 lib/discorb/image.rb
discorb-0.13.2 lib/discorb/image.rb
discorb-0.13.1 lib/discorb/image.rb
discorb-0.13.0 lib/discorb/image.rb