Class: Goatless::RssBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/goatless/rss_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ RssBuilder

Returns a new instance of RssBuilder.



4
5
6
# File 'lib/goatless/rss_builder.rb', line 4

def initialize(items)
  @items = items
end

Instance Method Details

#documentObject



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
34
35
36
37
38
# File 'lib/goatless/rss_builder.rb', line 8

def document
  #TODO: Move number of items in feed into settings
  items = @items.sort { |x, y| x.pub_date <=> y.pub_date }.last(100).reverse
  
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |rss|
    rss.rss(:version => '2.0') {
      rss.channel {
        rss.title "Friends"
        rss.generator "Goatless"
        rss.description "Goatless friends"
        rss.link "https://github.com/semka/goatless"

        items.each do |i|
          rss.item {
            rss.pubDate i.pub_date.strftime("%a, %d %b %Y %H:%M:%S %z") # to RFC 822
            rss.author  i.author
            rss.link    i.permalink
            if i.public?
              rss.title   i.title
              rss.description do |d| d << i.body end
            else
              rss.title  "Private post"
              rss.description "Private post"
            end
          }
        end
      }
    }
  end
  builder.to_xml
end