闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.3 KiB

  1. # frozen_string_literal: true
  2. class RSSBuilder
  3. class ItemBuilder
  4. def initialize
  5. @item = Ox::Element.new('item')
  6. end
  7. def title(str)
  8. @item << (Ox::Element.new('title') << str)
  9. self
  10. end
  11. def link(str)
  12. @item << Ox::Element.new('guid').tap do |guid|
  13. guid['isPermalink'] = 'true'
  14. guid << str
  15. end
  16. @item << (Ox::Element.new('link') << str)
  17. self
  18. end
  19. def pub_date(date)
  20. @item << (Ox::Element.new('pubDate') << date.to_formatted_s(:rfc822))
  21. self
  22. end
  23. def description(str)
  24. @item << (Ox::Element.new('description') << str)
  25. self
  26. end
  27. def enclosure(url, type, size)
  28. @item << Ox::Element.new('enclosure').tap do |enclosure|
  29. enclosure['url'] = url
  30. enclosure['length'] = size
  31. enclosure['type'] = type
  32. end
  33. self
  34. end
  35. def to_element
  36. @item
  37. end
  38. end
  39. def initialize
  40. @document = Ox::Document.new(version: '1.0')
  41. @channel = Ox::Element.new('channel')
  42. @document << (rss << @channel)
  43. end
  44. def title(str)
  45. @channel << (Ox::Element.new('title') << str)
  46. self
  47. end
  48. def link(str)
  49. @channel << (Ox::Element.new('link') << str)
  50. self
  51. end
  52. def image(str)
  53. @channel << Ox::Element.new('image').tap do |image|
  54. image << (Ox::Element.new('url') << str)
  55. image << (Ox::Element.new('title') << '')
  56. image << (Ox::Element.new('link') << '')
  57. end
  58. @channel << (Ox::Element.new('webfeeds:icon') << str)
  59. self
  60. end
  61. def cover(str)
  62. @channel << Ox::Element.new('webfeeds:cover').tap do |cover|
  63. cover['image'] = str
  64. end
  65. self
  66. end
  67. def logo(str)
  68. @channel << (Ox::Element.new('webfeeds:logo') << str)
  69. self
  70. end
  71. def accent_color(str)
  72. @channel << (Ox::Element.new('webfeeds:accentColor') << str)
  73. self
  74. end
  75. def description(str)
  76. @channel << (Ox::Element.new('description') << str)
  77. self
  78. end
  79. def item
  80. @channel << ItemBuilder.new.tap do |item|
  81. yield item
  82. end.to_element
  83. self
  84. end
  85. def to_xml
  86. ('<?xml version="1.0" encoding="UTF-8"?>' + Ox.dump(@document, effort: :tolerant)).force_encoding('UTF-8')
  87. end
  88. private
  89. def rss
  90. Ox::Element.new('rss').tap do |rss|
  91. rss['version'] = '2.0'
  92. rss['xmlns:webfeeds'] = 'http://webfeeds.org/rss/1.0'
  93. end
  94. end
  95. end