Class: Perron::Site::Builder::Sitemap

Inherits:
Object
  • Object
show all
Includes:
RouteResources
Defined in:
lib/perron/site/builder/sitemap.rb

Instance Method Summary collapse

Methods included from RouteResources

#buildable_routes, #collection_for, #resources_for

Constructor Details

#initialize(output_path) ⇒ Sitemap

Returns a new instance of Sitemap.



11
12
13
# File 'lib/perron/site/builder/sitemap.rb', line 11

def initialize(output_path)
  @output_path = output_path
end

Instance Method Details

#generateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/perron/site/builder/sitemap.rb', line 15

def generate
  return if !Perron.configuration.sitemap.enabled

  added_urls = Set.new

  xml = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |builder|
    builder.urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9") do
      add_additional_routes(with: builder)

      buildable_routes.each do |route|
        case route.defaults[:action]
        when "index"
          add_index_url(route, with: builder, added_urls: added_urls)
        when "show"
          if (route.required_keys - %i[controller action]).empty?
            add_index_url(route, with: builder, added_urls: added_urls)
          else
            resources_for(route).reject(&:root?).each do |resource|
              next if resource..sitemap == false

              add_show_url(route, resource, with: builder, added_urls: added_urls)
            end
          end
        end
      end
    end
  end.to_xml

  File.write(@output_path.join("sitemap.xml"), xml)
end