sitemap_builder 0.2.1

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,35 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+
7
+ desc 'Default: run unit tests.'
8
+ task :default => :test
9
+
10
+ desc 'Test the sitemap_builder plugin.'
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'lib'
13
+ t.libs << 'test'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = true
16
+ end
17
+
18
+ desc 'Generate documentation for the sitemap_builder plugin.'
19
+ Rake::RDocTask.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = 'SitemapBuilder'
22
+ rdoc.options << '--line-numbers' << '--inline-source'
23
+ rdoc.rdoc_files.include('README')
24
+ rdoc.rdoc_files.include('lib/**/*.rb')
25
+ end
26
+
27
+ require 'jeweler'
28
+ Jeweler::Tasks.new do |gem|
29
+ gem.name = "sitemap_builder"
30
+ gem.summary = "Rails plugins to build sitemap.xml"
31
+ gem.description = "Rails plugins to build sitemap.xml"
32
+ gem.email = "franck.dagostini@gmail.com"
33
+ gem.homepage = "http://github.com/franck/sitemap_builder"
34
+ gem.authors = ["Franck D'agostini"]
35
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
data/init.rb ADDED
File without changes
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,22 @@
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
+ include Rails.application.routes.url_helpers
14
+ sitemap_mapper_file = File.join(Rails.root, 'config/sitemap.rb')
15
+ eval(open(sitemap_mapper_file).read)
16
+ end
17
+
18
+ def w3c_date(date)
19
+ date.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
20
+ end
21
+ end
22
+ 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,12 @@
1
+ require 'sitemap_builder'
2
+ require 'rails'
3
+
4
+ module SitemapBuilder
5
+ class Railtie < Rails::Railtie
6
+
7
+ rake_tasks do
8
+ load "tasks/sitemap_builder_tasks.rake"
9
+ end
10
+
11
+ end
12
+ 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,5 @@
1
+ require 'sitemap_builder/sitemap'
2
+ require 'sitemap_builder/sitemap_index'
3
+ require 'sitemap_builder/link'
4
+ require 'sitemap_builder/helper'
5
+ require "sitemap_builder/railtie" if defined?(Rails) && Rails.version >= "3"
@@ -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,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sitemap_builder}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Franck D'agostini"]
12
+ s.date = %q{2011-02-17}
13
+ s.description = %q{Rails plugins to build sitemap.xml}
14
+ s.email = %q{franck.dagostini@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "init.rb",
24
+ "install.rb",
25
+ "lib/sitemap_builder.rb",
26
+ "lib/sitemap_builder/helper.rb",
27
+ "lib/sitemap_builder/link.rb",
28
+ "lib/sitemap_builder/railtie.rb",
29
+ "lib/sitemap_builder/sitemap.rb",
30
+ "lib/sitemap_builder/sitemap_index.rb",
31
+ "lib/tasks/sitemap_builder_tasks.rake",
32
+ "sitemap_builder.gemspec",
33
+ "sitemap_example.rb",
34
+ "test/sitemap_builder_test.rb",
35
+ "test/test_helper.rb",
36
+ "uninstall.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/franck/sitemap_builder}
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.7}
41
+ s.summary = %q{Rails plugins to build sitemap.xml}
42
+ s.test_files = [
43
+ "test/sitemap_builder_test.rb",
44
+ "test/test_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ else
53
+ end
54
+ else
55
+ end
56
+ end
57
+
@@ -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,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,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sitemap_builder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 1
9
+ version: 0.2.1
10
+ platform: ruby
11
+ authors:
12
+ - Franck D'agostini
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-17 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/railtie.rb
40
+ - lib/sitemap_builder/sitemap.rb
41
+ - lib/sitemap_builder/sitemap_index.rb
42
+ - lib/tasks/sitemap_builder_tasks.rake
43
+ - sitemap_builder.gemspec
44
+ - sitemap_example.rb
45
+ - test/sitemap_builder_test.rb
46
+ - test/test_helper.rb
47
+ - uninstall.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/franck/sitemap_builder
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.7
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Rails plugins to build sitemap.xml
80
+ test_files:
81
+ - test/sitemap_builder_test.rb
82
+ - test/test_helper.rb