class SitemapGenerator::Builder::SitemapFile

General Usage:

sitemap = SitemapFile.new(:location => SitemapLocation.new(...))
sitemap.add('/', { ... })    <- add a link to the sitemap
sitemap.finalize!            <- write the sitemap file and freeze the object to protect it from further modification

Attributes

filesize[R]
location[R]
news_count[R]

Public Class Methods

new(opts={}, schemas, schema_location) click to toggle source

Options

# File lib/sitemap_generator/builder/sitemap_file.rb, line 22
def initialize(opts={}, schemas, schema_location)
  @location = opts.is_a?(Hash) ? SitemapGenerator::SitemapLocation.new(opts) : opts
  @link_count = 0
  @news_count = 0
  @xml_content = '' # XML urlset content
  @schemas = schemas
  @schema_location = schema_location
  @xml_wrapper_start = xml_wrapper_start.gsub(/\s+/, ' ').gsub(/ *> */, '>').strip.slice(1..-2)
  @xml_wrapper_end   = %q[</urlset>]
  @filesize = SitemapGenerator::Utilities.bytesize(@xml_wrapper_start) + SitemapGenerator::Utilities.bytesize(@xml_wrapper_end)
  @written = false
  @reserved_name = nil # holds the name reserved from the namer
  @frozen = false      # rather than actually freeze, use this boolean
end

Public Instance Methods

add(link, options={}) click to toggle source

Add a link to the sitemap file.

If a link cannot be added, for example if the file is too large or the link limit has been reached, a SitemapGenerator::SitemapFullError exception is raised and the sitemap is finalized.

If the Sitemap has already been finalized a SitemapGenerator::SitemapFinalizedError exception is raised.

Return the new link count.

Call with:

sitemap_url - a SitemapUrl instance
sitemap, options - a Sitemap instance and options hash
path, options - a path for the URL and options hash.  For supported options
                see the SitemapGenerator::Builder::SitemapUrl class.

The link added to the sitemap will use the host from its location object if no host has been specified.

# File lib/sitemap_generator/builder/sitemap_file.rb, line 94
def add(link, options={})
  raise SitemapGenerator::SitemapFinalizedError if finalized?

  sitemap_url = if link.is_a?(SitemapUrl)
    link
  else
    options[:host] ||= @location.host
    SitemapUrl.new(link, options)
  end

  xml = sitemap_url.to_xml
  raise SitemapGenerator::SitemapFullError if !file_can_fit?(xml)

  if sitemap_url.news?
    @news_count += 1
  end

  # Add the XML to the sitemap
  @xml_content << xml
  @filesize += SitemapGenerator::Utilities.bytesize(xml)
  @link_count += 1
end
empty?() click to toggle source
# File lib/sitemap_generator/builder/sitemap_file.rb, line 63
def empty?
  @link_count == 0
end
file_can_fit?(bytes) click to toggle source

Return a boolean indicating whether the sitemap file can fit another link of bytes bytes in size. You can also pass a string and the bytesize will be calculated for you.

# File lib/sitemap_generator/builder/sitemap_file.rb, line 70
def file_can_fit?(bytes)
  bytes = bytes.is_a?(String) ? SitemapGenerator::Utilities.bytesize(bytes) : bytes
  (@filesize + bytes) < SitemapGenerator::MAX_SITEMAP_FILESIZE && @link_count < SitemapGenerator::MAX_SITEMAP_LINKS && @news_count < SitemapGenerator::MAX_SITEMAP_NEWS
end
finalize!() click to toggle source

“Freeze” this object. Actually just flags it as frozen.

A SitemapGenerator::SitemapFinalizedError exception is raised if the Sitemap has already been finalized.

# File lib/sitemap_generator/builder/sitemap_file.rb, line 121
def finalize!
  raise SitemapGenerator::SitemapFinalizedError if finalized?
  @frozen = true
end
finalized?() click to toggle source
# File lib/sitemap_generator/builder/sitemap_file.rb, line 126
def finalized?
  @frozen
end
lastmod() click to toggle source

If a name has been reserved, use the last modified time from the file. Otherwise return nil. We don’t want to prematurely assign a name for this sitemap if one has not yet been reserved, because we may mess up the name-assignment sequence.

# File lib/sitemap_generator/builder/sitemap_file.rb, line 57
def lastmod
  File.mtime(location.path) if location.reserved_name?
rescue
  nil
end
new() click to toggle source

Return a new instance of the sitemap file with the same options, and the next name in the sequence.

# File lib/sitemap_generator/builder/sitemap_file.rb, line 164
def new
  location = @location.dup
  location.delete(:filename) if location.namer
  self.class.new(location, @schemas, @schema_location)
end
reserve_name() click to toggle source

Reserve a name from the namer unless one has already been reserved. Safe to call more than once.

# File lib/sitemap_generator/builder/sitemap_file.rb, line 153
def reserve_name
  @reserved_name ||= @location.reserve_name
end
reserved_name?() click to toggle source

Return a boolean indicating whether a name has been reserved

# File lib/sitemap_generator/builder/sitemap_file.rb, line 158
def reserved_name?
  !!@reserved_name
end
write() click to toggle source

Write out the sitemap and free up memory.

All the xml content in the instance is cleared, but attributes like filesize are still available.

A SitemapGenerator::SitemapError exception is raised if the file has already been written.

# File lib/sitemap_generator/builder/sitemap_file.rb, line 137
def write
  raise SitemapGenerator::SitemapError.new("Sitemap already written!") if written?
  finalize! unless finalized?
  reserve_name
  @location.write(@xml_wrapper_start + @xml_content + @xml_wrapper_end, link_count)
  @xml_content = @xml_wrapper_start = @xml_wrapper_end = ''
  @written = true
end
written?() click to toggle source

Return true if this file has been written out to disk

# File lib/sitemap_generator/builder/sitemap_file.rb, line 147
def written?
  @written
end
xml_wrapper_start() click to toggle source
# File lib/sitemap_generator/builder/sitemap_file.rb, line 37
      def xml_wrapper_start
        wrapper_start = '<?xml version="1.0" encoding="UTF-8"?>
            <urlset
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
        schema_location = "xsi:schemaLocation=\"#{@schema_location}\""
        general_schema = 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'
        customized_schemas = @schemas.collect do |schema, content|
          "xmlns:#{schema}=\"#{content}\""
        end
        wrapper_end = 'xmlns:xhtml="http://www.w3.org/1999/xhtml">'
        xml_start = [wrapper_start, schema_location, general_schema, customized_schemas, wrapper_end].join(" ")
        return <<-HTML
          "#{xml_start}"
        HTML
      end