Class: AegisNet::Sitemapper::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/sitemapper/index.rb

Overview

:doc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Index

Returns a new instance of Index.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sitemapper/index.rb', line 9

def initialize(options = {})
  options.symbolize_keys!
  @sitemaps = []

  @config = AegisNet::Sitemapper::Loader.load_config
  @host   = @config[:default_host]
  @file   = File.join("#{@config[:local_path]}", @config[:index]["sitemapfile"])

  @static   = @config[:static]
  @models   = @config[:models]
  @includes = @config[:index]["includes"]

  # Validate all variables
  raise(ArgumentError, "No filename specified") if @file.nil?

  # Static Sitemap
  if @static
    sitemap_options = {:loc => @static["sitemapfile"], :lastmod => @static["lastmod"]}
    @sitemaps << AegisNet::Sitemapper::Urlset.new(sitemap_options)
  end

  # Include additional sitemaps
  @includes.each do |sitemap|
    sitemap_options = {:loc => sitemap["loc"] }
    sitemap_options.merge!(:lastmod => sitemap["lastmod"]) if sitemap["lastmod"]
    @sitemaps << AegisNet::Sitemapper::Sitemap.new(sitemap_options)
  end

  @models.each do |sitemap|
    klass = sitemap.first.camelize.constantize
    count = klass.count

    order_opts = {}
    order_opts = { :order => :created_at } if klass.column_names.include?("created_at")
    lastmod = sitemap.last["lastmod"] || klass.last(order_opts).created_at

    if count <= 50_000
      sitemap_options = {:loc => sitemap.last["sitemapfile"], :lastmod => lastmod}
      @sitemaps << AegisNet::Sitemapper::Urlset.new(sitemap_options)
    else
      1.upto( (count / 50_000.0).ceil ) do |part_number|
        sitemap_options = {
          :loc => sitemap.last["sitemapfile"].gsub("xml", "#{part_number}.xml"),
          :lastmod => lastmod
        }
        @sitemaps << AegisNet::Sitemapper::Urlset.new(sitemap_options)
      end
    end
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/sitemapper/index.rb', line 7

def host
  @host
end

#sitemapsObject (readonly)

Returns the value of attribute sitemaps.



7
8
9
# File 'lib/sitemapper/index.rb', line 7

def sitemaps
  @sitemaps
end

Class Method Details

.create!(options = {}) ⇒ Object



60
61
62
63
# File 'lib/sitemapper/index.rb', line 60

def self.create!(options = {})
  index = self.new(options)
  index.create!
end

Instance Method Details

#create!Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sitemapper/index.rb', line 65

def create!
  xml = Builder::XmlMarkup.new(:indent => 2)
  xml.instruct!

  xml.sitemapindex "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do

    @sitemaps.each do |sitemap|
      location = sitemap.loc.gsub(/^\//, '')
      xml.sitemap do
        xml.loc         "http://#{@host}/#{location}"
        xml.lastmod     sitemap.lastmod.to_date if sitemap.lastmod rescue nil # TODO handle properly
      end
    end
  end
  File.open(@file, "w") { |file| file.puts xml.target! }
end