sitemap_notifier 0.0.4 → 1.0.0
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/.gitignore +18 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +110 -0
- data/Rakefile +10 -0
- data/lib/generators/sitemap_notifier/USAGE +8 -0
- data/lib/generators/sitemap_notifier/install_generator.rb +10 -0
- data/lib/generators/sitemap_notifier/templates/initializer.rb +26 -11
- data/lib/sitemap_notifier.rb +1 -0
- data/lib/sitemap_notifier/active_record.rb +24 -8
- data/lib/sitemap_notifier/notifier.rb +173 -51
- data/lib/sitemap_notifier/version.rb +3 -0
- data/sitemap_notifier.gemspec +26 -0
- data/test/db.rb +42 -0
- data/test/notifier_test.rb +168 -0
- data/test/test_helper.rb +16 -0
- metadata +143 -43
- data/lib/generators/sitemap_notifier/sitemap_notifier_generator.rb +0 -7
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Lasse Bunk
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
[](http://travis-ci.org/lassebunk/sitemap_notifier)
|
2
|
+
|
3
|
+
Sitemap Notifier
|
4
|
+
================
|
5
|
+
|
6
|
+
Sitemap Notifier is a Ruby on Rails plugin that, when installed, automatically notifies Google, Bing, and Yahoo of changes to your models, i.e. changes to your sitemap. It also works in conjunction with the [dynamic_sitemaps](https://github.com/lassebunk/dynamic_sitemaps) plugin.
|
7
|
+
|
8
|
+
Installation
|
9
|
+
------------
|
10
|
+
|
11
|
+
In your *Gemfile*:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'sitemap_notifier'
|
15
|
+
```
|
16
|
+
|
17
|
+
And run:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
$ bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
Example
|
24
|
+
-------
|
25
|
+
|
26
|
+
Start by generating an initializer:
|
27
|
+
|
28
|
+
```bash
|
29
|
+
$ rails generate sitemap_notifier:install
|
30
|
+
```
|
31
|
+
|
32
|
+
Then, in *config/initializers/sitemap_notifier.rb*:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
SitemapNotifier::Notifier.configure do |config|
|
36
|
+
# Set URL to your sitemap. This is required.
|
37
|
+
# This can also be configured per model basis,
|
38
|
+
# see https://github.com/lassebunk/sitemap_notifier#per-model-sitemap-url
|
39
|
+
config.sitemap_url = "http://example.com/sitemap.xml"
|
40
|
+
|
41
|
+
# Models that should trigger notification of search engines.
|
42
|
+
# Default is to trigger on all creates, updates, and deletes.
|
43
|
+
config.models = [Article, Category]
|
44
|
+
|
45
|
+
# Or, if you want to specify on which actions to notify search engines:
|
46
|
+
config.models = { Article => [:create, :destroy],
|
47
|
+
Product => :update,
|
48
|
+
Page => :all }
|
49
|
+
|
50
|
+
# Enabled in which environments – default is [:production]
|
51
|
+
config.environments = [:development, :production]
|
52
|
+
|
53
|
+
# Delay to wait between notifications – default is 10 minutes
|
54
|
+
config.delay = 2.minutes
|
55
|
+
|
56
|
+
# Additional urls to ping
|
57
|
+
config.ping_urls << "http://localhost:3000/ping?sitemap=%{sitemap_url}"
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
Then create or update a model:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
Article.create :title => "My New Article"
|
65
|
+
```
|
66
|
+
|
67
|
+
The search engines are then automatically notified.
|
68
|
+
|
69
|
+
After installation
|
70
|
+
------------------
|
71
|
+
|
72
|
+
After you install and configure the plugin, it'll automatically notify Google, Bing, and Yahoo every time you update a model. After each notification, it'll wait 10 minutes (by default) before notifying again. This is to ensure that for example a batch update won't trigger an equal amount of notifications.
|
73
|
+
|
74
|
+
Customization
|
75
|
+
-------------
|
76
|
+
|
77
|
+
### Per model sitemap URL
|
78
|
+
|
79
|
+
The sitemaps defaults to what you set in `SitemapNotifier::Notifier.sitemap_url`. If you want to use a different sitemap URL based on data in your models, you can override the `sitemap_url` method of each of your models like this:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class Product < ActiveRecord::Base
|
83
|
+
belongs_to :site
|
84
|
+
|
85
|
+
def sitemap_url
|
86
|
+
"http://#{site.domain}/sitemap.xml"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
### Conditional notifications
|
92
|
+
|
93
|
+
You can decide on model level whether the search engines should be pinged by overriding the `notify_sitemap?` method of your models:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
class Product < ActiveRecord::Base
|
97
|
+
belongs_to :site
|
98
|
+
|
99
|
+
def notify_sitemap?
|
100
|
+
site.id == 1
|
101
|
+
end
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
Documentation
|
106
|
+
-------------
|
107
|
+
|
108
|
+
* [Complete documentation](http://rubydoc.info/github/lassebunk/sitemap_notifier)
|
109
|
+
|
110
|
+
Copyright © 2010-2013 Lasse Bunk, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
module SitemapNotifier
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
desc "Creates an initializer for breadcrumbs in config/initializers/sitemap_notifier.rb"
|
6
|
+
def create_initializer
|
7
|
+
copy_file "initializer.rb", "config/initializers/sitemap_notifier.rb"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -1,15 +1,30 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
SitemapNotifier::Notifier.configure do |config|
|
2
|
+
# Set URL to your sitemap. This is required.
|
3
|
+
# config.sitemap_url = "http://example.com/sitemap.xml"
|
4
|
+
#
|
5
|
+
# This can also be configured per model basis,
|
6
|
+
# see https://github.com/lassebunk/sitemap_notifier#per-model-sitemap-url
|
3
7
|
|
4
|
-
#
|
5
|
-
#
|
8
|
+
# Models that should trigger notification of search engines.
|
9
|
+
# It will trigger on creates, updates, and destroys af those models.
|
10
|
+
# config.models = [Article, Category]
|
6
11
|
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
12
|
+
# To trigger notifications on the specified actions only:
|
13
|
+
# config.models = { Article => [:create, :destroy],
|
14
|
+
# Product => :update,
|
15
|
+
# Page => :all }
|
10
16
|
|
11
|
-
#
|
12
|
-
#
|
17
|
+
# Enabled in which environments – default is [:production]
|
18
|
+
# config.environments = [:development, :production]
|
19
|
+
# or
|
20
|
+
# config.environments = :all
|
13
21
|
|
14
|
-
#
|
15
|
-
#
|
22
|
+
# Delay to wait between notifications – default is 10 minutes
|
23
|
+
# config.delay = 2.minutes
|
24
|
+
|
25
|
+
# Additional urls to ping
|
26
|
+
# config.ping_urls << "http://localhost:3000/ping?sitemap=%{sitemap_url}"
|
27
|
+
|
28
|
+
# If you don't want the notifications to run in the background
|
29
|
+
# config.background = false
|
30
|
+
end
|
data/lib/sitemap_notifier.rb
CHANGED
@@ -1,15 +1,31 @@
|
|
1
|
+
require "active_record"
|
2
|
+
|
1
3
|
module SitemapNotifier
|
2
4
|
module ActiveRecord
|
3
|
-
def
|
4
|
-
|
5
|
+
def self.included(base)
|
6
|
+
[:create, :update, :destroy].each do |action|
|
7
|
+
base.send("after_#{action}") do
|
8
|
+
notify_sitemap(action)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def notify_sitemap(action)
|
14
|
+
notifier = SitemapNotifier::Notifier
|
5
15
|
|
6
|
-
if notifier.
|
7
|
-
notifier.
|
16
|
+
if (notifier.notify_of_changes_to?(self.class, action)) && notify_sitemap?
|
17
|
+
notifier.run(sitemap_url)
|
8
18
|
end
|
9
19
|
end
|
10
|
-
|
11
|
-
def
|
12
|
-
|
20
|
+
|
21
|
+
def notify_sitemap?
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
def sitemap_url
|
26
|
+
SitemapNotifier::Notifier.sitemap_url
|
13
27
|
end
|
14
28
|
end
|
15
|
-
end
|
29
|
+
end
|
30
|
+
|
31
|
+
ActiveRecord::Base.send :include, SitemapNotifier::ActiveRecord
|
@@ -5,81 +5,203 @@ require 'cgi'
|
|
5
5
|
module SitemapNotifier
|
6
6
|
class Notifier
|
7
7
|
class << self
|
8
|
-
# sitemap
|
8
|
+
# The default sitemap URL to send to search engines.
|
9
|
+
#
|
10
|
+
# Example:
|
11
|
+
#
|
12
|
+
# SitemapNotifier::Notifier.sitemap_url = "http://test.dk/sitemap.xml"
|
13
|
+
#
|
14
|
+
# The default sitemap URL can be overridden on model level:
|
15
|
+
#
|
16
|
+
# class Product < ActiveRecord::Base
|
17
|
+
# belongs_to :site
|
18
|
+
#
|
19
|
+
# def sitemap_url
|
20
|
+
# "http://#{site.domain}/sitemap.xml"
|
21
|
+
# end
|
22
|
+
# end
|
9
23
|
attr_accessor :sitemap_url
|
10
|
-
|
11
|
-
#
|
12
|
-
|
24
|
+
|
25
|
+
# Set models that should trigger notification.
|
26
|
+
#
|
27
|
+
# If you supply a hash, you can specify which actions should trigger notifications using +:create+, +:update+, +:destroy+, or +:all+.
|
28
|
+
#
|
29
|
+
# If you supply an array, all creates, updates, and deletes will trigger notification.
|
30
|
+
#
|
31
|
+
# Example:
|
32
|
+
#
|
33
|
+
# # Will trigger notifications on creates, updates, and destroys on articles and products:
|
34
|
+
# SitemapNotifier::Notifier.models = [Article, Product]
|
35
|
+
#
|
36
|
+
# # Will trigger notifications on the specified actions:
|
37
|
+
# config.models = { Article => [:create, :destroy],
|
38
|
+
# Product => :update,
|
39
|
+
# Page => :all }
|
13
40
|
def models
|
14
|
-
@models ||=
|
41
|
+
@models ||= {}
|
15
42
|
end
|
16
43
|
|
17
|
-
|
18
|
-
|
44
|
+
def models=(hash_or_array)
|
45
|
+
if hash_or_array == :all
|
46
|
+
@models = { :all => :all }
|
47
|
+
elsif hash_or_array.is_a?(Array)
|
48
|
+
@models = Hash[hash_or_array.map { |model| [model, :all] }]
|
49
|
+
else
|
50
|
+
@models = hash_or_array
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Seconds to wait between notifications of the search engines.
|
55
|
+
# Default: 10 minutes
|
56
|
+
#
|
57
|
+
# Example:
|
58
|
+
#
|
59
|
+
# SitemapNotifier::Notifier.delay = 2.minutes
|
19
60
|
def delay
|
20
61
|
@delay ||= 600
|
21
62
|
end
|
63
|
+
attr_writer :delay
|
22
64
|
|
23
|
-
#
|
24
|
-
|
65
|
+
# Environments where sitemap notifications should be triggered.
|
66
|
+
#
|
67
|
+
# Example:
|
68
|
+
#
|
69
|
+
# SitemapNotifier::Notifier.environments = [:development, :production]
|
25
70
|
def environments
|
26
71
|
@environments ||= [:production]
|
27
72
|
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
|
73
|
+
attr_writer :environments
|
74
|
+
|
75
|
+
# Current environment. If not set manually, it returns the current Rails environment, like +:development:+ or +:production+.
|
76
|
+
def env
|
77
|
+
defined?(Rails) ? Rails.env.to_sym : @env
|
78
|
+
end
|
79
|
+
attr_writer :env
|
80
|
+
|
81
|
+
# URLs to be pinged / notified of changes to sitemap.
|
82
|
+
# Default is Google and Bing (and therefore Yahoo).
|
83
|
+
#
|
84
|
+
# Example:
|
85
|
+
#
|
86
|
+
# SitemapNotifier::Notifier.ping_urls << "http://mydomain.com/ping?sitemap=%{sitemap_url}"
|
87
|
+
#
|
88
|
+
# The sitemap URL will then be interpolated into the ping URL at runtime.
|
89
|
+
def ping_urls
|
90
|
+
@ping_urls ||= ["http://www.google.com/webmasters/sitemaps/ping?sitemap=%{sitemap_url}",
|
91
|
+
"http://www.bing.com/webmaster/ping.aspx?siteMap=%{sitemap_url}"]
|
35
92
|
# no Yahoo here, as they will be using Bing from september 15th, 2011
|
36
93
|
end
|
94
|
+
attr_writer :ping_urls
|
95
|
+
|
96
|
+
# Whether to run the notification pings in the background.
|
97
|
+
# Default: +true+
|
98
|
+
#
|
99
|
+
# Example:
|
100
|
+
#
|
101
|
+
# SitemapNotifier::Notifier.background = false
|
102
|
+
def background
|
103
|
+
return @background if defined?(@background)
|
104
|
+
@background = true
|
105
|
+
end
|
106
|
+
attr_writer :background
|
37
107
|
|
38
|
-
#
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
raise "sitemap_url not set
|
43
|
-
|
44
|
-
if (environments == :all || environments.include?(
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
108
|
+
# Runs the search engine notifications after checking environment and delay.
|
109
|
+
def run(url = nil)
|
110
|
+
url ||= sitemap_url
|
111
|
+
|
112
|
+
raise "sitemap_url not set - use SitemapNotifier::Notifier.sitemap_url = 'http://domain.com/sitemap.xml'" unless url
|
113
|
+
|
114
|
+
if (environments == :all || environments.include?(env)) && ping?(url)
|
115
|
+
ping_all url
|
116
|
+
sitemap_notified url
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Pings all the configured search engines with the supplied +url+.
|
121
|
+
def ping_all(url)
|
122
|
+
p = Proc.new do
|
123
|
+
Rails.logger.info "Notifying search engines of changes to sitemap #{url}..." if defined?(Rails)
|
124
|
+
|
125
|
+
escaped_url = escape_sitemap_url(url)
|
126
|
+
|
127
|
+
ping_urls.each do |url|
|
128
|
+
url.gsub! "%{sitemap_url}", escaped_url
|
129
|
+
if ping_url(url)
|
130
|
+
Rails.logger.info "Successfully notified #{url}" if defined?(Rails)
|
131
|
+
else
|
132
|
+
Rails.logger.info "Failed to notify #{url}" if defined?(Rails)
|
54
133
|
end
|
55
|
-
|
56
|
-
sleep delay
|
57
|
-
exit
|
58
134
|
end
|
59
135
|
end
|
136
|
+
|
137
|
+
background ? Thread.new(&p) : p.call
|
60
138
|
end
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
139
|
+
|
140
|
+
# Yields a configuration block to configure the notifier.
|
141
|
+
#
|
142
|
+
# Example:
|
143
|
+
#
|
144
|
+
# SitemapNotifier::Notifier.configure do |config|
|
145
|
+
# config.sitemap_url = "http://test.dk/sitemap.xml"
|
146
|
+
# end
|
147
|
+
def configure
|
148
|
+
yield self
|
149
|
+
if models.is_a?(Array) && models.empty? && defined?(Rails)
|
150
|
+
Rails.logger.warn "SitemapNotifier was configured without any models to trigger notifications. Search engines will therefore not be notified."
|
70
151
|
end
|
71
152
|
end
|
153
|
+
|
154
|
+
# For testing purposes. Resets all configuration and information about notified sitemaps.
|
155
|
+
def reset
|
156
|
+
[:@sitemap_url, :@models, :@delay, :@environments, :@ping_urls, :@background, :@notified_urls].each do |var|
|
157
|
+
remove_instance_variable var if instance_variable_defined?(var)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
# URL encodes the given +url+.
|
162
|
+
def escape_sitemap_url(url)
|
163
|
+
CGI::escape(url)
|
164
|
+
end
|
72
165
|
|
73
|
-
|
74
|
-
|
75
|
-
|
166
|
+
# Makes a GET request to the supplied +url+. Returns +true+ when successful, +false+ otherwise.
|
167
|
+
def ping_url(url)
|
76
168
|
begin
|
77
|
-
|
78
|
-
true
|
79
|
-
rescue
|
80
|
-
false
|
169
|
+
Net::HTTP.get(URI.parse(url))
|
170
|
+
return true
|
171
|
+
rescue
|
172
|
+
return false
|
81
173
|
end
|
82
174
|
end
|
175
|
+
|
176
|
+
# Returns +true+ if search engines should be modified. Returns +false+ if the configured +delay+ hasn't been met.
|
177
|
+
def ping?(sitemap_url)
|
178
|
+
last_notified = notified_at(sitemap_url)
|
179
|
+
last_notified.nil? || Time.now >= last_notified + delay
|
180
|
+
end
|
181
|
+
|
182
|
+
# Holds notified URL times like notified_urls["http://mydomain.com/sitemap.xml"] # => 2013-02-05 19:10:29 +0100
|
183
|
+
def notified_urls
|
184
|
+
@notified_urls ||= {}
|
185
|
+
end
|
186
|
+
|
187
|
+
# Returns the latest notification time for the given URL.
|
188
|
+
def notified_at(sitemap_url)
|
189
|
+
notified_urls[sitemap_url]
|
190
|
+
end
|
191
|
+
|
192
|
+
# Stores notification time for the given URL.
|
193
|
+
def sitemap_notified(sitemap_url)
|
194
|
+
notified_urls[sitemap_url] = Time.now
|
195
|
+
end
|
196
|
+
|
197
|
+
# Returns +true+ if changes to the given model class should trigger notifications.
|
198
|
+
def notify_of_changes_to?(model, action)
|
199
|
+
valid_actions = models[model] || models[:all]
|
200
|
+
|
201
|
+
return valid_actions == :all ||
|
202
|
+
valid_actions == action ||
|
203
|
+
(valid_actions.is_a?(Array) && valid_actions.include?(action))
|
204
|
+
end
|
83
205
|
end
|
84
206
|
end
|
85
207
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sitemap_notifier/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "sitemap_notifier"
|
8
|
+
gem.version = SitemapNotifier::VERSION
|
9
|
+
gem.authors = ["Lasse Bunk"]
|
10
|
+
gem.email = ["lassebunk@gmail.com"]
|
11
|
+
gem.description = %q{Ruby on Rails plugin that automatically notifies Google, Bing, and Yahoo of changes to your models, i.e. changes to your sitemap.}
|
12
|
+
gem.summary = %q{Automatically notify search engines when your models are updated.}
|
13
|
+
gem.homepage = "http://github.com/lassebunk/sitemap_notifier"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.test_files = gem.files.grep(%r{^test/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "activerecord", ">= 3.0.0"
|
21
|
+
gem.add_development_dependency "sqlite3"
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
gem.add_development_dependency "timecop"
|
24
|
+
gem.add_development_dependency "fakeweb"
|
25
|
+
gem.add_development_dependency "mocha"
|
26
|
+
end
|
data/test/db.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "active_record"
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
|
4
|
+
|
5
|
+
ActiveRecord::Migration.create_table :articles do |t|
|
6
|
+
t.string :title
|
7
|
+
end
|
8
|
+
|
9
|
+
ActiveRecord::Migration.create_table :products do |t|
|
10
|
+
t.string :name
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Migration.create_table :users do |t|
|
14
|
+
t.string :name
|
15
|
+
end
|
16
|
+
|
17
|
+
ActiveRecord::Migration.create_table :sites do |t|
|
18
|
+
t.string :name
|
19
|
+
t.boolean :has_sitemap
|
20
|
+
end
|
21
|
+
|
22
|
+
# Standard model to trigger notifications
|
23
|
+
class Article < ActiveRecord::Base
|
24
|
+
end
|
25
|
+
|
26
|
+
# Model with custom sitemap URL
|
27
|
+
class Product < ActiveRecord::Base
|
28
|
+
def sitemap_url
|
29
|
+
"http://mycustomurl.com/sitemapfile.xml"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Model that should not trigger notifications
|
34
|
+
class User < ActiveRecord::Base
|
35
|
+
end
|
36
|
+
|
37
|
+
# Model that has conditional notifications
|
38
|
+
class Site < ActiveRecord::Base
|
39
|
+
def notify_sitemap?
|
40
|
+
has_sitemap?
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class NotifierTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
SitemapNotifier::Notifier.reset
|
6
|
+
SitemapNotifier::Notifier.configure do |config|
|
7
|
+
config.environments = :all
|
8
|
+
config.background = false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_configuration_block
|
13
|
+
SitemapNotifier::Notifier.configure do |config|
|
14
|
+
config.sitemap_url = "http://myconfigureddomain.com/sitemap.xml"
|
15
|
+
end
|
16
|
+
assert_equal "http://myconfigureddomain.com/sitemap.xml", SitemapNotifier::Notifier.sitemap_url
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_doesnt_notify_when_no_models_configured
|
20
|
+
SitemapNotifier::Notifier.configure do |config|
|
21
|
+
end
|
22
|
+
[Article, Product, User, Site].each do |model|
|
23
|
+
[:create, :update, :destroy].each do |action|
|
24
|
+
assert !SitemapNotifier::Notifier.notify_of_changes_to?(model, :create), "Notifies of #{action} on #{model.name}."
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_notifies_when_configured_to_all_models
|
30
|
+
SitemapNotifier::Notifier.configure do |config|
|
31
|
+
config.models = :all
|
32
|
+
end
|
33
|
+
|
34
|
+
[Article, Product, User, Site].each do |model|
|
35
|
+
[:create, :update, :destroy].each do |action|
|
36
|
+
assert SitemapNotifier::Notifier.notify_of_changes_to?(model, action), "Doesn't notify of #{action} on #{model.name}."
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_per_model_action_settings
|
42
|
+
SitemapNotifier::Notifier.configure do |config|
|
43
|
+
config.models = { Article => [:create, :destroy],
|
44
|
+
Product => :update,
|
45
|
+
Site => :all }
|
46
|
+
end
|
47
|
+
|
48
|
+
[[Article, :create, true],
|
49
|
+
[Article, :update, false],
|
50
|
+
[Article, :destroy, true],
|
51
|
+
[Product, :create, false],
|
52
|
+
[Product, :update, true],
|
53
|
+
[Product, :destroy, false],
|
54
|
+
[Site, :create, true],
|
55
|
+
[Site, :update, true],
|
56
|
+
[Site, :destroy, true]].each do |model, action, value|
|
57
|
+
assert_equal value, SitemapNotifier::Notifier.notify_of_changes_to?(model, action), "Expected value to be #{value} for #{action} on #{model}."
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_models_without_action_settings
|
62
|
+
SitemapNotifier::Notifier.configure do |config|
|
63
|
+
config.models = [Article, Product]
|
64
|
+
end
|
65
|
+
|
66
|
+
[[Article, :create, true],
|
67
|
+
[Article, :update, true],
|
68
|
+
[Article, :destroy, true],
|
69
|
+
[Product, :create, true],
|
70
|
+
[Product, :update, true],
|
71
|
+
[Product, :destroy, true],
|
72
|
+
[Site, :create, false],
|
73
|
+
[Site, :update, false],
|
74
|
+
[Site, :destroy, false]].each do |model, action, value|
|
75
|
+
assert_equal value, SitemapNotifier::Notifier.notify_of_changes_to?(model, action), "Expected value to be #{value} for #{action} on #{model}."
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_waits_delay
|
80
|
+
notifier = SitemapNotifier::Notifier
|
81
|
+
notifier.configure do |config|
|
82
|
+
config.sitemap_url = "http://test.dk/sitemap.xml"
|
83
|
+
config.models = [Article]
|
84
|
+
config.delay = 10
|
85
|
+
end
|
86
|
+
|
87
|
+
SitemapNotifier::Notifier.expects(:ping_all).twice
|
88
|
+
|
89
|
+
Article.create!
|
90
|
+
Article.create!
|
91
|
+
Timecop.travel(Time.now + 20) do
|
92
|
+
Article.create!
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_notifies_search_engines
|
97
|
+
sitemap_url = "http://mydomain.dk/sitemap.xml"
|
98
|
+
|
99
|
+
SitemapNotifier::Notifier.configure do |config|
|
100
|
+
config.models = [Article]
|
101
|
+
config.sitemap_url = sitemap_url
|
102
|
+
end
|
103
|
+
|
104
|
+
["http://www.google.com/webmasters/sitemaps/ping?sitemap=#{CGI::escape(sitemap_url)}",
|
105
|
+
"http://www.bing.com/webmaster/ping.aspx?siteMap=#{CGI::escape(sitemap_url)}"].each do |ping_url|
|
106
|
+
Net::HTTP.expects(:get).with(URI.parse(ping_url))
|
107
|
+
end
|
108
|
+
Article.create! :title => "Test"
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_notifies_for_configured_models
|
112
|
+
SitemapNotifier::Notifier.configure do |config|
|
113
|
+
config.models = [Article, Product]
|
114
|
+
config.sitemap_url = "http://test.dk/sitemap.xml"
|
115
|
+
end
|
116
|
+
|
117
|
+
SitemapNotifier::Notifier.expects(:ping_all).twice
|
118
|
+
|
119
|
+
[Article, Product, User].each do |model|
|
120
|
+
model.create!
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_notifies_custom_ping_url
|
125
|
+
sitemap_url = "http://test.dk/sitemap.xml"
|
126
|
+
|
127
|
+
SitemapNotifier::Notifier.configure do |config|
|
128
|
+
config.models = [Article]
|
129
|
+
config.sitemap_url = sitemap_url
|
130
|
+
config.ping_urls << "http://bla.dk/ping.php?test=%{sitemap_url}"
|
131
|
+
end
|
132
|
+
|
133
|
+
["http://www.google.com/webmasters/sitemaps/ping?sitemap=#{CGI::escape(sitemap_url)}",
|
134
|
+
"http://www.bing.com/webmaster/ping.aspx?siteMap=#{CGI::escape(sitemap_url)}",
|
135
|
+
"http://bla.dk/ping.php?test=#{CGI::escape(sitemap_url)}"].each do |ping_url|
|
136
|
+
Net::HTTP.expects(:get).with(URI.parse(ping_url))
|
137
|
+
end
|
138
|
+
|
139
|
+
Article.create!
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_notifies_about_custom_sitemap_url
|
143
|
+
SitemapNotifier::Notifier.configure do |config|
|
144
|
+
config.models = [Product]
|
145
|
+
end
|
146
|
+
|
147
|
+
custom_sitemap_url = "http://mycustomurl.com/sitemapfile.xml"
|
148
|
+
["http://www.google.com/webmasters/sitemaps/ping?sitemap=#{CGI::escape(custom_sitemap_url)}",
|
149
|
+
"http://www.bing.com/webmaster/ping.aspx?siteMap=#{CGI::escape(custom_sitemap_url)}"].each do |ping_url|
|
150
|
+
Net::HTTP.expects(:get).with(URI.parse(ping_url))
|
151
|
+
end
|
152
|
+
|
153
|
+
Product.create!
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_responds_to_conditional_notifications
|
157
|
+
SitemapNotifier::Notifier.configure do |config|
|
158
|
+
config.sitemap_url = "http://test.dk/sitemap.xml"
|
159
|
+
config.models = [Site]
|
160
|
+
config.delay = 0
|
161
|
+
end
|
162
|
+
|
163
|
+
SitemapNotifier::Notifier.expects(:ping_all).twice
|
164
|
+
|
165
|
+
2.times { Site.create! :has_sitemap => true }
|
166
|
+
Site.create! :has_sitemap => false
|
167
|
+
end
|
168
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'test/unit'
|
3
|
+
require 'sitemap_notifier'
|
4
|
+
require 'db'
|
5
|
+
require 'timecop'
|
6
|
+
require 'fakeweb'
|
7
|
+
require 'mocha/setup'
|
8
|
+
|
9
|
+
# Catch requests to Google and Bing
|
10
|
+
[%r{http://www\.google\.com/webmasters/sitemaps/ping\?sitemap=},
|
11
|
+
%r{http://www\.bing\.com/webmaster/ping\.aspx\?siteMap=}].each do |url|
|
12
|
+
FakeWeb.register_uri(:get, url, :body => "OK!")
|
13
|
+
end
|
14
|
+
|
15
|
+
# Catch all other HTTP requests and make them fail so we don't accidentally make real HTTP requests in the test suite
|
16
|
+
FakeWeb.register_uri(:any, //, :body => "Fail! (change this in test_helper.rb)", :status => ["400", "Bad Request"])
|
metadata
CHANGED
@@ -1,69 +1,169 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: sitemap_notifier
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 4
|
10
|
-
version: 0.0.4
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Lasse Bunk
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
12
|
+
date: 2013-02-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: timecop
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: fakeweb
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: mocha
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Ruby on Rails plugin that automatically notifies Google, Bing, and Yahoo
|
111
|
+
of changes to your models, i.e. changes to your sitemap.
|
112
|
+
email:
|
113
|
+
- lassebunk@gmail.com
|
23
114
|
executables: []
|
24
|
-
|
25
115
|
extensions: []
|
26
|
-
|
27
116
|
extra_rdoc_files: []
|
28
|
-
|
29
|
-
|
30
|
-
-
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .travis.yml
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- lib/generators/sitemap_notifier/USAGE
|
125
|
+
- lib/generators/sitemap_notifier/install_generator.rb
|
31
126
|
- lib/generators/sitemap_notifier/templates/initializer.rb
|
127
|
+
- lib/sitemap_notifier.rb
|
32
128
|
- lib/sitemap_notifier/active_record.rb
|
33
129
|
- lib/sitemap_notifier/notifier.rb
|
34
|
-
- lib/sitemap_notifier.rb
|
130
|
+
- lib/sitemap_notifier/version.rb
|
131
|
+
- sitemap_notifier.gemspec
|
132
|
+
- test/db.rb
|
133
|
+
- test/notifier_test.rb
|
134
|
+
- test/test_helper.rb
|
35
135
|
homepage: http://github.com/lassebunk/sitemap_notifier
|
36
|
-
licenses:
|
37
|
-
|
136
|
+
licenses:
|
137
|
+
- MIT
|
38
138
|
post_install_message:
|
39
139
|
rdoc_options: []
|
40
|
-
|
41
|
-
require_paths:
|
140
|
+
require_paths:
|
42
141
|
- lib
|
43
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
143
|
none: false
|
45
|
-
requirements:
|
46
|
-
- -
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
|
49
|
-
segments:
|
144
|
+
requirements:
|
145
|
+
- - ! '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
segments:
|
50
149
|
- 0
|
51
|
-
|
52
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
|
+
hash: 3925366765903606920
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
152
|
none: false
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
|
58
|
-
segments:
|
153
|
+
requirements:
|
154
|
+
- - ! '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
segments:
|
59
158
|
- 0
|
60
|
-
|
159
|
+
hash: 3925366765903606920
|
61
160
|
requirements: []
|
62
|
-
|
63
161
|
rubyforge_project:
|
64
|
-
rubygems_version: 1.8.
|
162
|
+
rubygems_version: 1.8.25
|
65
163
|
signing_key:
|
66
164
|
specification_version: 3
|
67
165
|
summary: Automatically notify search engines when your models are updated.
|
68
|
-
test_files:
|
69
|
-
|
166
|
+
test_files:
|
167
|
+
- test/db.rb
|
168
|
+
- test/notifier_test.rb
|
169
|
+
- test/test_helper.rb
|