Class: RubyCord::Embed

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycord/embed.rb

Overview

Represents an embed of discord.

Defined Under Namespace

Classes: Author, Field, Footer, Image, Provider, Thumbnail, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, description = nil, color: nil, url: nil, timestamp: nil, author: nil, fields: nil, footer: nil, image: nil, thumbnail: nil) ⇒ Embed

Initialize a new Embed object.

Parameters:

  • title (String) (defaults to: nil)

    The title of embed.

  • description (String) (defaults to: nil)

    The description of embed.

  • color (RubyCord::Color, Integer) (defaults to: nil)

    The color of embed.

  • url (String) (defaults to: nil)

    The url of embed.

  • timestamp (Time) (defaults to: nil)

    The timestamp of embed.

  • author (RubyCord::Embed::Author) (defaults to: nil)

    The author field of embed.

  • fields (Array<RubyCord::Embed::Field>) (defaults to: nil)

    The fields of embed.

  • footer (RubyCord::Embed::Footer) (defaults to: nil)

    The footer of embed.

  • image (RubyCord::Embed::Image, String) (defaults to: nil)

    The image of embed.

  • thumbnail (RubyCord::Embed::Thumbnail, String) (defaults to: nil)

    The thumbnail of embed.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubycord/embed.rb', line 48

def initialize(
  title = nil,
  description = nil,
  color: nil,
  url: nil,
  timestamp: nil,
  author: nil,
  fields: nil,
  footer: nil,
  image: nil,
  thumbnail: nil
)
  @title = title
  @description = description
  @url = url
  @timestamp = timestamp
  @color = color && (color.is_a?(Color) ? color : Color.new(color))
  @author = author
  @fields = fields || []
  @footer = footer
  @image = image && (image.is_a?(String) ? Image.new(image) : image)
  @thumbnail =
    thumbnail &&
      (thumbnail.is_a?(String) ? Thumbnail.new(thumbnail) : thumbnail)
  @type = :rich
end

Instance Attribute Details

#authorRubyCord::Embed::Author?

Returns The author of embed.

Returns:



20
21
22
# File 'lib/rubycord/embed.rb', line 20

def author
  @author
end

#colorRubyCord::Color?

Returns The color of embed.

Returns:



18
19
20
# File 'lib/rubycord/embed.rb', line 18

def color
  @color
end

#descriptionString?

Returns The description of embed.

Returns:

  • (String, nil)

    The description of embed.



12
13
14
# File 'lib/rubycord/embed.rb', line 12

def description
  @description
end

#fieldsArray<RubyCord::Embed::Field>

Returns The fields of embed.

Returns:



22
23
24
# File 'lib/rubycord/embed.rb', line 22

def fields
  @fields
end

Returns The footer of embed.

Returns:



24
25
26
# File 'lib/rubycord/embed.rb', line 24

def footer
  @footer
end

#imageRubyCord::Embed::Image

Returns The image of embed.

Returns:



29
30
31
# File 'lib/rubycord/embed.rb', line 29

def image
  @image
end

#thumbnailRubyCord::Embed::Thumbnail

Returns The thumbnail of embed.

Returns:



29
30
31
# File 'lib/rubycord/embed.rb', line 29

def thumbnail
  @thumbnail
end

#timestampTime?

Returns The timestamp of embed.

Returns:

  • (Time, nil)

    The timestamp of embed.



16
17
18
# File 'lib/rubycord/embed.rb', line 16

def timestamp
  @timestamp
end

#titleString?

Returns The title of embed.

Returns:

  • (String, nil)

    The title of embed.



10
11
12
# File 'lib/rubycord/embed.rb', line 10

def title
  @title
end

#typeSymbol (readonly)

Returns The type of embed.

Returns:

  • (Symbol)

    The type of embed.



26
27
28
# File 'lib/rubycord/embed.rb', line 26

def type
  @type
end

#urlString?

Returns The url of embed.

Returns:

  • (String, nil)

    The url of embed.



14
15
16
# File 'lib/rubycord/embed.rb', line 14

def url
  @url
end

Class Method Details

.from_hash(data) ⇒ Object



148
149
150
151
152
# File 'lib/rubycord/embed.rb', line 148

def self.from_hash(data)
  inst = allocate
  inst.initialize_hash(data)
  inst
end

Instance Method Details

#inspectObject



123
124
125
# File 'lib/rubycord/embed.rb', line 123

def inspect
  "#<#{self.class} \"#{@title}\">"
end

#to_hashHash

Convert embed to hash.

Returns:

  • (Hash)

    Converted embed.

See Also:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rubycord/embed.rb', line 133

def to_hash
  ret = { type: "rich" }
  ret[:title] = @title if @title
  ret[:description] = @description if @description
  ret[:url] = @url if @url
  ret[:timestamp] = @timestamp&.iso8601 if @timestamp
  ret[:color] = @color&.to_i if @color
  ret[:footer] = @footer&.to_hash if @footer
  ret[:image] = @image&.to_hash if @image
  ret[:thumbnail] = @thumbnail&.to_hash if @thumbnail
  ret[:author] = @author&.to_hash if @author
  ret[:fields] = @fields&.map(&:to_hash) if @fields.any?
  ret
end