Module: FeedBuilder::Provider

Defined in:
lib/feedbuilder/provider.rb

Instance Method Summary collapse

Instance Method Details

#build_entry(model, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/feedbuilder/provider.rb', line 50

def build_entry(model,  &block)
  Atom::Entry.new do |entry|
    entry.id = model.entry_id if model.respond_to?(:entry_id)
    entry.title = model.entry_title if model.respond_to?(:entry_title)
    entry.published = model.respond_to?(:entry_published) ? model. : model.created_at
    entry.updated = model.respond_to?(:entry_updated) ? model.entry_updated : model.updated_at
    entry.summary = model.entry_summary if model.respond_to?(:entry_summary)
    entry.content = model.entry_content if model.respond_to?(:entry_content)
    yield(model, entry) if block_given?
  end
end

#build_feed(collection, url_builder, options = {}, &block) ⇒ Object



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
# File 'lib/feedbuilder/provider.rb', line 8

def build_feed(collection, url_builder, options = {}, &block)
  feed_updated = nil
  collection.each do |model|
    if feed_updated.nil? || model.updated_at > feed_updated
      feed_updated = model.updated_at
    end
  end
  Atom::Feed.new do |feed|
    feed.id = if options.include?(:feed_id)
      options[:feed_id]
    elsif options.include?(:feed_id_path)
      feed_tag_uri(options[:feed_id_path])
    elsif self.feed_id_path.present?
      feed_tag_uri(self.feed_id_path)
    else
      self.feed_id
    end
    feed.title = options[:feed_title]
    feed.updated = feed_updated
    feed.links << Atom::Link.new(:href => url_builder.html_url, :rel => :via, :type => 'text/html')
    feed.links << Atom::Link.new(:href => url_builder.self_url, :rel => :self, :type => 'application/atom+xml')
    if collection.respond_to?(:total_pages) && collection.respond_to?(:current_page) &&
        collection.respond_to?(:per_page)
      if collection.total_pages > 1
        feed.links << Atom::Link.new(:href => url_builder.first_url(collection.per_page), :rel => :first,
                                     :type => 'application/atom+xml')
        if collection.current_page > 1
          feed.links << Atom::Link.new(:href => url_builder.prev_url(collection.current_page, collection.per_page),
            :rel => :previous, :type => 'application/atom+xml')
        end
        if collection.current_page < collection.total_pages
          feed.links << Atom::Link.new(:href => url_builder.next_url(collection.current_page, collection.per_page),
            :rel => :next, :type => 'application/atom+xml')
        end
        feed.links << Atom::Link.new(:href => url_builder.last_url(collection.total_pages, collection.per_page),
          :rel => :last, :type => 'application/atom+xml')
      end
    end
    collection.each {|model| feed.entries << build_entry(model, &block)}
  end
end

#feed_tag_uri(path, options = {}) ⇒ Object



63
64
65
66
67
68
# File 'lib/feedbuilder/provider.rb', line 63

def feed_tag_uri(path, options = {})
  domain = options[:domain] || self.feed_id_domain || FeedBuilder.feed_id_domain
  date = options[:date] || self.feed_id_date || Date.today
  date_str = date.acts_like_date?? date.strftime("%Y-%m-%d") : date.to_s
  "tag:#{domain},#{date_str}:#{path.gsub(/\#/, '/')}"
end