sitemap-builder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,87 @@
1
+ SitemapBuilder
2
+ ==============
3
+
4
+ SitemapBuilder is a rail plugin for sitemap generation. It is heavily based on Adam Salter's Sitemap Generator plugin (git://github.com/adamsalter/sitemap_generator.git).
5
+
6
+ This plugin is not a fork of Adam's plugin but a complete rewriting since the goal and the way to build sitemap is different.
7
+
8
+ Indeed, I wanted a way to build seveal sitemap for the same website depending on some paramters such as the site language (see file sitemap_example.rb).
9
+
10
+ Installation
11
+ ============
12
+
13
+ no gem for now.
14
+
15
+ script/plugin install git://github.com/franck/sitemap-builder.git
16
+
17
+
18
+ Usage
19
+ =======
20
+
21
+ Create a sitemap.rb file in config/
22
+
23
+ Create a sitemap instance:
24
+
25
+ sitemap = SitemapBuilder::Sitemap.new(
26
+ :debug => true,
27
+ :host => "http://localhost:3000",
28
+ :filename => "sitemap.yml",
29
+ :ping_search_engines => true
30
+ )
31
+
32
+ sitemap.add root_path
33
+ sitemap.add pages_path
34
+ Page.all.each do |page|
35
+ sitemap.add page_path(page)
36
+ end
37
+ sitemap.add url_for(:controller => "articles", :action => "index", :only_path => true)
38
+
39
+ Then add the following lines for the sitemap generation and ping search engines (if set to true):
40
+
41
+ sitemap.generate
42
+ sitemap.ping_search_engines
43
+
44
+ Then, fire the rake task :
45
+
46
+ rake sitemapbuilder:create
47
+
48
+ I use whenever gem (git://github.com/javan/whenever.git) to schedule a new sitemap creation :
49
+
50
+ every 1.week do
51
+ rake "sitemapbuilder:create"
52
+ end
53
+
54
+
55
+ A complete example file is available inside the plugin root directory : sitemap_example.rb
56
+
57
+ Sitemap index
58
+ =============
59
+
60
+ A sitemap index builder is also provided. Example of config/sitemap.rb:
61
+
62
+ sitemap_index = SitemapBuilder::SitemapIndex.new()
63
+ sitemap = SitemapBuilder::Sitemap.new()
64
+ sitemap.add root_path
65
+ sitemap_index.sitemaps << sitemap
66
+ sitemap_index.generate
67
+
68
+ These lines create a sitemap.xml file and a sitemap_index.xml file in the public directory.
69
+
70
+ Options
71
+ =======
72
+
73
+ SitemapBuilder::Sitemap accepts several options :
74
+ * debug: generate sitemap files, verbose, does not ping search engines even if set to true (default: false)
75
+ * host: base url of your website. don't forget the http:// (default: "http://localhost:3000")
76
+ * filename: sitemap name (default: "sitemap.xml")
77
+ * sitemap_folder: indicate a folder where the sitemap is located (default: "", example: "sitemaps")
78
+ * ping_search_engines: ping google, yahoo, bing and ask.com (default: false)
79
+
80
+
81
+ TODO
82
+ ====
83
+
84
+ * test coverage
85
+ * handling lot of links and sitemap indexes
86
+
87
+ Copyright (c) 2009 Franck D'agostini, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the sitemap_builder plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the sitemap_builder plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'SitemapBuilder'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |gem|
27
+ gem.name = "sitemap-builder"
28
+ gem.summary = "Rails plugins to build sitemap.xml"
29
+ gem.description = "Rails plugins to build sitemap.xml"
30
+ gem.email = "franck.dagostini@gmail.com"
31
+ gem.homepage = "http://github.com/franck/sitemap-builder"
32
+ gem.authors = ["Franck D'agostini"]
33
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ # Include hook code here
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,26 @@
1
+ require 'action_controller'
2
+ require 'action_controller/test_process'
3
+ begin
4
+ require 'application_controller'
5
+ rescue LoadError
6
+ # Rails < 2.3
7
+ require 'application'
8
+ end
9
+
10
+ module SitemapBuilder
11
+ module Helper
12
+ def generate_sitemap
13
+ controller = ApplicationController.new
14
+ controller.request = ActionController::TestRequest.new
15
+ controller.params = {}
16
+ controller.send(:initialize_current_url)
17
+ b = controller.instance_eval{binding}
18
+ sitemap_mapper_file = File.join(RAILS_ROOT, 'config/sitemap.rb')
19
+ eval(open(sitemap_mapper_file).read, b)
20
+ end
21
+
22
+ def w3c_date(date)
23
+ date.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ module SitemapBuilder
2
+ class Link
3
+ class << self
4
+ def generate(path, host, options={})
5
+ options = {
6
+ :path => path,
7
+ :priority => 0.5,
8
+ :changefreq => 'weekly',
9
+ :lastmod => Time.now,
10
+ :host => host,
11
+ :loc => URI.join(host, path).to_s
12
+ }.merge options
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,109 @@
1
+ module SitemapBuilder
2
+ class Sitemap
3
+ attr_accessor :links, :default_host
4
+
5
+ def initialize(options={})
6
+ options = {
7
+ :debug => false,
8
+ :default_host => 'http://localhost:3000',
9
+ :filename => 'sitemap.xml',
10
+ :sitemap_folder => '',
11
+ :ping_search_engines => false
12
+ }.merge options
13
+ @debug = options[:debug]
14
+ @default_host = options[:default_host]
15
+ @filename = options[:filename]
16
+ @sitemap_folder = options[:sitemap_folder]
17
+
18
+ @links = []
19
+
20
+ deleting_previous_sitemap(fullpath)
21
+ create_sitemap_folder(@sitemap_folder)
22
+ end
23
+
24
+ def add(link, options={})
25
+ link = Link.generate(link, @default_host, options)
26
+ p link[:loc] if @debug
27
+ @links << link
28
+ end
29
+
30
+ def generate
31
+ puts "GENERATING XML FILE ..." if @debug
32
+ xml_str = ""
33
+ xml = Builder::XmlMarkup.new(:target => xml_str, :indent=>2)
34
+
35
+ xml.instruct!
36
+ xml.urlset "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
37
+ "xsi:schemaLocation" => "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd",
38
+ "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
39
+
40
+ links.each do |link|
41
+ xml.url do
42
+ xml.loc link[:loc]
43
+ xml.lastmod w3c_date(link[:lastmod]) if link[:lastmod]
44
+ xml.changefreq link[:changefreq] if link[:changefreq]
45
+ xml.priority link[:priority] if link[:priority]
46
+ end
47
+ end
48
+ end
49
+ save_file(xml_str)
50
+ ping_search_engines if @ping_search_engines
51
+ end
52
+
53
+ def ping_search_engines
54
+ puts "PINGING SEARCH ENGINES ..." if @debug
55
+ sitemap_uri = self.loc
56
+ index_location = URI.escape(sitemap_uri)
57
+ search_engines = {
58
+ :google => "http://www.google.com/webmasters/sitemaps/ping?sitemap=#{index_location}",
59
+ :yahoo => "http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=#{index_location}",
60
+ :ask => "http://submissions.ask.com/ping?sitemap=#{index_location}",
61
+ :bing => "http://www.bing.com/webmaster/ping.aspx?siteMap=#{index_location}"
62
+ }
63
+
64
+ search_engines.each do |engine, link|
65
+ begin
66
+ if @debug
67
+ puts "pinging #{engine.to_s.titleize} : #{link}"
68
+ else
69
+ open(link)
70
+ puts "Successful ping of #{engine.to_s.titleize}"
71
+ end
72
+ rescue Timeout::Error, StandardError => e
73
+ puts "Ping failed for #{engine.to_s.titleize}: #{e.inspect}"
74
+ end
75
+ end
76
+ end
77
+
78
+ def save_file(xml)
79
+ File.open(RAILS_ROOT + fullpath, "w+") do |f|
80
+ f.write(xml)
81
+ end
82
+ end
83
+
84
+ def fullpath
85
+ fullpath = "/public/"
86
+ fullpath << @sitemap_folder + '/' unless @sitemap_folder.blank?
87
+ fullpath << @filename
88
+ end
89
+
90
+ def loc
91
+ loc = @default_host + '/'
92
+ loc << @sitemap_folder + '/' unless @sitemap_folder.blank?
93
+ loc << @filename
94
+ end
95
+
96
+ def deleting_previous_sitemap(sitemap)
97
+ sitemap_files = Dir[File.join(RAILS_ROOT, sitemap)]
98
+ FileUtils.rm sitemap_files, :verbose => true
99
+ end
100
+
101
+ def create_sitemap_folder(sitemap_folder)
102
+ return if sitemap_folder.blank?
103
+ path = RAILS_ROOT + "/public/" + sitemap_folder
104
+ unless File.exists?(path) && File.directory?(path)
105
+ FileUtils.mkdir path, :mode => 0755, :verbose => true
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,41 @@
1
+ module SitemapBuilder
2
+ class SitemapIndex
3
+ attr_accessor :sitemaps
4
+
5
+ def initialize(options={})
6
+ options = {
7
+ :filename => 'sitemap_index.xml',
8
+ }.merge options
9
+ @filename = options[:filename]
10
+ @sitemaps = []
11
+ end
12
+
13
+ def generate
14
+ puts "GENERATING XML INDEX FILE ..." if @debug
15
+ xml_str = ""
16
+ xml = Builder::XmlMarkup.new(:target => xml_str, :indent=>2)
17
+
18
+ xml.instruct!
19
+ xml.sitemapindex "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
20
+ @sitemaps.each do |s|
21
+ xml.sitemap do
22
+ xml.loc s.loc
23
+ xml.lastmod w3c_date(Time.now)
24
+ end
25
+ end
26
+ end
27
+ save_file(xml_str)
28
+ end
29
+
30
+ def save_file(xml)
31
+ File.open(RAILS_ROOT + fullpath, "w+") do |f|
32
+ f.write(xml)
33
+ end
34
+ end
35
+
36
+ def fullpath
37
+ "/public/" + @filename
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+ require 'sitemap_builder/sitemap'
2
+ require 'sitemap_builder/sitemap_index'
3
+ require 'sitemap_builder/link'
4
+ require 'sitemap_builder/helper'
@@ -0,0 +1,31 @@
1
+ # Example of config/sitemap.rb
2
+ # One sitemap = one Sitemap instance
3
+
4
+ # Example below generate two sitemaps. One per languages (FR and DE)
5
+ # and then generate a sitemap inde file with the location of these two sitemaps
6
+
7
+ sitemap_index = SitemapBuilder::SitemapIndex.new()
8
+
9
+ ["fr", "de"].each do |locale|
10
+
11
+ sitemap = SitemapBuilder::Sitemap.new(
12
+ :debug => true,
13
+ :host => "http://localhost:3000",
14
+ :filename => "sitemap_#{locale}.yml",
15
+ :ping_search_engines => true
16
+ )
17
+
18
+
19
+ sitemap.add "/#{locale}"
20
+
21
+ sitemap.add articles_path(:locale => locale)
22
+ Article.find(:all).each do |a|
23
+ sitemap.add article_path(a, :locale => locale), :lastmod => a.updated_at
24
+ end
25
+
26
+ sitemap.generate
27
+ sitemap.ping_search_engines
28
+ sitemap_index.sitemaps << sitemap
29
+ end
30
+
31
+ sitemap_index.generate
@@ -0,0 +1,11 @@
1
+ namespace :sitemapbuilder do
2
+
3
+ desc 'create sitemap'
4
+ task :create => :environment do
5
+ include SitemapBuilder::Helper
6
+
7
+ puts "CREATING SITEMAPS ..."
8
+ generate_sitemap
9
+ end
10
+
11
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class SitemapBuilderTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sitemap-builder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Franck D'agostini
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-16 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Rails plugins to build sitemap.xml
22
+ email: franck.dagostini@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ files:
30
+ - MIT-LICENSE
31
+ - README
32
+ - Rakefile
33
+ - VERSION
34
+ - init.rb
35
+ - install.rb
36
+ - lib/sitemap_builder.rb
37
+ - lib/sitemap_builder/helper.rb
38
+ - lib/sitemap_builder/link.rb
39
+ - lib/sitemap_builder/sitemap.rb
40
+ - lib/sitemap_builder/sitemap_index.rb
41
+ - sitemap_example.rb
42
+ - tasks/sitemap_builder_tasks.rake
43
+ - test/sitemap_builder_test.rb
44
+ - test/test_helper.rb
45
+ - uninstall.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/franck/sitemap-builder
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Rails plugins to build sitemap.xml
78
+ test_files:
79
+ - test/sitemap_builder_test.rb
80
+ - test/test_helper.rb