Class: Cms::SitemapSubmitter

Inherits:
Object
  • Object
show all
Includes:
ActionController::UrlWriter
Defined in:
lib/bcms_sitemap/sitemap_submitter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_engine) ⇒ SitemapSubmitter

:nodoc:



95
96
97
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 95

def initialize(search_engine) #:nodoc:
  @search_engine = search_engine
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



92
93
94
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 92

def connection
  @connection
end

#search_engineObject

Returns the value of attribute search_engine.



92
93
94
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 92

def search_engine
  @search_engine
end

Class Method Details

.expire_sitemap(options = {}) ⇒ Object



36
37
38
39
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 36

def expire_sitemap(options = {})
  return unless perform_caching
  ApplicationController::expire_page sitemap_path(options)
end

.loggerObject

:nodoc:



82
83
84
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 82

def logger #:nodoc:
  RAILS_DEFAULT_LOGGER
end

.modelsObject

the models defined by the application that will have sitemap information



78
79
80
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 78

def models
  @models
end

.perform_cachingObject

:nodoc:



86
87
88
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 86

def perform_caching #:nodoc:
  ApplicationController.perform_caching
end

.publish_models=(models) ⇒ Object

State what models to publish as a hash. The keys are the plural names of the models. The values should be the scope to be used, formed as string

Cms::SitemapSubmitter.publish_models = {:pages => 'published.not_hidden', :news_articles => 'released' }


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 59

def publish_models=(models)
  models.each_pair do |model, scope|
    logger.debug "Backporting #{model} with bcms_sitemap accessors for selecting data - scope defined: '#{scope}'"
    src = <<-end_src
      class << self
        def bcms_sitemap_scope
          #{scope}.all
        end
        def bcms_sitemap_last_update
          #{scope}.maximum(:updated_at)
        end
      end
    end_src
    model.to_s.classify.constantize.class_eval src, __FILE__, __LINE__
  end
  @models = models.keys.collect { |k| k.to_s  }.sort
end

.runObject

Checks to see if there has been any updates since last time. If so, clears the cache for the relevant model and submits the url to the search engine’s that have not yet been notified



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 10

def run
  submit_time = SearchEngine.enabled.minimum(:submitted_at) || Time.now.years_ago(10)
  
  # collect timestamp for all models. We don't want to expire more pages than necessary
  timestamps = {}
  @models.each do |model|
    last_update = model.classify.constantize.bcms_sitemap_last_update
    timestamps[model] = last_update if last_update
  end
  last_update = timestamps.values.compact.max
  # try this {}.values.compact.max
  if last_update && (submit_time.nil? || (submit_time < last_update))
    # This is a lazy cleaning of cache
    expire_sitemap :controller => 'sitemaps', :action => 'index', :format => 'xml'

    @models.each do |model|
      expire_sitemap :controller => 'sitemaps', :action => model, :format => 'xml' if !timestamps[model] || submit_time < timestamps[model]
    end
    SearchEngine.enabled.all.each do |search_engine|
      if search_engine..nil? || search_engine. < last_update
        search_engine.submit
      end
    end
  end
end

.sitemap_path(options = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 41

def sitemap_path(options = {})
  if options[:action] == 'index'
    "/#{options[:controller]}.#{options[:format]}"
  else
    "/#{options[:controller]}/#{options[:action]}.#{options[:format]}"
  end
end

.submit(search_engine) ⇒ Object

Submit a single search engine. Called from run through the search engine



50
51
52
53
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 50

def submit(search_engine)
  sumbmitter = new(search_engine)
  sumbmitter.submit
end

Instance Method Details

#document_urlObject

:nodoc:



118
119
120
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 118

def document_url #:nodoc:
  @document_url ||= "#{search_engine.url}#{parameters}"
end

#get(document_url) ⇒ Object



112
113
114
115
116
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 112

def get(document_url)
  url = URI.parse(document_url)
  http = Net::HTTP.new(url.host, url.port)
  resp = http.send_request('GET', url.request_uri)
end

#loggerObject

:nodoc:



126
127
128
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 126

def logger #:nodoc:
  self.class.logger
end

#parametersObject

:nodoc:



122
123
124
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 122

def parameters #:nodoc:
  CGI.escape "#{sitemaps_url(:host => SITE_DOMAIN)}"
end

#submitObject

:nodoc:



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 99

def submit #:nodoc:
  #puts "Submitting #{document_url}"
  resp = get document_url
  #puts "Response was #{resp.code} #{resp.message}"
  #puts "Body #{resp.body}"
  if resp.is_a? Net::HTTPOK
    logger.info "Sitemap was successfully submitted to #{search_engine.name} (#{document_url})"
  else
    logger.error "Sitemap submition failed for #{search_engine.name} (#{document_url})\nResponse was #{resp.code} #{resp.message}"
  end
  resp.code
end