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

#index_filename(index_count) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/simple_sitemap/generators/index.rb', line 33

def index_filename(index_count)
  filename = "index.xml"
  filename = "index_#{index_count}.xml" if index_count > 0
  filename = "#{@config.prefix}_#{filename}" if @config.prefix
  filename << '.gz' if @config.gzip
  filename
end

#to_xml(sitemaps) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/simple_sitemap/generators/index.rb', line 41

def to_xml(sitemaps)
  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
# File 'lib/simple_sitemap/generators/index.rb', line 26

def write!
  @sitemaps.each_slice(SimpleSitemap::MAX_INDEX_SIZE).with_index do |sitemaps, index_count|
    xml = to_xml(sitemaps)
    write_file index_filename(index_count), xml
  end
end