Sha256: 318a94b37dce08bf0d70bd45d961f0d70a4c49684605adecf9ff64d6d0154605

Contents?: true

Size: 1.04 KB

Versions: 13

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require "base64"
require "mime/types"

module Discorb
  #
  # Represents an image.
  #
  class Image
    #
    # Initializes a new Image.
    #
    # @param [#read] source The source of the image.
    # @param [String] type The MIME type of the image.
    # @overload
    #   @param [String] source The file path of the source.
    #   @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

13 entries across 13 versions & 1 rubygems

Version Path
discorb-0.2.4 lib/discorb/image.rb
discorb-0.2.3 lib/discorb/image.rb
discorb-0.2.1 lib/discorb/image.rb
discorb-0.2.0 lib/discorb/image.rb
discorb-0.1.0 lib/discorb/image.rb
discorb-0.0.8 lib/discorb/image.rb
discorb-0.0.7 lib/discorb/image.rb
discorb-0.0.6 lib/discorb/image.rb
discorb-0.0.5 lib/discorb/image.rb
discorb-0.0.4 lib/discorb/image.rb
discorb-0.0.3 lib/discorb/image.rb
discorb-0.0.2 lib/discorb/image.rb
discorb-0.0.1 lib/discorb/image.rb