Sha256: 9cd368764a6542b02b916215e400338c9d06be4471e0261cff35384cd75c40db

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 KB

Contents

module Navigasmic
  class XmlNavigationBuilder

    @@wrapper_tag = :urlset
    @@group_tag = :urlset
    @@item_tag = :url

    attr_accessor :template, :name, :items, :host

    def initialize(template, name, options = {}, &proc)
      @template, @name, @items = template, name.to_s, []
      @host = options[:host] || "http://#{template.request.host_with_port}"
      @changefreq = options[:changefreq] || 'yearly'
      render(options.delete(:xml), &proc)
    end

    def render(options, &proc)
      buffer = template.capture(self, &proc)

      options ||= {}
      options['xmlns'] ||= 'http://www.sitemaps.org/schemas/sitemap/0.9'
      options['xmlns:xsi'] ||= 'http://www.w3.org/2001/XMLSchema-instance'
      options['xsi:schemaLocation'] ||= 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'

      template.concat(template.content_tag(@@wrapper_tag, buffer, options))
    end

    def group(label = nil, options = {}, &proc)
      raise ArgumentError, "Missing block" unless block_given?

      buffer = template.capture(self, &proc)
      template.concat(template.content_tag(@@group_tag, buffer))
      ''
    end

    def item(label, options = {}, &proc)
      buffer = block_given? ? template.capture(self, &proc) : ''

      item = NavigationItem.new(label, options, template)

      contents = template.content_tag(:loc, @host + template.url_for(item.link))
      contents << template.content_tag(:changefreq, options[:changefreq] || @changefreq)
      contents << template.content_tag(:lastmod, options[:lastmod]) if options[:lastmod]
      contents << template.content_tag(:priority, options[:priority]) if options[:priority]
      
      template.concat(template.content_tag(@@item_tag, contents + buffer, options.delete(:xml)))
      ''
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
navigasmic-0.4.1 lib/builders/xml_builder.rb
navigasmic-0.4.0 lib/builders/xml_builder.rb
navigasmic-0.3.3 lib/builders/xml_builder.rb
navigasmic-0.3.2 lib/builders/xml_builder.rb
navigasmic-0.3.1 lib/builders/xml_builder.rb
navigasmic-0.3.0 lib/builders/xml_builder.rb