Class: TinyAtom::Feed

Inherits:
Object
  • Object
show all
Defined in:
lib/tinyatom/feed.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_url, title, feed_url, options = {}) ⇒ Feed

Returns a new instance of Feed.



9
10
11
12
13
14
15
16
# File 'lib/tinyatom/feed.rb', line 9

def initialize(site_url, title, feed_url, options={})
  @site_url, @title, @feed_url, @feed_options = site_url, title, feed_url,
    options

  @site_domain = URI(@site_url).extend(TinyAtom::URIDomain).domain

  @entries = []
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



80
81
82
# File 'lib/tinyatom/feed.rb', line 80

def entries
  @entries
end

#feed_optionsObject (readonly)

Returns the value of attribute feed_options.



78
79
80
# File 'lib/tinyatom/feed.rb', line 78

def feed_options
  @feed_options
end

#feed_urlObject (readonly)

Returns the value of attribute feed_url.



77
78
79
# File 'lib/tinyatom/feed.rb', line 77

def feed_url
  @feed_url
end

#site_domainObject (readonly)

Returns the value of attribute site_domain.



79
80
81
# File 'lib/tinyatom/feed.rb', line 79

def site_domain
  @site_domain
end

#site_urlObject (readonly)

Returns the value of attribute site_url.



75
76
77
# File 'lib/tinyatom/feed.rb', line 75

def site_url
  @site_url
end

#titleObject (readonly)

Returns the value of attribute title.



76
77
78
# File 'lib/tinyatom/feed.rb', line 76

def title
  @title
end

Instance Method Details

#add_entry(id, title, updated, link, options = {}) ⇒ Object

Add an entry to the feed



19
20
21
22
23
24
25
26
# File 'lib/tinyatom/feed.rb', line 19

def add_entry(id, title, updated, link, options={})
  entries << {
    :id => id,
    :title => title,
    :updated => updated,
    :link => link
    }.merge(options)
end

#entry_id(e) ⇒ Object

Build an entry id.



70
71
72
73
# File 'lib/tinyatom/feed.rb', line 70

def entry_id(e)
  # http://diveintomark.org/archives/2004/05/28/howto-atom-id
  "tag:#{site_domain},#{e[:updated].strftime('%Y-%m-%d')}:#{e[:id]}"
end

#make(options = {}) ⇒ Object

Build the feed and return a Builder::XmlMarkup.



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
# File 'lib/tinyatom/feed.rb', line 29

def make(options={})
  xm = Builder::XmlMarkup.new(options)
  xm.instruct! :xml

  xm.feed(:xmlns => 'http://www.w3.org/2005/Atom',
    :'xmlns:media' => 'http://search.yahoo.com/mrss/') {
    xm.title title
    xm.link :href => feed_url, :rel => 'self'
    if u = updated
      xm.updated u.xmlschema
    end
    xm.id site_url
    TinyAtom::author xm, feed_options
    feed_options.fetch(:hubs, []).each do |hub|
      xm.link :rel => 'hub', :href => hub
    end

    entries.each do |e|
      xm.entry {
        xm.title e[:title]
        xm.link :href => e[:link]
        xm.id entry_id(e)
        xm.updated e[:updated].xmlschema
        xm.summary(e[:summary])  if e[:summary]
        xm.content(e[:content])  if e[:content]

        (e[:authors] || [e]).each { |h| TinyAtom::author xm, h }
        (e[:enclosures] || [e]).each { |h| TinyAtom::enclosure xm, h }
        (e[:media_thumbnails] || [e]).each do |h|
          TinyAtom::media_thumbnail xm, h
        end
        TinyAtom::via xm, e
      }
    end
  }
end

#updatedObject

Return the last update time of the feed.



67
# File 'lib/tinyatom/feed.rb', line 67

def updated; entries.map { |e| e[:updated] }.max; end