Class: AegisNet::Sitemapper::Index
- Inherits:
-
Object
- Object
- AegisNet::Sitemapper::Index
- Defined in:
- lib/sitemapper/index.rb
Overview
:doc:
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#sitemaps ⇒ Object
readonly
Returns the value of attribute sitemaps.
Class Method Summary collapse
Instance Method Summary collapse
- #create! ⇒ Object
-
#initialize(options = {}) ⇒ Index
constructor
A new instance of Index.
Constructor Details
#initialize(options = {}) ⇒ Index
Returns a new instance of Index.
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( = {}) .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 = {:loc => @static["sitemapfile"], :lastmod => @static["lastmod"]} @sitemaps << AegisNet::Sitemapper::Urlset.new() end # Include additional sitemaps @includes.each do |sitemap| = {:loc => sitemap["loc"] } .merge!(:lastmod => sitemap["lastmod"]) if sitemap["lastmod"] @sitemaps << AegisNet::Sitemapper::Sitemap.new() 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 = {:loc => sitemap.last["sitemapfile"], :lastmod => lastmod} @sitemaps << AegisNet::Sitemapper::Urlset.new() else 1.upto( (count / 50_000.0).ceil ) do |part_number| = { :loc => sitemap.last["sitemapfile"].gsub("xml", "#{part_number}.xml"), :lastmod => lastmod } @sitemaps << AegisNet::Sitemapper::Urlset.new() end end end end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/sitemapper/index.rb', line 7 def host @host end |
#sitemaps ⇒ Object (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!( = {}) index = self.new() 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 |