1
0

builder.rb 849 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # frozen_string_literal: true
  2. class RSS::Builder
  3. attr_reader :dsl
  4. def self.build
  5. new.tap do |builder|
  6. yield builder.dsl
  7. end.to_xml
  8. end
  9. def initialize
  10. @dsl = RSS::Channel.new
  11. end
  12. def to_xml
  13. Ox.dump(wrap_in_document, effort: :tolerant).force_encoding('UTF-8')
  14. end
  15. private
  16. def wrap_in_document
  17. Ox::Document.new(version: '1.0').tap do |document|
  18. document << xml_instruct
  19. document << Ox::Element.new('rss').tap do |rss|
  20. rss['version'] = '2.0'
  21. rss['xmlns:webfeeds'] = 'http://webfeeds.org/rss/1.0'
  22. rss['xmlns:media'] = 'http://search.yahoo.com/mrss/'
  23. rss << @dsl.to_element
  24. end
  25. end
  26. end
  27. def xml_instruct
  28. Ox::Instruct.new(:xml).tap do |instruct|
  29. instruct[:version] = '1.0'
  30. instruct[:encoding] = 'UTF-8'
  31. end
  32. end
  33. end