Class: SitemapBoiler::SitemapGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/sitemap_boiler/sitemap_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SitemapGenerator

Returns a new instance of SitemapGenerator.



8
9
10
# File 'lib/sitemap_boiler/sitemap_generator.rb', line 8

def initialize config
  @config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/sitemap_boiler/sitemap_generator.rb', line 6

def config
  @config
end

Instance Method Details

#location_url(config, localization, page) ⇒ Object



16
17
18
# File 'lib/sitemap_boiler/sitemap_generator.rb', line 16

def location_url config, localization, page
  URLComposer.compose(config[:base_url], localization['prefix'], page['path'])
end

#to_xml(localization) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sitemap_boiler/sitemap_generator.rb', line 20

def to_xml localization
  xml_markup.urlset(config[:urlset_headers]) do |urlset|
    config[:pages].each do |page|
      urlset.url do |url|
        url.loc location_url(config, localization, page)
        config[:localizations].each do |alternate|
          url.tag!('xhtml:link', 
            href: location_url(config, alternate, page),
            hreflang: alternate['hreflang'],
            rel: :alternate
            )
        end
        if page['android_url']
          url.tag!('xhtml:link', href: page['android_url'], rel: :alternate)
        end
      end
    end
  end
end

#write(localization, path) ⇒ Object



12
13
14
# File 'lib/sitemap_boiler/sitemap_generator.rb', line 12

def write localization, path
  File.open(path, 'w') { |file| file.write(to_xml(localization)) }
end