Class: Discorb::Embed

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

Overview

Represents an embed of discord.

Defined Under Namespace

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

Instance Attribute 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, data: 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 (Discorb::Color) (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 (Discorb::Embed::Author) (defaults to: nil)

    The author field of embed.

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

    The fields of embed.

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

    The footer of embed.

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

    The image of embed.

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

    The thumbnail of embed.



47
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
74
75
76
77
78
79
# File 'lib/discorb/embed.rb', line 47

def initialize(title = nil, description = nil, color: nil, url: nil, timestamp: nil, author: nil,
                                               fields: nil, footer: nil, image: nil, thumbnail: nil, data: nil)
  if data.nil?
    @title = title
    @description = description
    @url = url
    @timestamp = timestamp
    @color = 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"
  else
    @title = data[:title]
    @description = data[:description]
    @url = data[:url]
    @timestamp = data[:timestamp] && Time.iso8601(data[:timestamp])
    @type = data[:type]
    @color = data[:color] && Color.new(data[:color])
    @footer = data[:footer] && Footer.new(data[:footer][:text], icon: data[:footer][:icon_url])
    @author = if data[:author]
        Author.new(data[:author][:name], icon: data[:author][:icon_url],
                                         url: data[:author][:url])
      end
    @thumbnail = data[:thumbnail] && Thumbnail.new(data[:thumbnail])
    @image = data[:image] && Image.new(data[:image])
    @video = data[:video] && Video.new(data[:video])
    @provider = data[:provider] && Provider.new(data[:provider])
    @fields = data[:fields] ? data[:fields].map { |f| Field.new(f[:name], f[:value], inline: f[:inline]) } : []
  end
end

Instance Attribute Details

#author -> Discorb::Embed::Author?

Returns The author of embed.

Returns:



19
20
21
# File 'lib/discorb/embed.rb', line 19

def author
  @author
end

#color -> Discorb::Color?

Returns The color of embed.

Returns:



17
18
19
# File 'lib/discorb/embed.rb', line 17

def color
  @color
end

#description -> String?

Returns The description of embed.

Returns:

  • (String, nil)

    The description of embed.



11
12
13
# File 'lib/discorb/embed.rb', line 11

def description
  @description
end

#fields -> Array<Discorb::Embed::Field>

Returns The fields of embed.

Returns:



21
22
23
# File 'lib/discorb/embed.rb', line 21

def fields
  @fields
end

Returns The footer of embed.

Returns:



23
24
25
# File 'lib/discorb/embed.rb', line 23

def footer
  @footer
end

#image -> Discorb::Embed::Image

Returns The image of embed.

Returns:



28
29
30
# File 'lib/discorb/embed.rb', line 28

def image
  @image
end

#thumbnail -> Discorb::Embed::Thumbnail

Returns The thumbnail of embed.

Returns:



28
29
30
# File 'lib/discorb/embed.rb', line 28

def thumbnail
  @thumbnail
end

#timestamp -> Time?

Returns The timestamp of embed.

Returns:

  • (Time, nil)

    The timestamp of embed.



15
16
17
# File 'lib/discorb/embed.rb', line 15

def timestamp
  @timestamp
end

#title -> String?

Returns The title of embed.

Returns:

  • (String, nil)

    The title of embed.



9
10
11
# File 'lib/discorb/embed.rb', line 9

def title
  @title
end

#type -> Symbol (readonly)

Returns The type of embed.

Returns:

  • (Symbol)

    The type of embed.



25
26
27
# File 'lib/discorb/embed.rb', line 25

def type
  @type
end

#url -> String?

Returns The url of embed.

Returns:

  • (String, nil)

    The url of embed.



13
14
15
# File 'lib/discorb/embed.rb', line 13

def url
  @url
end

Instance Method Details

#to_hash -> Hash

Convert embed to hash.

Returns:

  • (Hash)

    Converted embed.

See Also:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/discorb/embed.rb', line 95

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 { |f| f.to_hash } if @fields.any?
  ret
end