Class: SitemapNotifier::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/sitemap_notifier/notifier.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.backgroundObject

Whether to run the notification pings in the background. Default: true

Example:

SitemapNotifier::Notifier.background = false


102
103
104
105
# File 'lib/sitemap_notifier/notifier.rb', line 102

def background
  return @background if defined?(@background)
  @background = true
end

.delayObject

Seconds to wait between notifications of the search engines. Default: 10 minutes

Example:

SitemapNotifier::Notifier.delay = 2.minutes


60
61
62
# File 'lib/sitemap_notifier/notifier.rb', line 60

def delay
  @delay ||= 600
end

.envObject

Current environment. If not set manually, it returns the current Rails environment, like :development: or :production.



76
77
78
# File 'lib/sitemap_notifier/notifier.rb', line 76

def env
  defined?(Rails) ? Rails.env.to_sym : @env
end

.environmentsObject

Environments where sitemap notifications should be triggered.

Example:

SitemapNotifier::Notifier.environments = [:development, :production]


70
71
72
# File 'lib/sitemap_notifier/notifier.rb', line 70

def environments
  @environments ||= [:production]
end

.ping_urlsObject

URLs to be pinged / notified of changes to sitemap. Default is Google and Bing (and therefore Yahoo).

Example:

SitemapNotifier::Notifier.ping_urls << "http://mydomain.com/ping?sitemap=%{sitemap_url}"

The sitemap URL will then be interpolated into the ping URL at runtime.



89
90
91
92
93
# File 'lib/sitemap_notifier/notifier.rb', line 89

def ping_urls
  @ping_urls ||= ["http://www.google.com/webmasters/sitemaps/ping?sitemap=%{sitemap_url}",
                  "http://www.bing.com/webmaster/ping.aspx?siteMap=%{sitemap_url}"]
             # no Yahoo here, as they will be using Bing from september 15th, 2011
end

.sitemap_urlObject

The default sitemap URL to send to search engines.

Example:

SitemapNotifier::Notifier.sitemap_url = "http://test.dk/sitemap.xml"

The default sitemap URL can be overridden on model level:

class Product < ActiveRecord::Base
  belongs_to :site

  def sitemap_url
    "http://#{site.domain}/sitemap.xml"
  end
end


23
24
25
# File 'lib/sitemap_notifier/notifier.rb', line 23

def sitemap_url
  @sitemap_url
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields a configuration block to configure the notifier.

Example:

SitemapNotifier::Notifier.configure do |config|
  config.sitemap_url = "http://test.dk/sitemap.xml"
end

Yields:

  • (_self)

Yield Parameters:



147
148
149
150
151
152
# File 'lib/sitemap_notifier/notifier.rb', line 147

def configure
  yield self
  if models.is_a?(Array) && models.empty? && defined?(Rails)
    Rails.logger.warn "SitemapNotifier was configured without any models to trigger notifications. Search engines will therefore not be notified."
  end
end

.escape_sitemap_url(url) ⇒ Object

URL encodes the given url.



162
163
164
# File 'lib/sitemap_notifier/notifier.rb', line 162

def escape_sitemap_url(url)
  CGI::escape(url)
end

.modelsObject

Set models that should trigger notification.

If you supply a hash, you can specify which actions should trigger notifications using :create, :update, :destroy, or :all.

If you supply an array, all creates, updates, and deletes will trigger notification.

Example:

# Will trigger notifications on creates, updates, and destroys on articles and products:
SitemapNotifier::Notifier.models = [Article, Product]

# Will trigger notifications on the specified actions:
config.models = { Article => [:create, :destroy],
                  Product => :update,
                  Page => :all }


40
41
42
# File 'lib/sitemap_notifier/notifier.rb', line 40

def models
  @models ||= {}
end

.models=(hash_or_array) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/sitemap_notifier/notifier.rb', line 44

def models=(hash_or_array)
  if hash_or_array == :all
    @models = { :all => :all }
  elsif hash_or_array.is_a?(Array)
    @models = Hash[hash_or_array.map { |model| [model, :all] }]
  else
    @models = hash_or_array
  end
end

.notified_at(sitemap_url) ⇒ Object

Returns the latest notification time for the given URL.



188
189
190
# File 'lib/sitemap_notifier/notifier.rb', line 188

def notified_at(sitemap_url)
  notified_urls[sitemap_url]
end

.notified_urlsObject

Holds notified URL times like notified_urls # => 2013-02-05 19:10:29 +0100



183
184
185
# File 'lib/sitemap_notifier/notifier.rb', line 183

def notified_urls
  @notified_urls ||= {}
end

.notify_of_changes_to?(model, action) ⇒ Boolean

Returns true if changes to the given model class should trigger notifications.

Returns:

  • (Boolean)


198
199
200
201
202
203
204
# File 'lib/sitemap_notifier/notifier.rb', line 198

def notify_of_changes_to?(model, action)
  valid_actions = models[model] || models[:all]

  return valid_actions == :all ||
         valid_actions == action ||
         (valid_actions.is_a?(Array) && valid_actions.include?(action))
end

.ping?(sitemap_url) ⇒ Boolean

Returns true if search engines should be modified. Returns false if the configured delay hasn’t been met.

Returns:

  • (Boolean)


177
178
179
180
# File 'lib/sitemap_notifier/notifier.rb', line 177

def ping?(sitemap_url)
  last_notified = notified_at(sitemap_url)
  last_notified.nil? || Time.now >= last_notified + delay
end

.ping_all(url) ⇒ Object

Pings all the configured search engines with the supplied url.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sitemap_notifier/notifier.rb', line 121

def ping_all(url)
  p = Proc.new do
    Rails.logger.info "Notifying search engines of changes to sitemap #{url}..." if defined?(Rails)
    
    escaped_url = escape_sitemap_url(url)

    ping_urls.each do |url|
      url.gsub! "%{sitemap_url}", escaped_url
      if ping_url(url)
        Rails.logger.info "Successfully notified #{url}" if defined?(Rails)
      else
        Rails.logger.info "Failed to notify #{url}" if defined?(Rails)
      end
    end
  end

  background ? Thread.new(&p) : p.call
end

.ping_url(url) ⇒ Object

Makes a GET request to the supplied url. Returns true when successful, false otherwise.



167
168
169
170
171
172
173
174
# File 'lib/sitemap_notifier/notifier.rb', line 167

def ping_url(url)
  begin
    Net::HTTP.get(URI.parse(url))
    return true
  rescue
    return false
  end
end

.resetObject

For testing purposes. Resets all configuration and information about notified sitemaps.



155
156
157
158
159
# File 'lib/sitemap_notifier/notifier.rb', line 155

def reset
  [:@sitemap_url, :@models, :@delay, :@environments, :@ping_urls, :@background, :@notified_urls].each do |var|
    remove_instance_variable var if instance_variable_defined?(var)
  end
end

.run(url = nil) ⇒ Object

Runs the search engine notifications after checking environment and delay.



109
110
111
112
113
114
115
116
117
118
# File 'lib/sitemap_notifier/notifier.rb', line 109

def run(url = nil)
  url ||= sitemap_url

  raise "sitemap_url not set - use SitemapNotifier::Notifier.sitemap_url = 'http://domain.com/sitemap.xml'" unless url

  if (environments == :all || environments.include?(env)) && ping?(url)
    ping_all url
    sitemap_notified url
  end
end

.sitemap_notified(sitemap_url) ⇒ Object

Stores notification time for the given URL.



193
194
195
# File 'lib/sitemap_notifier/notifier.rb', line 193

def sitemap_notified(sitemap_url)
  notified_urls[sitemap_url] = Time.now
end