class Nanoc::Helpers::Blogging::AtomFeedBuilder

Attributes

author_name[RW]
author_uri[RW]
config[RW]
content_proc[RW]
excerpt_proc[RW]
icon[RW]
id[RW]
limit[RW]
preserve_order[RW]
relevant_articles[RW]
title[RW]
title_proc[RW]

Public Class Methods

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

Public Instance Methods

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

Protected Instance Methods

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

  xml.entry do
    # Add primary attributes
    xml.id atom_tag_for(article)
    xml.title title_proc.call(article), type: 'html'

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

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

    # Add link
    xml.link(rel: 'alternate', href: url, type: 'text/html')

    # Add content
    summary = excerpt_proc.call(article)
    xml.content content_proc.call(article), 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 111
def build_for_feed(xml)
  root_url = @config[:base_url] + '/'
  xml.instruct!
  xml.feed(xmlns: 'http://www.w3.org/2005/Atom', 'xml:base' => root_url) do
    # Add primary attributes
    xml.id(id || root_url)
    xml.title title

    # Add date
    xml.updated(updated.__nanoc_to_iso8601_time)

    # Add links
    xml.link(rel: 'alternate', href: alt_link || root_url, type: 'text/html')
    xml.link(rel: 'self',      href: feed_url,             type: 'application/atom+xml')

    # 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|
      build_for_article(a, xml)
    end
  end
end
last_article() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 76
def last_article
  sorted_relevant_articles.first
end
sorted_relevant_articles() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 66
def sorted_relevant_articles
  all = relevant_articles

  unless @preserve_order
    all = all.sort_by { |a| attribute_to_time(a[:created_at]) }
  end

  all.reverse.first(limit)
end
updated() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 80
def updated
  relevant_articles.map { |a| attribute_to_time(a[:updated_at] || a[:created_at]) }.max
end
validate_articles() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 102
def validate_articles
  if relevant_articles.empty?
    raise Nanoc::Core::TrivialError.new('Cannot build Atom feed: no articles')
  end
  if relevant_articles.any? { |a| a[:created_at].nil? }
    raise Nanoc::Core::TrivialError.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 84
def validate_config
  if @config[:base_url].nil?
    raise Nanoc::Core::TrivialError.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 90
def validate_feed_item
  if title.nil?
    raise Nanoc::Core::TrivialError.new('Cannot build Atom feed: no title in params, item or site config')
  end
  if author_name.nil?
    raise Nanoc::Core::TrivialError.new('Cannot build Atom feed: no author_name in params, item or site config')
  end
  if author_uri.nil?
    raise Nanoc::Core::TrivialError.new('Cannot build Atom feed: no author_uri in params, item or site config')
  end
end