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: Time.now, freq: "monthly", priority: 1},
{url: "Two" , lastmod: Time.now, freq: "monthly", priority: 1},
{url: "Three", lastmod: Time.now, freq: "monthly", priority: 1}]
extra= [{url: "Four" , lastmod: Time.now, freq: "yearly", priority: 0.5},
{url: "Five" , lastmod: Time.now, freq: "yearly", priority: 0.5},
{url: "Six" , lastmod: Time.now, freq: "yearly", priority: 0.5}]
sitemapper = SiteMapper::SiteMapper.new
sitemapper.loc = :url
sitemapper.changefreq = :freq
sitemapper.priority = :priority
sitemapper.file = "/tmp/sitemap_class.xml"
sitemapper.url = "http://localhost.com/sitemap.xml"
sitemapper.write_sitemap(data.entries)
# extra data
sitemapper.file = "/tmp/sitemap_class_extra.xml"
sitemapper.write_sitemap(data.entries,extra.entries)
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"
ActiveRecord::Base.establish_connection({
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: Time.now, freq: "yearly", priority: 0.5},
{loc: "Five", lastmod: Time.now, freq: "yearly", priority: 0.5},
{loc: "Six" , lastmod: Time.now, freq: "yearly", priority: 0.5}]
end
def loc
url
end
def write_sitemap
self.class.write_sitemap
end
end
SitemapperTestAR.write_sitemap
# extra data
SitemapperTestAR.sitemap[:extra] = :sitemap_extra
SitemapperTestAR.sitemap[:file] = "/tmp/sitemap_module_extra.xml"
SitemapperTestAR.write_sitemap
SitemapperTestAR.sitemap[:file] = "/tmp/sitemap_module_extra_after_save.xml"
first = SitemapperTestAR.first
first.save
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
method.
4 - You can use some hooks like after_save to create the sitemap after an object
is saved, see the example above.
Project
taq-sitemapper
Create a sitemap with a data collection
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
Development
Project Readme