Class: SimpleSitemap::Generators::Index

Inherits:
Base
  • Object
show all
Defined in:
lib/simple_sitemap/generators/index.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #hooks

Instance Method Summary collapse

Constructor Details

#initialize(config, hooks) ⇒ Index

Returns a new instance of Index.



12
13
14
15
# File 'lib/simple_sitemap/generators/index.rb', line 12

def initialize(config, hooks)
  super
  @sitemaps = []
end

Instance Attribute Details

#sitemapsObject

Returns the value of attribute sitemaps.



10
11
12
# File 'lib/simple_sitemap/generators/index.rb', line 10

def sitemaps
  @sitemaps
end

Instance Method Details

#add_sitemap(name) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/simple_sitemap/generators/index.rb', line 17

def add_sitemap(name)
  url = if @config.sitemap_location[-1,1] == '/'
    "#{@config.sitemap_location}#{name}"
  else
    "#{@config.sitemap_location}/#{name}"
  end
  @sitemaps << url
end

#to_xmlObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/simple_sitemap/generators/index.rb', line 38

def to_xml
  builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.sitemapindex(xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9') do
      @sitemaps.each do |sitemap_url|
        xml.sitemap do
          xml.loc sitemap_url
        end
      end
    end
  end
  builder.to_xml
end

#write!Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/simple_sitemap/generators/index.rb', line 26

def write!
  xml = to_xml
  index_filename = 'index.xml'
  if @config.prefix
    index_filename = "#{@config.prefix}_#{index_filename}"
  end
  if @config.gzip
    index_filename << '.gz'
  end
  write_file index_filename, xml
end