Class: Glue::Template::Feed

Inherits:
Base
  • Object
show all
Defined in:
lib/glue/template/feed.rb

Constant Summary collapse

RSS_TIME_FORMAT =
"%a, %d %b %Y %H:%M:%S %z"

Instance Attribute Summary

Attributes inherited from Base

#config, #filters, #items

Instance Method Summary collapse

Methods inherited from Base

#<<, #apply_filters, #initialize

Constructor Details

This class inherits a constructor from Glue::Template::Base

Instance Method Details

#save(path) ⇒ Object



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/glue/template/feed.rb', line 6

def save(path)
  # create a file handler
  file = File.open(path, "w+")
  
  # sort and keep latest 10 items
  self.filters = config[:feed][:filters]
  
  filtered_items = apply_filters
  filtered_items = filtered_items.sort_by {|options| options[:updated_at].to_i }
  filtered_items.reverse!
  filtered_items = filtered_items[0,10]
  
  # instantiate builder
  xml = Builder::XmlMarkup.new(:target => file, :indent => 2)
  xml.instruct! :xml, :version => "1.1", :encoding => "UTF-8"
  
  feed_opts = {
    "xmlns" => "http://www.w3.org/2005/Atom",
    "xml:lang" => config[:site][:language],
    "xml:base" => "http://www.example.org"
  }
  
  xml.feed(feed_opts) do
    # set feed id to url
    xml.id config[:site][:base_url]
    
    # set title
    xml.title config[:site][:title]
    
    # set description
    xml.subtitle config[:site][:description]
    
    # set link to site
    xml.link(:href => config[:site][:base_url])
    
    # set link to feed
    xml.link(:href => File.join(config[:site][:base_url], "feed.xml"), :rel => "self")
    
    # add update date as the saving time 
    xml.updated Time.now.xmlschema
    
    # add generator info
    xml.generator("Glue", :uri => "http://github.com/fnando/glue", :version => Glue::VERSION)
    
    # set language
    xml.language config[:site][:language]
    
    filtered_items.each do |options|
      url = File.join(config[:site][:base_url], options[:path])
      
      xml.entry do
        xml.title options[:title]
        xml.summary options[:description]
        xml.updated options[:updated_at].xmlschema
        xml.link :rel => "alternate", :type => "text/html", :href => url
        xml.id url
      end
    end
  end
  
  # close handler
  file.close
end