sitemap 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +77 -0
- data/Rakefile +4 -32
- data/lib/sitemap.rb +20 -12
- data/lib/sitemap/configuration.rb +66 -0
- data/lib/sitemap/generator.rb +4 -4
- data/lib/sitemap/version.rb +5 -0
- data/lib/tasks/sitemap.rake +1 -0
- data/sitemap.gemspec +6 -7
- metadata +46 -26
- data/README.rdoc +0 -56
- data/test/setup.rb +0 -51
- data/test/singleton.rb +0 -24
- data/test/sitemap_test.rb +0 -193
- data/test/store_test.rb +0 -35
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Sitemap
|
2
|
+
|
3
|
+
A simple ruby on rails sitemap generator.
|
4
|
+
|
5
|
+
## Instalation
|
6
|
+
|
7
|
+
Install the gem:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem install sitemap
|
11
|
+
```
|
12
|
+
|
13
|
+
Or as a plugin:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
rails plugin install git://github.com/viseztrance/rails-sitemap.git
|
17
|
+
```
|
18
|
+
|
19
|
+
Then create the initial config file:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
rails g sitemap:install
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
In your sitemap config file, paths can be indexed as follows:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Sitemap::Generator.instance.load :host => "mywebsite.com" do
|
31
|
+
path :root, :priority => 1
|
32
|
+
path :faq, :priority => 0.5, :change_frequency => "weekly"
|
33
|
+
resources :activities, :params => { :format => "html" }
|
34
|
+
resources :articles, :objects => proc { Article.published }
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Please read the docs for a more comprehensive list of options.
|
39
|
+
|
40
|
+
Building the sitemap:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
rake sitemap:generate
|
44
|
+
```
|
45
|
+
|
46
|
+
Ping search engines:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
rake sitemap:ping
|
50
|
+
```
|
51
|
+
|
52
|
+
## Setting defaults
|
53
|
+
|
54
|
+
You may change the defaults for either *params* or *search* options as follows:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
Sitemap.configure do |config|
|
58
|
+
config.params_format = "html"
|
59
|
+
config.search_change_frequency = "monthly"
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
## Large sites
|
64
|
+
|
65
|
+
Google imposes a limit of 50000 entries per sitemap and maximum size of 10 MB. To comply with these rules,
|
66
|
+
sitemaps having over 10.000 urls are being split into multiple files. You can change this value by overriding the max urls value:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
Sitemap.configure do |config|
|
70
|
+
config.max_urls = 50000
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
This package is licensed under the MIT license and/or the Creative
|
77
|
+
Commons Attribution-ShareAlike.
|
data/Rakefile
CHANGED
@@ -1,37 +1,9 @@
|
|
1
|
-
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
2
3
|
require "rake/testtask"
|
3
4
|
|
4
|
-
spec = Gem::Specification.load(File.expand_path("sitemap.gemspec", File.dirname(__FILE__)))
|
5
|
-
|
6
|
-
desc "Default: run sitemap unit tests."
|
7
5
|
task :default => :test
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
t.libs << "lib"
|
12
|
-
t.pattern = "test/**/*_test.rb"
|
13
|
-
t.verbose = true
|
14
|
-
end
|
15
|
-
|
16
|
-
# Create the documentation.
|
17
|
-
Rake::RDocTask.new do |rdoc|
|
18
|
-
rdoc.rdoc_files.include "README.rdoc", "lib/**/*.rb"
|
19
|
-
rdoc.options = spec.rdoc_options
|
20
|
-
end
|
21
|
-
|
22
|
-
desc "Push new release to rubyforge and git tag"
|
23
|
-
task :push do
|
24
|
-
sh "git push"
|
25
|
-
puts "Tagging version #{spec.version} .."
|
26
|
-
sh "git tag v#{spec.version}"
|
27
|
-
sh "git push --tag"
|
28
|
-
puts "Building and pushing gem .."
|
29
|
-
sh "gem build #{spec.name}.gemspec"
|
30
|
-
sh "gem push #{spec.name}-#{spec.version}.gem"
|
31
|
-
end
|
32
|
-
|
33
|
-
desc "Install #{spec.name} locally"
|
34
|
-
task :install do
|
35
|
-
sh "gem build #{spec.name}.gemspec"
|
36
|
-
sh "gem install #{spec.name}-#{spec.version}.gem"
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
t.pattern = "spec/**/*_spec.rb"
|
37
9
|
end
|
data/lib/sitemap.rb
CHANGED
@@ -7,6 +7,8 @@
|
|
7
7
|
|
8
8
|
require "singleton"
|
9
9
|
require "builder"
|
10
|
+
require "sitemap/version"
|
11
|
+
require "sitemap/configuration"
|
10
12
|
require "sitemap/railtie"
|
11
13
|
require "sitemap/ping"
|
12
14
|
require "sitemap/store"
|
@@ -14,19 +16,25 @@ require "sitemap/generator"
|
|
14
16
|
|
15
17
|
module Sitemap
|
16
18
|
|
17
|
-
|
19
|
+
def self.configuration
|
20
|
+
@configuration ||= Configuration.new
|
21
|
+
end
|
18
22
|
|
19
|
-
|
23
|
+
def self.configure
|
24
|
+
yield configuration
|
25
|
+
end
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
# mattr_accessor :defaults
|
28
|
+
|
29
|
+
# self.defaults = {
|
30
|
+
# :params => {},
|
31
|
+
# :search => {
|
32
|
+
# :updated_at => proc { |obj|
|
33
|
+
# obj.updated_at.strftime("%Y-%m-%d") if obj.respond_to?(:updated_at)
|
34
|
+
# }
|
35
|
+
# },
|
36
|
+
# :query_batch_size => 500,
|
37
|
+
# :max_urls => 10000
|
38
|
+
# }
|
31
39
|
|
32
40
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Sitemap
|
2
|
+
|
3
|
+
class Configuration
|
4
|
+
|
5
|
+
module Defaults
|
6
|
+
|
7
|
+
PARAMS = {}.freeze
|
8
|
+
|
9
|
+
SEARCH = {
|
10
|
+
:updated_at => proc { |obj|
|
11
|
+
obj.updated_at.strftime("%Y-%m-%d") if obj.respond_to?(:updated_at)
|
12
|
+
}
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
QUERY_BATCH_SIZE = 500
|
16
|
+
|
17
|
+
MAX_URLS = 10000
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_accessor :data
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
reset
|
25
|
+
end
|
26
|
+
|
27
|
+
def reset
|
28
|
+
self.data = {
|
29
|
+
:params => Defaults::PARAMS.dup,
|
30
|
+
:search => Defaults::SEARCH.dup,
|
31
|
+
:query_batch_size => Defaults::QUERY_BATCH_SIZE,
|
32
|
+
:max_urls => Defaults::MAX_URLS
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def params
|
37
|
+
data[:params]
|
38
|
+
end
|
39
|
+
|
40
|
+
def search
|
41
|
+
data[:search]
|
42
|
+
end
|
43
|
+
|
44
|
+
def method_missing(method, *args, &block)
|
45
|
+
if /^(?<prefix>search|params)?_?(?<name>[a-z\_]+)(?<setter>=)?/ =~ method
|
46
|
+
if prefix
|
47
|
+
if setter
|
48
|
+
self.data[prefix.to_sym][name.to_sym] = args.first
|
49
|
+
else
|
50
|
+
data[prefix.to_sym][name.to_sym]
|
51
|
+
end
|
52
|
+
else
|
53
|
+
if setter
|
54
|
+
self.data[name.to_sym] = args.first
|
55
|
+
else
|
56
|
+
data[name.to_sym]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
else
|
60
|
+
super(method, *args, &block)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/lib/sitemap/generator.rb
CHANGED
@@ -17,7 +17,7 @@ module Sitemap
|
|
17
17
|
def initialize
|
18
18
|
self.class.send(:include, Rails.application.routes.url_helpers)
|
19
19
|
self.fragments = []
|
20
|
-
self.store = Store.new(:max_entries => Sitemap.
|
20
|
+
self.store = Store.new(:max_entries => Sitemap.configuration.max_urls)
|
21
21
|
self.store.before_reset do |entries|
|
22
22
|
self.process_fragment!
|
23
23
|
end
|
@@ -72,11 +72,11 @@ module Sitemap
|
|
72
72
|
# The resolved url would be <tt>http://mywebsite.com/frequent-questions?filter=recent</tt>.
|
73
73
|
#
|
74
74
|
def path(object, options = {})
|
75
|
-
params = Sitemap.
|
75
|
+
params = Sitemap.configuration.params.clone.merge!(options[:params] || {})
|
76
76
|
params[:host] ||= host # Use global host if none was specified.
|
77
77
|
params.merge!(params) { |type, value| get_data(object, value) }
|
78
78
|
|
79
|
-
search = Sitemap.
|
79
|
+
search = Sitemap.configuration.search.clone.merge!(options.select { |k, v| SEARCH_ATTRIBUTES.keys.include?(k) })
|
80
80
|
search.merge!(search) { |type, value| get_data(object, value) }
|
81
81
|
|
82
82
|
self.store << {
|
@@ -114,7 +114,7 @@ module Sitemap
|
|
114
114
|
get_objects = lambda {
|
115
115
|
options[:objects] ? options[:objects].call : type.to_s.classify.constantize
|
116
116
|
}
|
117
|
-
get_objects.call.find_each(:batch_size => Sitemap.
|
117
|
+
get_objects.call.find_each(:batch_size => Sitemap.configuration.query_batch_size) do |object|
|
118
118
|
path(object, link_params)
|
119
119
|
end
|
120
120
|
end
|
data/lib/tasks/sitemap.rake
CHANGED
data/sitemap.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
|
1
|
+
require File.expand_path('../lib/sitemap/version', __FILE__)
|
2
2
|
|
3
3
|
spec = Gem::Specification.new do |spec|
|
4
4
|
spec.name = "sitemap"
|
5
|
-
spec.version =
|
5
|
+
spec.version = Sitemap::VERSION
|
6
6
|
spec.summary = "Sitemap"
|
7
7
|
spec.description = "A simple ruby on rails sitemap generator"
|
8
8
|
|
@@ -12,13 +12,12 @@ spec = Gem::Specification.new do |spec|
|
|
12
12
|
|
13
13
|
spec.add_development_dependency "sqlite3"
|
14
14
|
spec.add_development_dependency "nokogiri"
|
15
|
+
spec.add_development_dependency "minitest"
|
16
|
+
spec.add_development_dependency "rails", ">= 3.0.0"
|
17
|
+
spec.add_development_dependency "nokogiri"
|
15
18
|
|
16
|
-
spec.files = Dir["{lib,docs}/**/*"] + ["README.
|
19
|
+
spec.files = Dir["{lib,docs}/**/*"] + ["README.md", "LICENSE", "Rakefile", "sitemap.gemspec"]
|
17
20
|
spec.test_files = Dir["test/**/*"]
|
18
21
|
spec.require_paths = ["lib"]
|
19
22
|
|
20
|
-
spec.has_rdoc = true
|
21
|
-
spec.rdoc_options << "--main" << "README.rdoc" << "--title" << "Sitemap" << "--line-numbers"
|
22
|
-
"--webcvs" << "http://github.com/viseztrance/rails-sitemap"
|
23
|
-
spec.extra_rdoc_files = ["README.rdoc", "LICENSE"]
|
24
23
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sitemap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-02 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sqlite3
|
16
|
-
requirement: &
|
16
|
+
requirement: &20303660 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *20303660
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: nokogiri
|
27
|
-
requirement: &
|
27
|
+
requirement: &20303240 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,43 +32,67 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *20303240
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: minitest
|
38
|
+
requirement: &20302820 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *20302820
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rails
|
49
|
+
requirement: &20302320 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *20302320
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: nokogiri
|
60
|
+
requirement: &20301900 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *20301900
|
36
69
|
description: A simple ruby on rails sitemap generator
|
37
70
|
email: daniel@viseztrance.com
|
38
71
|
executables: []
|
39
72
|
extensions: []
|
40
|
-
extra_rdoc_files:
|
41
|
-
- README.rdoc
|
42
|
-
- LICENSE
|
73
|
+
extra_rdoc_files: []
|
43
74
|
files:
|
44
75
|
- lib/tasks/sitemap.rake
|
45
76
|
- lib/sitemap/ping.rb
|
46
77
|
- lib/sitemap/railtie.rb
|
47
78
|
- lib/sitemap/store.rb
|
79
|
+
- lib/sitemap/configuration.rb
|
48
80
|
- lib/sitemap/generator.rb
|
81
|
+
- lib/sitemap/version.rb
|
49
82
|
- lib/views/index.xml.builder
|
50
83
|
- lib/views/fragment.xml.builder
|
51
84
|
- lib/sitemap.rb
|
52
85
|
- lib/generators/sitemap/USAGE
|
53
86
|
- lib/generators/sitemap/install_generator.rb
|
54
87
|
- lib/generators/sitemap/templates/sitemap.rb
|
55
|
-
- README.
|
88
|
+
- README.md
|
56
89
|
- LICENSE
|
57
90
|
- Rakefile
|
58
91
|
- sitemap.gemspec
|
59
|
-
- test/singleton.rb
|
60
|
-
- test/setup.rb
|
61
|
-
- test/store_test.rb
|
62
|
-
- test/sitemap_test.rb
|
63
92
|
homepage: http://github.com/viseztrance/rails-sitemap
|
64
93
|
licenses: []
|
65
94
|
post_install_message:
|
66
|
-
rdoc_options:
|
67
|
-
- --main
|
68
|
-
- README.rdoc
|
69
|
-
- --title
|
70
|
-
- Sitemap
|
71
|
-
- --line-numbers
|
95
|
+
rdoc_options: []
|
72
96
|
require_paths:
|
73
97
|
- lib
|
74
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -89,9 +113,5 @@ rubygems_version: 1.8.10
|
|
89
113
|
signing_key:
|
90
114
|
specification_version: 3
|
91
115
|
summary: Sitemap
|
92
|
-
test_files:
|
93
|
-
|
94
|
-
- test/setup.rb
|
95
|
-
- test/store_test.rb
|
96
|
-
- test/sitemap_test.rb
|
97
|
-
has_rdoc: true
|
116
|
+
test_files: []
|
117
|
+
has_rdoc:
|
data/README.rdoc
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
= Sitemap
|
2
|
-
|
3
|
-
A simple ruby on rails sitemap generator.
|
4
|
-
|
5
|
-
== Instalation
|
6
|
-
|
7
|
-
Install the gem:
|
8
|
-
|
9
|
-
gem install sitemap
|
10
|
-
|
11
|
-
Or as a plugin:
|
12
|
-
|
13
|
-
rails plugin install git://github.com/viseztrance/rails-sitemap.git
|
14
|
-
|
15
|
-
Then create the initial config file:
|
16
|
-
|
17
|
-
rails g sitemap:install
|
18
|
-
|
19
|
-
== Usage
|
20
|
-
|
21
|
-
In your sitemap config file, paths can be indexed as follows:
|
22
|
-
|
23
|
-
Sitemap::Generator.instance.load :host => "mywebsite.com" do
|
24
|
-
path :root, :priority => 1
|
25
|
-
path :faq, :priority => 0.5, :change_frequency => "weekly"
|
26
|
-
resources :activities, :params => { :format => "html" }
|
27
|
-
resources :articles, :objects => proc { Article.published }
|
28
|
-
end
|
29
|
-
|
30
|
-
Please read the docs for a more comprehensive list of options.
|
31
|
-
|
32
|
-
Building the sitemap:
|
33
|
-
|
34
|
-
rake sitemap:generate
|
35
|
-
|
36
|
-
Ping search engines:
|
37
|
-
|
38
|
-
rake sitemap:ping
|
39
|
-
|
40
|
-
== Setting defaults
|
41
|
-
|
42
|
-
You may change the defaults for either <tt>params</tt> or <tt>search</tt> options as follows:
|
43
|
-
|
44
|
-
Sitemap.defaults[:params] = { :format => "html" }
|
45
|
-
|
46
|
-
== Large sites
|
47
|
-
|
48
|
-
Google imposes a limit of 50000 entries per sitemap and maximum size of 10 MB. To comply with these rules,
|
49
|
-
sitemaps having over 10.000 urls are being split into multiple files. You can change this value by overriding the max urls value:
|
50
|
-
|
51
|
-
Sitemap.defaults[:max_urls] = 50000
|
52
|
-
|
53
|
-
== License
|
54
|
-
|
55
|
-
This package is licensed under the MIT license and/or the Creative
|
56
|
-
Commons Attribution-ShareAlike.
|
data/test/setup.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
module TestApp
|
2
|
-
|
3
|
-
class Application < Rails::Application
|
4
|
-
config.active_support.deprecation = :log
|
5
|
-
end
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
TestApp::Application.initialize!
|
10
|
-
|
11
|
-
TestApp::Application.routes.draw do
|
12
|
-
|
13
|
-
root :to => "main#index"
|
14
|
-
|
15
|
-
match "/questions" => "static#faq", :as => "faq"
|
16
|
-
|
17
|
-
resources :activities
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
module SitemapTestSetup
|
22
|
-
|
23
|
-
def create_db
|
24
|
-
# Database
|
25
|
-
ActiveRecord::Schema.define(:version => 1) do
|
26
|
-
create_table :activities do |t|
|
27
|
-
t.string :name
|
28
|
-
t.text :contents
|
29
|
-
t.string :location
|
30
|
-
t.boolean :published, :default => true
|
31
|
-
t.timestamps
|
32
|
-
end
|
33
|
-
end
|
34
|
-
1.upto(8) do |i|
|
35
|
-
options = {
|
36
|
-
:name => "Coding #{i}",
|
37
|
-
:contents => "Lorem ipsum dolor sit",
|
38
|
-
:location => "someplace-#{i}",
|
39
|
-
:published => (i < 6)
|
40
|
-
}
|
41
|
-
Activity.create!(options)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def drop_db
|
46
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
47
|
-
ActiveRecord::Base.connection.drop_table(table)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
data/test/singleton.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
# Reset singleton
|
2
|
-
# http://blog.ardes.com/2006/12/11/testing-singletons-with-ruby
|
3
|
-
class << Singleton
|
4
|
-
|
5
|
-
def included_with_reset(klass)
|
6
|
-
|
7
|
-
included_without_reset(klass)
|
8
|
-
|
9
|
-
class << klass
|
10
|
-
|
11
|
-
def reset_instance
|
12
|
-
Singleton.send :__init__, self
|
13
|
-
self
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
alias_method :included_without_reset, :included
|
21
|
-
|
22
|
-
alias_method :included, :included_with_reset
|
23
|
-
|
24
|
-
end
|
data/test/sitemap_test.rb
DELETED
@@ -1,193 +0,0 @@
|
|
1
|
-
require "test/unit"
|
2
|
-
require "rubygems"
|
3
|
-
require "rails"
|
4
|
-
require "action_controller/railtie" # Rails 3.1
|
5
|
-
require "active_record"
|
6
|
-
require "nokogiri"
|
7
|
-
|
8
|
-
require File.expand_path("singleton", File.dirname(__FILE__))
|
9
|
-
require File.expand_path("setup", File.dirname(__FILE__))
|
10
|
-
require File.expand_path("../lib/sitemap", File.dirname(__FILE__))
|
11
|
-
|
12
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
13
|
-
|
14
|
-
class Activity < ActiveRecord::Base; end
|
15
|
-
|
16
|
-
class SitemapTest < Test::Unit::TestCase
|
17
|
-
|
18
|
-
include SitemapTestSetup
|
19
|
-
|
20
|
-
def setup
|
21
|
-
create_db
|
22
|
-
Sitemap.defaults[:max_urls] = 10000
|
23
|
-
Sitemap::Generator.reset_instance
|
24
|
-
end
|
25
|
-
|
26
|
-
def teardown
|
27
|
-
drop_db
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_xml_response
|
31
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") {}
|
32
|
-
doc = Nokogiri::XML(Sitemap::Generator.instance.render)
|
33
|
-
assert doc.errors.empty?
|
34
|
-
assert_equal doc.root.name, "urlset"
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_path_route
|
38
|
-
urls = ["http://someplace.com/", "http://someplace.com/questions"]
|
39
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
40
|
-
path :root
|
41
|
-
path :faq
|
42
|
-
end
|
43
|
-
Sitemap::Generator.instance.build!
|
44
|
-
doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
|
45
|
-
elements = doc.xpath "//url/loc"
|
46
|
-
assert_equal elements.length, urls.length
|
47
|
-
elements.each_with_index do |element, i|
|
48
|
-
assert_equal element.text, urls[i]
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_resources_route
|
53
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
54
|
-
resources :activities
|
55
|
-
end
|
56
|
-
Sitemap::Generator.instance.build!
|
57
|
-
doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
|
58
|
-
elements = doc.xpath "//url/loc"
|
59
|
-
assert_equal elements.length, Activity.count + 1
|
60
|
-
assert_equal elements.first.text, "http://someplace.com/activities"
|
61
|
-
elements[1..-1].each_with_index do |element, i|
|
62
|
-
assert_equal element.text, "http://someplace.com/activities/#{i + 1}"
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_custom_resource_objects
|
67
|
-
activities = proc { Activity.where(:published => true) }
|
68
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
69
|
-
resources :activities, :objects => activities, :skip_index => true
|
70
|
-
end
|
71
|
-
Sitemap::Generator.instance.build!
|
72
|
-
doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
|
73
|
-
elements = doc.xpath "//url/loc"
|
74
|
-
assert_equal elements.length, activities.call.length
|
75
|
-
activities.call.each_with_index do |activity, i|
|
76
|
-
assert_equal elements[i].text, "http://someplace.com/activities/%d" % activity.id
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_params_options
|
81
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
82
|
-
path :faq, :params => { :host => "anotherplace.com", :format => "html", :filter => "recent" }
|
83
|
-
end
|
84
|
-
Sitemap::Generator.instance.build!
|
85
|
-
doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
|
86
|
-
elements = doc.xpath "//url/loc"
|
87
|
-
assert_equal elements.first.text, "http://anotherplace.com/questions.html?filter=recent"
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_params_blocks
|
91
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
92
|
-
resources :activities, :skip_index => true, :params => { :host => proc { |obj| [obj.location, host].join(".") } }
|
93
|
-
end
|
94
|
-
activities = Activity.all
|
95
|
-
Sitemap::Generator.instance.build!
|
96
|
-
doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
|
97
|
-
elements = doc.xpath "//url/loc"
|
98
|
-
elements.each_with_index do |element, i|
|
99
|
-
assert_equal element.text, "http://%s.someplace.com/activities/%d" % [activities[i].location, activities[i].id]
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_search_attribute_options
|
104
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
105
|
-
path :faq, :priority => 1, :change_frequency => "always"
|
106
|
-
resources :activities, :change_frequency => "weekly"
|
107
|
-
end
|
108
|
-
Sitemap::Generator.instance.build!
|
109
|
-
doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
|
110
|
-
assert_equal doc.xpath("//url/priority").first.text, "1"
|
111
|
-
elements = doc.xpath "//url/changefreq"
|
112
|
-
assert_equal elements[0].text, "always"
|
113
|
-
elements[1..-1].each do |element|
|
114
|
-
assert_equal element.text, "weekly"
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_search_attribute_blocks
|
119
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
120
|
-
resources :activities, :priority => proc { |obj| obj.id <= 2 ? 1 : 0.5 }, :skip_index => true
|
121
|
-
end
|
122
|
-
activities = Activity.all
|
123
|
-
doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
|
124
|
-
elements = doc.xpath "//url/priority"
|
125
|
-
elements.each_with_index do |element, i|
|
126
|
-
value = activities[i].id <= 2 ? "1" : "0.5"
|
127
|
-
assert_equal element.text, value
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
def test_discards_empty_search_attributes # Empty or false (boolean).
|
132
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
133
|
-
path :faq, :priority => "", :change_frequency => lambda { |e| return false}, :updated_at => Date.today
|
134
|
-
end
|
135
|
-
Sitemap::Generator.instance.build!
|
136
|
-
doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
|
137
|
-
assert_equal doc.xpath("//url/priority").count, 0
|
138
|
-
assert_equal doc.xpath("//url/changefreq").count, 0
|
139
|
-
assert_equal doc.xpath("//url/lastmod").text, Date.today.to_s
|
140
|
-
end
|
141
|
-
|
142
|
-
def test_file_url
|
143
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") {}
|
144
|
-
assert_equal Sitemap::Generator.instance.file_url, "http://someplace.com/sitemap.xml"
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_save_creates_file
|
148
|
-
path = File.join(Dir.tmpdir, "sitemap.xml")
|
149
|
-
File.unlink(path) if File.exist?(path)
|
150
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
151
|
-
resources :activities
|
152
|
-
end
|
153
|
-
Sitemap::Generator.instance.build!
|
154
|
-
Sitemap::Generator.instance.save(path)
|
155
|
-
assert File.exist?(path)
|
156
|
-
File.unlink(path)
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_saves_fragments
|
160
|
-
Sitemap.defaults[:max_urls] = 2
|
161
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
162
|
-
path :root
|
163
|
-
path :root
|
164
|
-
path :root
|
165
|
-
path :root
|
166
|
-
end
|
167
|
-
path = File.join(Dir.tmpdir, "sitemap.xml")
|
168
|
-
root = File.join(Dir.tmpdir, "sitemaps") # Directory is being removed at the end of the test.
|
169
|
-
assert !File.directory?(root)
|
170
|
-
Sitemap::Generator.instance.build!
|
171
|
-
Sitemap::Generator.instance.save(path)
|
172
|
-
1.upto(2) { |i|
|
173
|
-
assert File.exists?(File.join(root, "sitemap-fragment-#{i}.xml"))
|
174
|
-
}
|
175
|
-
FileUtils.rm_rf(root)
|
176
|
-
end
|
177
|
-
|
178
|
-
def test_fragments_index
|
179
|
-
Sitemap.defaults[:max_urls] = 2
|
180
|
-
Sitemap::Generator.instance.load(:host => "someplace.com") do
|
181
|
-
path :root
|
182
|
-
path :root
|
183
|
-
path :root
|
184
|
-
path :root
|
185
|
-
path :root
|
186
|
-
end
|
187
|
-
Sitemap::Generator.instance.build!
|
188
|
-
doc = Nokogiri::HTML(Sitemap::Generator.instance.render("index"))
|
189
|
-
elements = doc.xpath "//sitemap"
|
190
|
-
assert_equal Sitemap::Generator.instance.fragments.length, 3
|
191
|
-
end
|
192
|
-
|
193
|
-
end
|
data/test/store_test.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require "test/unit"
|
2
|
-
require "rubygems"
|
3
|
-
|
4
|
-
class StoreTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_append_entries
|
7
|
-
store = Sitemap::Store.new(:max_entries => 1000)
|
8
|
-
3.times { store << "contents" }
|
9
|
-
assert_equal store.entries.length, 3
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_reset_entries_limit
|
13
|
-
store = Sitemap::Store.new(:max_entries => 2)
|
14
|
-
2.times { store << "contents" }
|
15
|
-
assert_equal store.entries.length, 2
|
16
|
-
store << "contents"
|
17
|
-
assert_equal store.entries.length, 1
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_reset_callback
|
21
|
-
store = Sitemap::Store.new(:max_entries => 2)
|
22
|
-
store.before_reset do |entries|
|
23
|
-
store.instance_variable_set("@callback_data", entries.join(", "))
|
24
|
-
end
|
25
|
-
3.times { |i| store << "item #{i + 1}" }
|
26
|
-
assert_equal store.instance_variable_get("@callback_data"), "item 1, item 2"
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_increments_reset_count
|
30
|
-
store = Sitemap::Store.new(:max_entries => 2)
|
31
|
-
5.times { store << "contents" }
|
32
|
-
assert_equal store.reset_count, 2
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|