Sha256: 98277f09568884269594593c23370f9dccae2a0e3b90842b37bd5f1b330240a9

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module XmlSitemap
  class Index
    attr_reader :maps
    
    # Initialize a new Index instance
    #
    # opts   - Index options
    #
    # opts[:secure] - Force HTTPS for all items. (default: false)
    #
    def initialize(opts={})
      @maps     = []
      @offsets  = Hash.new(0)
      @secure   = opts[:secure] || false
      
      yield self if block_given?
    end
    
    # Add map object to index
    #
    # map - XmlSitemap::Map instance
    #
    def add(map)
      raise ArgumentError, 'XmlSitemap::Map object requred!' unless map.kind_of?(XmlSitemap::Map)
      raise ArgumentError, 'Map is empty!' if map.empty?
      
      @maps << {
        :loc     => map.index_url(@offsets[map.group], @secure),
        :lastmod => map.created_at.utc.iso8601
      }
      @offsets[map.group] += 1
    end
    
    # Generate sitemap XML index
    #
    def render
      xml = Builder::XmlMarkup.new(:indent => 2)
      xml.instruct!(:xml, :version => '1.0', :encoding => 'UTF-8')
      xml.sitemapindex(XmlSitemap::INDEX_SCHEMA_OPTIONS) { |s|
        @maps.each do |item|
          s.sitemap do |m|
            m.loc        item[:loc]
            m.lastmod    item[:lastmod]
          end
        end
      }.to_s
    end
    
    # Render XML sitemap index into the file
    #
    # path    - Output filename
    # options - Options hash
    #
    # options[:ovewrite] - Overwrite file contents (default: true)
    #
    def render_to(path, options={})
      overwrite = options[:overwrite] || true
      path = File.expand_path(path)
      
      if File.exists?(path) && !overwrite
        raise RuntimeError, "File already exists and not overwritable!"
      end
      
      File.open(path, 'w') { |f| f.write(self.render) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xml-sitemap-1.3.1 lib/xml-sitemap/index.rb