Class: Cms::SitemapSubmitter

Inherits:
Object
  • Object
show all
Includes:
ActionController::Caching::Pages, 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:



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

def initialize(search_engine) #:nodoc:
  @search_engine = search_engine
  @connection = ActiveResource::Connection.new(search_engine.url)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#search_engineObject

Returns the value of attribute search_engine.



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

def search_engine
  @search_engine
end

Class Method Details

.loggerObject

:nodoc:



69
70
71
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 69

def logger #:nodoc:
  RAILS_DEFAULT_LOGGER
end

.modelsObject

the models defined by the application that will have sitemap information



65
66
67
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 65

def models
  @models
end

.perform_cachingObject

:nodoc:



73
74
75
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 73

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' }


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 46

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)
  
  # 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_page :controller => 'sitemaps', :action => 'index', :format => 'xml'

    @models.each do |model|
      expire_page :controller => 'sitemaps', :model => 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

.submit(search_engine) ⇒ Object

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



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

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

Instance Method Details

#document_urlObject

:nodoc:



99
100
101
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 99

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

#loggerObject

:nodoc:



107
108
109
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 107

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

#parametersObject

:nodoc:



103
104
105
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 103

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

#submitObject

:nodoc:



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bcms_sitemap/sitemap_submitter.rb', line 87

def submit #:nodoc:
  response = 200
  begin
    @connection.get(document_url)
    logger.info "Sitemap was successfully submitted to #{search_engine.name} (#{document_url})"
  rescue => e
    logger.error "Sitemap submition failed for #{search_engine.name} (#{document_url})\nResponse was #{e.response}"
    response = e.response
  end
  response
end