builder.rb 720 B

123456789101112131415161718192021222324252627282930313233
  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. ('<?xml version="1.0" encoding="UTF-8"?>'.dup << 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 << Ox::Element.new('rss').tap do |rss|
  19. rss['version'] = '2.0'
  20. rss['xmlns:webfeeds'] = 'http://webfeeds.org/rss/1.0'
  21. rss['xmlns:media'] = 'http://search.yahoo.com/mrss/'
  22. rss << @dsl.to_element
  23. end
  24. end
  25. end
  26. end