Class: MijDiscord::Data::Embed

Inherits:
Object
  • Object
show all
Defined in:
lib/mij-discord/data/embed.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Embed

Returns a new instance of Embed.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mij-discord/data/embed.rb', line 31

def initialize(data)
  @type, @url = data['type'], data['url']
  @title, @description = data['title'], data['description']

  @color = ColorRGB.new(data['color']) if data['color']
  @timestamp = Time.parse(data['timestamp']).utc if data['timestamp']

  @footer = EmbedFooter.new(data['footer']) if data['footer']
  @thumbnail = EmbedMedia.new(data['thumbnail']) if data['thumbnail']

  @image = EmbedMedia.new(data['image']) if data['image']
  @video = EmbedMedia.new(data['video']) if data['video']

  @author = EmbedAuthor.new(data['author']) if data['author']
  @provider = EmbedProvider.new(data['provider']) if data['provider']

  @fields = data['fields']&.map {|x| EmbedField.new(x) }
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



27
28
29
# File 'lib/mij-discord/data/embed.rb', line 27

def author
  @author
end

#colorObject (readonly)

Returns the value of attribute color.



15
16
17
# File 'lib/mij-discord/data/embed.rb', line 15

def color
  @color
end

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/mij-discord/data/embed.rb', line 9

def description
  @description
end

#fieldsObject (readonly)

Returns the value of attribute fields.



29
30
31
# File 'lib/mij-discord/data/embed.rb', line 29

def fields
  @fields
end

Returns the value of attribute footer.



17
18
19
# File 'lib/mij-discord/data/embed.rb', line 17

def footer
  @footer
end

#imageObject (readonly)

Returns the value of attribute image.



21
22
23
# File 'lib/mij-discord/data/embed.rb', line 21

def image
  @image
end

#providerObject (readonly)

Returns the value of attribute provider.



25
26
27
# File 'lib/mij-discord/data/embed.rb', line 25

def provider
  @provider
end

#thumbnailObject (readonly)

Returns the value of attribute thumbnail.



19
20
21
# File 'lib/mij-discord/data/embed.rb', line 19

def thumbnail
  @thumbnail
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



13
14
15
# File 'lib/mij-discord/data/embed.rb', line 13

def timestamp
  @timestamp
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/mij-discord/data/embed.rb', line 7

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/mij-discord/data/embed.rb', line 5

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/mij-discord/data/embed.rb', line 11

def url
  @url
end

#videoObject (readonly)

Returns the value of attribute video.



23
24
25
# File 'lib/mij-discord/data/embed.rb', line 23

def video
  @video
end

Class Method Details

.construct(data) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mij-discord/data/embed.rb', line 79

def self.construct(data)
  embed = {
    'type' => data.try_keys(:type, 'type') || :rich,
    'title' => data.try_keys(:title, 'title'),
    'description' => data.try_keys(:description, 'description'),
    'url' => data.try_keys(:url, 'url'),

    'color' => data.try_keys(:color, 'color')&.to_i,
    'timestamp' => data.try_keys(:timestamp, 'timestamp')&.iso8601,

    'footer' => data.try_keys(:footer, 'footer')&.to_hash,
    'thumbnail' => data.try_keys(:thumbnail, 'thumbnail')&.to_hash,

    'image' => data.try_keys(:image, 'image')&.to_hash,
    'video' => data.try_keys(:video, 'video')&.to_hash,

    'author' => data.try_keys(:author, 'author')&.to_hash,
    'provider' => data.try_keys(:provider, 'provider')&.to_hash,

    'fields' => data.try_keys(:fields, 'fields')&.map(&:to_hash),
  }.delete_if {|_,v| v.nil? }

  embed
end

Instance Method Details

#inspectObject



50
51
52
53
54
# File 'lib/mij-discord/data/embed.rb', line 50

def inspect
  MijDiscord.make_inspect(self,
    :type, :title, :description, :url, :color, :timestamp, :image,
    :video, :thumbnail, :footer, :author, :provider, :fields)
end

#to_hashObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mij-discord/data/embed.rb', line 56

def to_hash
  self.class.construct({
    type: @type,
    title: @title,
    description: @description,
    url: @url,

    color: @color,
    timestamp: @timestamp,

    footer: @footer,
    thumbnail: @thumbnail,

    image: @image,
    video: @video,

    author: @author,
    provider: @provider,

    fields: @fields,
  })
end