module XmlSitemap PERIODS = [:none, :always, :hourly, :daily, :weekly, :monthly, :yearly, :never] class Item attr_reader :path attr_reader :updated attr_reader :priority attr_reader :changefreq def initialize(opts={}) @path = opts[:url] if opts.key?(:url) @updated = opts[:updated] || Time.now @priority = opts[:priority] || 0.5 @changefreq = opts[:period] || :never end end class Map attr_reader :domain, :items attr_reader :buffer # Creates new Map class for specified domain def initialize(domain) @domain = domain @items = [] yield self if block_given? end # Yields Map class for easier access def generate raise ArgumentError, 'Block required' unless block_given? yield self end # Add new item to sitemap list def add(opts) @items << XmlSitemap::Item.new(opts) end # Render XML def render return '' if @items.size == 0 output = [] output << '' output << '' @items.each do |item| output << '' output << "http://#{@domain}#{item.path.to_s}" output << "#{item.updated.to_date}" output << "#{item.changefreq.to_s}" output << "#{item.priority.to_s}" output << '' end output << '' return output.join("\n") end end end