Class: RubyCord::Attachment
- Inherits:
-
Object
- Object
- RubyCord::Attachment
- Defined in:
- lib/rubycord/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 ⇒ RubyCord::Snowflake
readonly
The attachment id.
-
#image? ⇒ Boolean
readonly
return [Boolean] The filename of the attachment.
-
#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) ⇒ RubyCord::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 ⇒ String
Object class and attributes.
Constructor Details
#initialize(source, filename = nil, description: nil, content_type: nil, will_close: true) ⇒ Attachment
Creates a new attachment.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rubycord/attachment.rb', line 53 def initialize( source, filename = nil, description: nil, content_type: nil, will_close: true ) @io = (source.respond_to?(:read) ? source : File.open(source, "rb")) @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.
17 18 19 |
# File 'lib/rubycord/attachment.rb', line 17 def content_type @content_type end |
#created_by ⇒ :client, :discord (readonly)
Returns The attachment was created by.
35 36 37 |
# File 'lib/rubycord/attachment.rb', line 35 def created_by @created_by end |
#description ⇒ String (readonly)
Returns The attachment description.
19 20 21 |
# File 'lib/rubycord/attachment.rb', line 19 def description @description end |
#filename ⇒ String (readonly)
Returns The attachment filename.
15 16 17 |
# File 'lib/rubycord/attachment.rb', line 15 def filename @filename end |
#height ⇒ Integer? (readonly)
30 31 32 |
# File 'lib/rubycord/attachment.rb', line 30 def height @height end |
#id ⇒ RubyCord::Snowflake (readonly)
Returns The attachment id.
21 22 23 |
# File 'lib/rubycord/attachment.rb', line 21 def id @id end |
#image? ⇒ Boolean (readonly)
return [Boolean] The filename of the attachment.
|
# File 'lib/rubycord/attachment.rb', line 40
|
#io ⇒ #read (readonly)
Returns The file content.
13 14 15 |
# File 'lib/rubycord/attachment.rb', line 13 def io @io end |
#proxy_url ⇒ String (readonly)
Returns The attachment proxy url.
27 28 29 |
# File 'lib/rubycord/attachment.rb', line 27 def proxy_url @proxy_url end |
#size ⇒ Integer (readonly)
Returns The attachment size in bytes.
23 24 25 |
# File 'lib/rubycord/attachment.rb', line 23 def size @size end |
#url ⇒ String (readonly)
Returns The attachment url.
25 26 27 |
# File 'lib/rubycord/attachment.rb', line 25 def url @url end |
#width ⇒ Integer? (readonly)
33 34 35 |
# File 'lib/rubycord/attachment.rb', line 33 def width @width end |
Class Method Details
.from_string(string, filename = nil, content_type: nil, description: nil) ⇒ RubyCord::Attachment
Creates a new file from a string.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rubycord/attachment.rb', line 120 def self.from_string( string, filename = nil, content_type: nil, description: nil ) io = StringIO.new(string) filename ||= "#{string.object_id}.txt" new( io, filename, content_type:, description:, will_close: true ) end |
Instance Method Details
#inspect ⇒ String
Returns Object class and attributes.
93 94 95 96 97 98 99 |
# File 'lib/rubycord/attachment.rb', line 93 def inspect if @created_by == :discord "<#{self.class} #{@id}: #{@filename}>" else "<#{self.class} #{io.fileno}: #{@filename}>" end end |