Creates a sitemap
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


This is a simple class to write a website sitemap file.
You can use it on two ways:

As a class

You can create a class and tell it to write the sitemap file when needed, 
sending it a collection with the data to be written and optionally another one
with extra data. The extra data can be used, for example, to send static urls
that are not part of the first collection.

require "rubygems"
require "taq-sitemapper"

data = [{url: "One"  , lastmod:, freq: "monthly", priority: 1}, 
        {url: "Two"  , lastmod:, freq: "monthly", priority: 1}, 
        {url: "Three", lastmod:, freq: "monthly", priority: 1}]

extra= [{url: "Four" , lastmod:, freq: "yearly", priority: 0.5}, 
        {url: "Five" , lastmod:, freq: "yearly", priority: 0.5}, 
        {url: "Six"  , lastmod:, freq: "yearly", priority: 0.5}]

sitemapper =
sitemapper.loc  = :url
sitemapper.changefreq  = :freq
sitemapper.priority  = :priority
sitemapper.file = "/tmp/sitemap_class.xml"
sitemapper.url = ""

# extra data
sitemapper.file = "/tmp/sitemap_class_extra.xml"

The default values for the attributes are:

url        = nil
loc        = :loc
lastmod    = :lastmod
changefreq = "daily"
priority   = 1.00
file       = nil

The can be changed (as above) to point to some method, attribute or static
value. For example, if your collection has an attribute called url and you
want to use it as the loc attribute, you can point that using 
sitemapper.loc = :url and on the example above. It's important that the extra
collection have the same attributes as the main collection.

If the file attribute is null, no sitemap file will be written.

If the url attribute is not null, you can use the ping method to send a ping
to search engines, pointing to the sitemap on the url.

As a module

You can use it also as a module. Of course the main use for this kind of
behaviour was thinking about the use with ActiveRecord, but will (hopefully)
work on other type of collections:

require "rubygems"
require "active_record"
require "taq-sitemapper"

   adapter: "sqlite3",
   database: "../test/taq-sitemapper.sqlite"

class SitemapperTestAR < ActiveRecord::Base  
   include SiteMapper
   sitemap[:file] = "/tmp/sitemap_module.xml"
   sitemap[:changefreq] = :freq
   sitemap[:priority] = :priority

   after_save :write_sitemap
   def self.sitemap_extra
      [{loc: "Four", lastmod:, freq: "yearly", priority: 0.5}, 
       {loc: "Five", lastmod:, freq: "yearly", priority: 0.5}, 
       {loc: "Six" , lastmod:, freq: "yearly", priority: 0.5}]

   def loc

   def write_sitemap

# extra data
SitemapperTestAR.sitemap[:extra] = :sitemap_extra
SitemapperTestAR.sitemap[:file]  = "/tmp/sitemap_module_extra.xml"

SitemapperTestAR.sitemap[:file]  = "/tmp/sitemap_module_extra_after_save.xml"
first = SitemapperTestAR.first

Some points here:

1 - Is inserted an attribute accessor called sitemap on the object. The same
    class attributes described above are accessible there, as a hash. For
    example, Object.sitemap[:file]
2 - The default for collections is the all method. If need to change it, set the
    sitemap[:collection] attribute to point to the desired method.   
3 - The extra collection is pointed to sitemap[:extra]. There is no need to
    use these attributes if calling directly the write_sitemap method with the
    collections. The extra collection method, if set, needs to be a class
4 - You can use some hooks like after_save to create the sitemap after an object
    is saved, see the example above.