Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhellsten committed May 16, 2009
1 parent 0af289e commit 772f026
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 46 deletions.
12 changes: 0 additions & 12 deletions README

This file was deleted.

2 changes: 1 addition & 1 deletion init.rb
Original file line number Original file line Diff line number Diff line change
@@ -1 +1 @@
# Include hook code here ActiveRecord::Base.send :include, SitemapGenerator::ActiveRecord
12 changes: 12 additions & 0 deletions install.rb
Original file line number Original file line Diff line number Diff line change
@@ -1 +1,13 @@
# Install hook code here # Install hook code here

if !File.exist?(Options::CONFIG_FILE)
File.open(Options::CONFIG_FILE, 'w') do |file|
file << "domain: # For example: 'aktagon.com'"
file << "change_frequency: weekly"
file << "limit: 5000"
file << "priority: 1.0"
end
end

puts "SitemapGenerator installed."
puts "NOTE: You need to specify the domain of your application in #{Options::CONFIG_FILE}"
7 changes: 2 additions & 5 deletions lib/sitemap_generator.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,6 @@
ENV['RAILS_ENV'] ||= "development"

#require 'rubygems'
#require 'config/environment'

module SitemapGenerator module SitemapGenerator
DOMAIN = nil

module Version #:nodoc: module Version #:nodoc:
Major = 0 Major = 0
Minor = 1 Minor = 1
Expand Down
87 changes: 72 additions & 15 deletions lib/sitemap_generator/generator.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,29 +4,80 @@


module SitemapGenerator module SitemapGenerator
class Generator class Generator
def initialize(host, filename = "#{RAILS_ROOT}/public/sitemap.xml")
@host = host DEFAULT_OPTIONS = {
:order => nil,
:limit => Options.limit,
:priority => Options.priority,
:change_frequency => Options.change_frequency
}

def initialize(filename = "#{RAILS_ROOT}/public/sitemap.xml")
@filename = filename @filename = filename
@old_size, @new_size = File.size(@filename) rescue 0 @old_size, @new_size = File.size(@filename) rescue 0
end end


def find_models
models = []

files = Dir.glob(File.join(RAILS_ROOT, 'app', 'models', '*.rb')).delete_if {|c| c =~ /observer\.rb/ } #{|c| c < ActiveRecord::Base== false}

files.each do |file|
# Get the class from the filename
model = file.split('/').last[0..-4].classify.constantize
# Skip classes that don't have any sitemap options
next if !model.methods.include?('sitemap_options') || model.sitemap_options == nil

models << model
end

puts "Sitemap WARNING!! No models found. Have you included a call to the sitemap in your ActiveRecord models?" if models.empty?

models
end

def find_models_and_generate
self.generate do |data|
self.find_models.each do |model|
model_columns = model.columns.map(&:name)

# Use defaults
options = DEFAULT_OPTIONS.merge(model.sitemap_options)

# Find a column for ordering
if options[:order] == nil
order = ['updated_at', 'updated_on', 'created_at', 'created_on'].delete_if { |x| !model_columns.include?(x) }
options[:order] = "#{order.first} ASC"
end

puts "Sitemap #{model} #{options.inspect}"

# This is where we create the sitemap.
# Find and add model instances to the sitemap
model.all(:order => options[:order], :limit => options[:limit]).each do |o|
data.add o, options[:priority], options[:change_frequency]
end

end
end
end

def generate(&block) def generate(&block)
default_url_options[:host] = @host


File.open(@filename, "w") do |file| File.open(@filename, "w") do |file|
xml = Builder::XmlMarkup.new(:target => file, :indent => 2) xml = Builder::XmlMarkup.new(:target => file, :indent => 2)
xml.instruct! xml.instruct!


xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
block.call(@host, Sitemap.new(@host, xml)) block.call(Sitemap.new(xml))
end end
end end


@new_size = File.size(@filename) @new_size = File.size(@filename)


ping(host) if ping? ping if ping?


p "Sitemap '#{@filename}' generated successfully." puts "Sitemap '#{@filename}' generated successfully."
end end


def ping? def ping?
Expand All @@ -46,25 +97,31 @@ def valid?
true true
end end


def ping(host) def ping
# Ping sites http://en.wikipedia.org/wiki/Sitemaps # Ping sites http://en.wikipedia.org/wiki/Sitemaps
[ "http://www.google.com/webmasters/tools/ping?sitemap=http://#{host}/sitemap.xml", # TODO support other names than sitemap.xml?
"http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=http://#{host}/sitemap.xml",
"http://submissions.ask.com/ping?sitemap=http://#{host}/sitemap.xml", [ "http://www.google.com/webmasters/tools/ping?sitemap=http://#{Options.domain}/sitemap.xml",
"http://webmaster.live.com/ping.aspx?siteMap=http://#{host}/sitemap.xml" ].each do |url| "http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=http://#{Options.domain}/sitemap.xml",
"http://submissions.ask.com/ping?sitemap=http://#{Options.domain}/sitemap.xml",
"http://webmaster.live.com/ping.aspx?siteMap=http://#{Options.domain}/sitemap.xml" ].each do |url|
open(url) do |f| open(url) do |f|
if f.status[0] == "200" if f.status[0] == "200"
p "Sitemap successfully submitted to #{url}" puts "Sitemap successfully submitted to #{url}"
else else
p "Failed to submit sitemap to #{url}" puts "Failed to submit sitemap to #{url}"
end end
end end
end end
end end


class << self class << self
def generate(host, &block) def run
Generator.new(host).generate(&block) Generator.new.find_models_and_generate
end

def generate(&block)
Generator.new.generate(&block)
end end
end end


Expand Down
23 changes: 15 additions & 8 deletions lib/sitemap_generator/sitemap.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,28 +10,35 @@ class Sitemap
:never :never
] ]


def initialize(host, xml) def initialize(xml)
@xml = xml @xml = xml
@host = host
raise "!!!!domain is nil. You need specify a value for it in #{Options::CONFIG_FILE}" if Options.domain.nil?

# default_url_options[:domain] = Options.domain
end end


def add(model_instance, priority = nil, change_freq = nil) def add(model_instance, priority = nil, change_freq = nil)
last_modified = model_instance.read_attribute(:updated_on) || model_instance.read_attribute(:updated_at) last_modified = find_last_modified(model_instance)

if last_modified.nil?
last_modified = model_instance.read_attribute(:created_on) || model_instance.read_attribute(:created_at)
end


if !VALID_CHANGE_FREQ.include?(change_freq.to_sym) && !change_freq.nil? if !VALID_CHANGE_FREQ.include?(change_freq.to_sym) && !change_freq.nil?
raise "Invalid change frequency #{change_freq}, should be one of #{VALID_CHANGE_FREQ}" raise "Invalid change frequency #{change_freq}, should be one of #{VALID_CHANGE_FREQ}"
end end


@xml.url do @xml.url do
@xml.loc "http://#{@host}#{Helpers.instance.url_for(model_instance)}" @xml.loc "http://#{Options.domain}#{Helpers.instance.url_for(model_instance)}"
@xml.lastmod Helpers.instance.w3c_date(last_modified) if last_modified @xml.lastmod Helpers.instance.w3c_date(last_modified) if last_modified
@xml.changefreq change_freq.to_s if change_freq @xml.changefreq change_freq.to_s if change_freq
@xml.priority priority if priority @xml.priority priority if priority
end end
end end

def find_last_modified(model_instance)
last_modified = model_instance.read_attribute(:updated_on) || model_instance.read_attribute(:updated_at)

if last_modified.nil?
last_modified = model_instance.read_attribute(:created_on) || model_instance.read_attribute(:created_at)
end
end
end end
end end
13 changes: 8 additions & 5 deletions tasks/sitemap_generator_tasks.rake
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ namespace :sitemap do


desc "Generates the sitemap" desc "Generates the sitemap"
task :generate do task :generate do
# TODO read configuration from a YAML file require(File.join(RAILS_ROOT, 'config', 'environment'))

# Finds models and generates the sitemap
SitemapGenerator::Generator.run

# You can also generate a sitemap 'manually' like this:
=begin
SitemapGenerator::Generator.generate 'config' do |host, data| SitemapGenerator::Generator.generate 'config' do |host, data|
Post.all(:order => 'created_at desc', :limit => 5000).each do |post| Post.all(:order => 'created_at desc', :limit => 5000).each do |post|
data.add post, 0.7, :monthly data.add post, 0.7, :monthly
Expand All @@ -17,11 +23,8 @@ namespace :sitemap do
Tag.all(:order => 'created_at desc', :limit => 5000).each do |tag| Tag.all(:order => 'created_at desc', :limit => 5000).each do |tag|
data.add tag, 0.5, :weekly data.add tag, 0.5, :weekly
end end

Brand.all(:order => 'created_at desc', :limit => 5000).each do |brand|
data.add brand, 0.7, :weekly
end
end end
=end
end end


end end

0 comments on commit 772f026

Please sign in to comment.