Parent

Included Modules

Class/Module Index [+]

Quicksearch

Nanoc::Helpers::Blogging::AtomFeedBuilder

Attributes

author_name[RW]
author_uri[RW]
content_proc[RW]
excerpt_proc[RW]
icon[RW]
limit[RW]
logo[RW]
relevant_articles[RW]
site[RW]
title[RW]

Public Class Methods

new(site, item) click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 60
def initialize(site, item)
  @site = site
  @item = item
end

Public Instance Methods

build() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 71
def build
  buffer = ''
  xml = Builder::XmlMarkup.new(:target => buffer, :indent => 2)
  self.build_for_feed(xml)
  buffer
end
validate() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 65
def validate
  self.validate_config
  self.validate_feed_item
  self.validate_articles
end

Protected Instance Methods

build_for_article(a, xml) click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 150
def build_for_article(a, xml)
  # Get URL
  url = url_for(a)
  return if url.nil?

  xml.entry do
    # Add primary attributes
    xml.id        atom_tag_for(a)
    xml.title     a[:title], :type => 'html'

    # Add dates
    xml.published attribute_to_time(a[:created_at]).to_iso8601_time
    xml.updated   attribute_to_time(a[:updated_at] || a[:created_at]).to_iso8601_time

    # Add specific author information
    if a[:author_name] || a[:author_uri]
      xml.author do
        xml.name  a[:author_name] || author_name
        xml.uri   a[:author_uri]  || author_uri
      end
    end

    # Add link
    xml.link(:rel => 'alternate', :href => url)

    # Add content
    summary = excerpt_proc.call(a)
    xml.content   content_proc.call(a), :type => 'html'
    xml.summary   summary, :type => 'html' unless summary.nil?
  end
end
build_for_feed(xml) click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 117
def build_for_feed(xml)
  xml.instruct!
  xml.feed(:xmlns => 'http://www.w3.org/2005/Atom') do
    root_url = @site.config[:base_url] + '/'

    # Add primary attributes
    xml.id      root_url
    xml.title   title

    # Add date
    xml.updated(attribute_to_time(last_article[:created_at]).to_iso8601_time)

    # Add links
    xml.link(:rel => 'alternate', :href => root_url)
    xml.link(:rel => 'self',      :href => feed_url)

    # Add author information
    xml.author do
      xml.name  author_name
      xml.uri   author_uri
    end

    # Add icon and logo
    xml.icon icon if icon
    xml.logo logo if logo

    # Add articles
    sorted_relevant_articles.each do |a|
      self.build_for_article(a, xml)
    end
  end
end
last_article() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 86
def last_article
  sorted_relevant_articles.first
end
sorted_relevant_articles() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 80
def sorted_relevant_articles
  relevant_articles.sort_by do |a|
    attribute_to_time(a[:created_at])
  end.reverse.first(limit)
end
validate_articles() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 108
def validate_articles
  if relevant_articles.empty?
    raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: no articles')
  end
  if relevant_articles.any? { |a| a[:created_at].nil? }
    raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: one or more articles lack created_at')
  end
end
validate_config() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 90
def validate_config
  if @site.config[:base_url].nil?
    raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: site configuration has no base_url')
  end
end
validate_feed_item() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 96
def validate_feed_item
  if title.nil?
    raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: no title in params, item or site config')
  end
  if author_name.nil?
    raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: no author_name in params, item or site config')
  end
  if author_uri.nil?
    raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: no author_uri in params, item or site config')
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.