Class: Discorb::Attachment
- Inherits:
-
Object
- Object
- Discorb::Attachment
- Defined in:
- lib/discorb/attachment.rb
Overview
Represents a attachment file.
Instance Attribute Summary collapse
-
#content_type -> String
readonly
The attachment content type.
-
#created_by -> :client, :discord
readonly
The attachment was created by.
-
#description -> String
readonly
The attachment description.
-
#filename -> String
readonly
The attachment filename.
- #height -> Integer? readonly
-
#id -> Discorb::Snowflake
readonly
The attachment id.
-
#image? -> Boolean
readonly
Whether the file is an image.
-
#io -> #read
readonly
The file content.
-
#proxy_url -> String
readonly
The attachment proxy url.
-
#size -> Integer
readonly
The attachment size in bytes.
-
#url -> String
readonly
The attachment url.
- #width -> Integer? readonly
Class Method Summary collapse
-
.from_string(string, filename = nil, content_type: nil, description: nil) -> Discorb::Attachment
Creates a new file from a string.
Instance Method Summary collapse
-
#initialize(source, filename = nil, description: nil, content_type: nil, will_close: true) -> Attachment
constructor
Creates a new attachment.
- #inspect -> Object
Constructor Details
#initialize(source, filename = nil, description: nil, content_type: nil, will_close: true) -> Attachment
Creates a new attachment.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/discorb/attachment.rb', line 52 def initialize(source, filename = nil, description: nil, content_type: nil, will_close: true) @io = if source.respond_to?(:read) source else File.open(source, "rb") end @filename = filename || (@io.respond_to?(:path) ? @io.path : @io.object_id) @description = description @content_type = content_type || MIME::Types.type_for(@filename.to_s)[0].to_s @content_type = "application/octet-stream" if @content_type == "" @will_close = will_close @created_by = :client end |
Instance Attribute Details
#content_type -> String (readonly)
Returns The attachment content type.
16 17 18 |
# File 'lib/discorb/attachment.rb', line 16 def content_type @content_type end |
#created_by -> :client, :discord (readonly)
Returns The attachment was created by.
34 35 36 |
# File 'lib/discorb/attachment.rb', line 34 def created_by @created_by end |
#description -> String (readonly)
Returns The attachment description.
18 19 20 |
# File 'lib/discorb/attachment.rb', line 18 def description @description end |
#filename -> String (readonly)
Returns The attachment filename.
14 15 16 |
# File 'lib/discorb/attachment.rb', line 14 def filename @filename end |
#height -> Integer? (readonly)
29 30 31 |
# File 'lib/discorb/attachment.rb', line 29 def height @height end |
#id -> Discorb::Snowflake (readonly)
Returns The attachment id.
20 21 22 |
# File 'lib/discorb/attachment.rb', line 20 def id @id end |
#image? -> Boolean (readonly)
Returns whether the file is an image.
|
# File 'lib/discorb/attachment.rb', line 39
|
#io -> #read (readonly)
Returns The file content.
12 13 14 |
# File 'lib/discorb/attachment.rb', line 12 def io @io end |
#proxy_url -> String (readonly)
Returns The attachment proxy url.
26 27 28 |
# File 'lib/discorb/attachment.rb', line 26 def proxy_url @proxy_url end |
#size -> Integer (readonly)
Returns The attachment size in bytes.
22 23 24 |
# File 'lib/discorb/attachment.rb', line 22 def size @size end |
#url -> String (readonly)
Returns The attachment url.
24 25 26 |
# File 'lib/discorb/attachment.rb', line 24 def url @url end |
#width -> Integer? (readonly)
32 33 34 |
# File 'lib/discorb/attachment.rb', line 32 def width @width end |
Class Method Details
.from_string(string, filename = nil, content_type: nil, description: nil) -> Discorb::Attachment
Creates a new file from a string.
113 114 115 116 117 |
# File 'lib/discorb/attachment.rb', line 113 def self.from_string(string, filename = nil, content_type: nil, description: nil) io = StringIO.new(string) filename ||= string.object_id.to_s + ".txt" new(io, filename, content_type: content_type, description: description, will_close: true) end |
Instance Method Details
#inspect -> Object
86 87 88 89 90 91 92 |
# File 'lib/discorb/attachment.rb', line 86 def inspect if @created_by == :discord "<#{self.class} #{@id}: #{@filename}>" else "<#{self.class} #{io.fileno}: #{@filename}>" end end |