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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/discorb/attachment.rb', line 52

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_typeString (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

#descriptionString (readonly)

Returns The attachment description.

Returns:

  • (String)

    The attachment description.



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

def description
  @description
end

#filenameString (readonly)

Returns The attachment filename.

Returns:

  • (String)

    The attachment filename.



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

def filename
  @filename
end

#heightInteger? (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

#idDiscorb::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 39

#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_urlString (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

#sizeInteger (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

#urlString (readonly)

Returns The attachment url.

Returns:

  • (String)

    The attachment url.



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

def url
  @url
end

#widthInteger? (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) ⇒ Discorb::Attachment

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:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/discorb/attachment.rb', line 117

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: content_type,
    description: description,
    will_close: true
  )
end

Instance Method Details

#inspectObject



90
91
92
93
94
95
96
# File 'lib/discorb/attachment.rb', line 90

def inspect
  if @created_by == :discord
    "<#{self.class} #{@id}: #{@filename}>"
  else
    "<#{self.class} #{io.fileno}: #{@filename}>"
  end
end