embed.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # frozen_string_literal: true
  2. require_relative 'concerns/serializable'
  3. class Embed
  4. attr_accessor :title,
  5. :description,
  6. :url,
  7. :color,
  8. :timestamp,
  9. :footer,
  10. :thumbnail,
  11. :image,
  12. :author,
  13. :fields
  14. include Serializable
  15. class Footer
  16. attr_accessor :icon_url, :text
  17. include Serializable
  18. def to_hash
  19. {
  20. icon_url: icon_url,
  21. text: text
  22. }
  23. end
  24. end
  25. class Image
  26. attr_accessor :url
  27. include Serializable
  28. def to_hash
  29. {
  30. url: url
  31. }
  32. end
  33. end
  34. class Author
  35. attr_accessor :name, :url, :icon_url
  36. include Serializable
  37. def to_hash
  38. {
  39. name: name,
  40. url: url,
  41. icon_url: icon_url
  42. }
  43. end
  44. end
  45. class Field
  46. attr_accessor :name, :value, :inline
  47. include Serializable
  48. def to_hash
  49. {
  50. name: name,
  51. value: value,
  52. inline: inline
  53. }
  54. end
  55. end
  56. def to_hash
  57. hash = {
  58. title: title,
  59. description: description,
  60. url: url,
  61. color: int_color,
  62. timestamp: timestamp
  63. }
  64. hash[:footer] = footer if footer
  65. hash[:thumbnail] = thumbnail if thumbnail
  66. hash[:image] = image if image
  67. hash[:author] = author if author
  68. hash[:fields] = fields if fields
  69. hash
  70. end
  71. def int_color
  72. return nil unless color
  73. hex = color.to_s.sub(/\A#/, '0x')
  74. Integer(hex)
  75. end
  76. end