Sha256: dffbf305c5371fb616f8c887bd99cc0bc74dcccb0b368fb43573e7c8811ed267

Contents?: true

Size: 953 Bytes

Versions: 43

Compression:

Stored size: 953 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)
        @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

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

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
discorb-0.12.4 lib/discorb/image.rb
discorb-0.12.3 lib/discorb/image.rb
discorb-0.12.2 lib/discorb/image.rb
discorb-0.12.1 lib/discorb/image.rb
discorb-0.12.0 lib/discorb/image.rb
discorb-0.11.4 lib/discorb/image.rb
discorb-0.11.3 lib/discorb/image.rb
discorb-0.11.2 lib/discorb/image.rb
discorb-0.11.1 lib/discorb/image.rb
discorb-0.11.0 lib/discorb/image.rb
discorb-0.10.3 lib/discorb/image.rb
discorb-0.10.2 lib/discorb/image.rb
discorb-0.10.1 lib/discorb/image.rb
discorb-0.10.0 lib/discorb/image.rb
discorb-0.9.6 lib/discorb/image.rb
discorb-0.9.5 lib/discorb/image.rb
discorb-0.9.4 lib/discorb/image.rb
discorb-0.9.3 lib/discorb/image.rb
discorb-0.9.1 lib/discorb/image.rb
discorb-0.9.0 lib/discorb/image.rb