class Nanoc::Helpers::Blogging::AtomFeedBuilder

Attributes

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

Public Class Methods

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

Public Instance Methods

build() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 77
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 71
def validate
  validate_config
  validate_feed_item
  validate_articles
end

Protected Instance Methods

build_for_article(a, xml) click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 160
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]).__nanoc_to_iso8601_time
    xml.updated attribute_to_time(a[:updated_at] || a[:created_at]).__nanoc_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 127
def build_for_feed(xml)
  xml.instruct!
  xml.feed(xmlns: 'http://www.w3.org/2005/Atom') do
    root_url = @config[:base_url] + '/'

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

    # Add date
    xml.updated(attribute_to_time(last_article[:created_at]).__nanoc_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|
      build_for_article(a, xml)
    end
  end
end
last_article() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 96
def last_article
  sorted_relevant_articles.first
end
sorted_relevant_articles() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 86
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
validate_articles() click to toggle source
# File lib/nanoc/helpers/blogging.rb, line 118
def validate_articles
  if relevant_articles.empty?
    raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: no articles')
  end
  if relevant_articles.any? { |a| a[:created_at].nil? }
    raise Nanoc::Int::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 100
def validate_config
  if @config[:base_url].nil?
    raise Nanoc::Int::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 106
def validate_feed_item
  if title.nil?
    raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: no title in params, item or site config')
  end
  if author_name.nil?
    raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: no author_name in params, item or site config')
  end
  if author_uri.nil?
    raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: no author_uri in params, item or site config')
  end
end