~cytrogen/masto-fe

ref: 4d1b67f664e463f28ff45b8e125998ffcd2de50b masto-fe/app/lib/rss/builder.rb -rw-r--r-- 720 bytes
4d1b67f6 — Renaud Chaput Add end-to-end (system) tests (#25461) 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

class RSS::Builder
  attr_reader :dsl

  def self.build
    new.tap do |builder|
      yield builder.dsl
    end.to_xml
  end

  def initialize
    @dsl = RSS::Channel.new
  end

  def to_xml
    ('<?xml version="1.0" encoding="UTF-8"?>'.dup << Ox.dump(wrap_in_document, effort: :tolerant)).force_encoding('UTF-8')
  end

  private

  def wrap_in_document
    Ox::Document.new(version: '1.0').tap do |document|
      document << Ox::Element.new('rss').tap do |rss|
        rss['version']        = '2.0'
        rss['xmlns:webfeeds'] = 'http://webfeeds.org/rss/1.0'
        rss['xmlns:media']    = 'http://search.yahoo.com/mrss/'

        rss << @dsl.to_element
      end
    end
  end
end