Class: Discorb::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/discorb/attachment.rb

Overview

Represents a attachment file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, filename = nil, description: nil, content_type: nil, will_close: true) -> Attachment

Creates a new attachment.

Parameters:

  • source (#read, String)

    The Source of the attachment.

  • filename (String) (defaults to: nil)

    The filename of the attachment. If not set, path or object_id of the IO is used.

  • description (String) (defaults to: nil)

    The description of the attachment.

  • content_type (String) (defaults to: nil)

    The content type of the attachment. If not set, it is guessed from the filename. If failed to guess, it is set to application/octet-stream.

  • will_close (Boolean) (defaults to: true)

    Whether the IO will be closed after the attachment is sent.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/discorb/attachment.rb', line 51

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.

Returns:

  • (String)

    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.

Returns:

  • (:client, :discord)

    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.

Returns:

  • (String)

    The attachment description.



18
19
20
# File 'lib/discorb/attachment.rb', line 18

def description
  @description
end

#filename -> String (readonly)

Returns The attachment filename.

Returns:

  • (String)

    The attachment filename.



14
15
16
# File 'lib/discorb/attachment.rb', line 14

def filename
  @filename
end

#height -> Integer? (readonly)

Returns:

  • (Integer)

    The image height.

  • (nil)

    If the attachment is not an image.



29
30
31
# File 'lib/discorb/attachment.rb', line 29

def height
  @height
end

#id -> Discorb::Snowflake (readonly)

Returns The attachment id.

Returns:



20
21
22
# File 'lib/discorb/attachment.rb', line 20

def id
  @id
end

#image? -> Boolean (readonly)

Returns whether the file is an image.

Returns:

  • (Boolean)

    whether the file is an image.



# File 'lib/discorb/attachment.rb', line 38

#io -> #read (readonly)

Returns The file content.

Returns:

  • (#read)

    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.

Returns:

  • (String)

    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.

Returns:

  • (Integer)

    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.

Returns:

  • (String)

    The attachment url.



24
25
26
# File 'lib/discorb/attachment.rb', line 24

def url
  @url
end

#width -> Integer? (readonly)

Returns:

  • (Integer)

    The image width.

  • (nil)

    If the attachment is not an image.



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) -> File

Creates a new file from a string.

Parameters:

  • string (String)

    The string to create the file from.

  • filename (String) (defaults to: nil)

    The filename of the file. object_id of the string is used if not set.

  • content_type (String) (defaults to: nil)

    The content type of the file. If not set, it is guessed from the filename.

Returns:

  • (File)

    The new file.



98
99
100
101
102
# File 'lib/discorb/attachment.rb', line 98

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