Class: Discorb::Image
- Inherits:
-
Object
- Object
- Discorb::Image
- Defined in:
- lib/discorb/image.rb
Overview
Represents an image.
Instance Method Summary collapse
-
#initialize(source, type = nil) ⇒ Image
constructor
Initializes a new Image.
-
#to_s ⇒ String
Formats the image as a Discord style.
Constructor Details
#initialize(source, type = nil) ⇒ Image
Initializes a new 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.
36 37 38 |
# File 'lib/discorb/image.rb', line 36 def to_s "data:#{@type};base64,#{Base64.strict_encode64(@bytes)}" end |