Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* adding sitemap for Google/Bing/Yahoo and ping task
  • Loading branch information
brandur committed May 31, 2011
1 parent 584fea5 commit 2e84a53
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Rakefile
Expand Up @@ -47,6 +47,24 @@ task :init do
$stdout.puts 'Initialized Askja content'
end

desc 'Ping major search engines to indicate that our sitemap has been updated'
task :ping => :environment do
if !APP_CONFIG['url']
$stderr.puts "\t[error] Missing config key 'url'."
exit 1
end

include ActionController::UrlWriter
sitemap = APP_CONFIG['url'] + sitemap_path(:format => :xml)
[ "http://www.google.com/webmasters/sitemaps/ping?sitemap=",
"http://www.bing.com/webmaster/ping.aspx?siteMap=" ,
"http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=YahooDemo&url="
].each do |url|
$stdout.puts "Pinging #{url}#{sitemap}"
#puts `curl #{url}#{sitemap}`
end
end

desc 'Updates all entities'
task :update => [ 'update:series', 'update:articles', 'update:related' ]

Expand Down Expand Up @@ -154,6 +172,6 @@ def update_model(name, klass, cache_controller)
updated = ModelLoader.new(klass, cache_controller).load(paths, is_forced) do |model|
$stdout.puts "\t[ok] Saved '#{model.title}'"
end
$stdout.puts "Updated #{updated.count} series"
$stdout.puts "Updated #{updated.count} #{name}"
end

59 changes: 59 additions & 0 deletions app/controllers/sitemap_controller.rb
@@ -0,0 +1,59 @@
class SitemapController < ApplicationController
#layout nil

def index
base_url = request.protocol + request.host_with_port
latest_article = Article.published.first
@pages = []

@pages << Page.new(
:location => base_url,
:last_modified_at => latest_article.published_at,
:change_frequency => 'daily'
)
@pages << Page.new(
:location => base_url + archive_path,
:last_modified_at => latest_article.published_at,
:change_frequency => 'daily'
)

Article.published.each do |article|
@pages << Page.new(
:location => base_url + article_path(article),
:last_modified_at => article.last_updated_at,
:change_frequency => 'weekly'
)
end
Series.active.each do |series|
articles = series.articles
last_modified_at = if articles && articles.last.published_at > series.updated_at
articles.last.published_at
else
series.updated_at
end
@pages << Page.new(
:location => base_url + series_path(series),
:last_modified_at => last_modified_at,
:change_frequency => 'daily'
)
end

if stale?(:etag => latest_article, :last_modified => latest_article.updated_at.utc)
respond_to do |format|
format.xml { render :layout => nil }
end
end
end

private

class Page
attr_accessor :location, :last_modified_at, :change_frequency

def initialize(params)
self.location = params[:location]
self.last_modified_at = params[:updated_at]
self.change_frequency = params[:change_frequency]
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/sitemap_helper.rb
@@ -0,0 +1,2 @@
module SitemapHelper
end
11 changes: 11 additions & 0 deletions app/views/sitemap/index.xml.builder
@@ -0,0 +1,11 @@
xml.instruct! :xml, :version=>'1.0'
xml.tag! 'urlset', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9' do
@pages.each do |page|
xml.tag! 'url' do
xml.tag! 'loc', page.location
xml.tag! 'lastmod', page.last_modified_at
xml.tag! 'changefreq', page.change_frequency
#xml.tag! 'priority', '0.5'
end
end
end
5 changes: 5 additions & 0 deletions config/config.yml.example
@@ -1,2 +1,7 @@
title: Askja

# Used for `rake top` to get top articles from Analytics
analytics_id:

# Used for `rake ping`, no trailing slash
url:
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -11,6 +11,8 @@
match 'atom.xml', :to => 'articles#index', :format => 'atom'
match 'atom', :to => 'articles#index', :format => 'atom'

match 'sitemap(.:format)', :to => 'sitemap#index', :as => :sitemap

root :to => 'articles#index'

# The priority is based upon order of creation:
Expand Down

0 comments on commit 2e84a53

Please sign in to comment.