Class: SitemapBuilder::SitemapIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/sitemap_builder/sitemap_index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SitemapIndex

Returns a new instance of SitemapIndex.



5
6
7
8
9
10
11
# File 'lib/sitemap_builder/sitemap_index.rb', line 5

def initialize(options={})
  options = { 
    :filename => 'sitemap_index.xml',
  }.merge options
  @filename = options[:filename]
  @sitemaps = []
end

Instance Attribute Details

#sitemapsObject

Returns the value of attribute sitemaps.



3
4
5
# File 'lib/sitemap_builder/sitemap_index.rb', line 3

def sitemaps
  @sitemaps
end

Instance Method Details

#fullpathObject



36
37
38
# File 'lib/sitemap_builder/sitemap_index.rb', line 36

def fullpath
  "/public/" + @filename
end

#generateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sitemap_builder/sitemap_index.rb', line 13

def generate
  puts "GENERATING XML INDEX FILE ..." if @debug
  xml_str = ""
  xml = Builder::XmlMarkup.new(:target => xml_str, :indent=>2)

  xml.instruct!
  xml.sitemapindex "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
    @sitemaps.each do |s|
      xml.sitemap do
        xml.loc s.loc
        xml.lastmod w3c_date(Time.now)
      end
    end
  end
  save_file(xml_str)
end

#save_file(xml) ⇒ Object



30
31
32
33
34
# File 'lib/sitemap_builder/sitemap_index.rb', line 30

def save_file(xml)
  File.open(File.join(Rails.root, fullpath), "w+") do |f|
    f.write(xml)
  end    
end