Sha256: 34218b1cf30075210032af292ca2a4331497c0e8c9fa68320bdd60d055d11035

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

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 << '<?xml version="1.0" encoding="UTF-8"?>'
      output << '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
      @items.each do |item|
        output << '<url>'
          output << "<loc>http://#{@domain}#{item.path.to_s}</loc>"
          output << "<lastmod>#{item.updated.to_date}</lastmod>"
          output << "<changefreq>#{item.changefreq.to_s}</changefreq>"
          output << "<priority>#{item.priority.to_s}</priority>"
        output << '</url>'
      end
      output << '</urlset>'
      return output.join("\n")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xml-sitemap-1.0.1 lib/xml-sitemap.rb