lib/big_sitemap.rb in big_sitemap-1.0.2 vs lib/big_sitemap.rb in big_sitemap-1.1.0
- old
+ new
@@ -4,27 +4,21 @@
require 'big_sitemap/builder'
class BigSitemap
DEFAULTS = {
:max_per_sitemap => Builder::MAX_URLS,
- :batch_size => 1001, # TODO: Deprecate
:document_path => '/',
:gzip => true,
# Opinionated
:ping_google => true,
:ping_yahoo => false, # needs :yahoo_app_id
:ping_bing => false,
- :ping_ask => false
+ :ping_ask => false,
+ :ping_yandex => false
}
- # TODO: Deprecate
- COUNT_METHODS = [:count_for_sitemap, :count]
- FIND_METHODS = [:find_for_sitemap, :all]
- TIMESTAMP_METHODS = [:updated_at, :updated_on, :updated, :created_at, :created_on, :created]
- PARAM_METHODS = [:to_param, :id]
-
class << self
def generate(options={}, &block)
@sitemap = self.new(options)
@sitemap.first_id_of_last_sitemap = first_id_of_last_sitemap
@@ -156,19 +150,12 @@
# TODO: Deprecate (move to private)
def generate(options={})
clean unless options[:partial_update]
- # TODO: Ddeprecate
- prepare_update
-
add_urls
- # TODO: Deprecate
- generate_models
- generate_static
-
generate_sitemap_index
ping_search_engines
self
@@ -228,141 +215,18 @@
end
if @options[:ping_ask]
Net::HTTP.get('submissions.ask.com', "/ping?sitemap=#{sitemap_uri}")
end
- end
- # TODO: Deprecate
- def get_last_id(filename)
- Dir["#{filename}*.{xml,xml.gz}"].map do |file|
- file.to_s.scan(/#{filename}_(.+).xml/).flatten.last.to_i
- end.sort.last
+ if @options[:ping_yandex]
+ Net::HTTP.get('webmaster.yandex.ru', "/wmconsole/sitemap_list.xml?host=#{sitemap_uri}")
+ end
end
private
- # TODO: Deprecate
- def table_name(model)
- model.table_name
- end
-
- # TODO: Deprecate
- def generate_models
- for model, options in @sources
- with_sitemap(options.dup.merge({:name => model})) do |sitemap|
- last_id = nil #id of last processed item
- count_method = pick_method(model, COUNT_METHODS)
- find_method = pick_method(model, FIND_METHODS)
- raise ArgumentError, "#{model} must provide a count_for_sitemap class method" if count_method.nil?
- raise ArgumentError, "#{model} must provide a find_for_sitemap class method" if find_method.nil?
-
- find_options = {}
- [:conditions, :limit, :joins, :select, :order, :include, :group].each do |key|
- find_options[key] = options.delete(key)
- end
-
- # Keep the intial conditions for later user
- conditions = find_options[:conditions]
-
- primary_method = options.delete(:primary_column)
- primary_column = "#{table_name(model)}.#{primary_method}"
-
- count = model.send(count_method, find_options.merge(:select => (primary_column || '*'), :include => nil))
- count = find_options[:limit].to_i if find_options[:limit] && find_options[:limit].to_i < count
- num_sitemaps = 1
- num_batches = 1
-
- if count > @options[:batch_size]
- num_batches = (count.to_f / @options[:batch_size].to_f).ceil
- num_sitemaps = (count.to_f / @options[:max_per_sitemap].to_f).ceil
- end
- batches_per_sitemap = num_batches.to_f / num_sitemaps.to_f
-
- for sitemap_num in 1..num_sitemaps
- # Work out the start and end batch numbers for this sitemap
- batch_num_start = sitemap_num == 1 ? 1 : ((sitemap_num * batches_per_sitemap).ceil - batches_per_sitemap + 1).to_i
- batch_num_end = (batch_num_start + [batches_per_sitemap, num_batches].min).floor - 1
-
- for batch_num in batch_num_start..batch_num_end
- offset = (batch_num - 1) * @options[:batch_size]
- limit = (count - offset) < @options[:batch_size] ? (count - offset) : @options[:batch_size]
- find_options.update(:limit => limit, :offset => offset) if num_batches > 1
-
- if last_id && primary_column
- find_options.update(:limit => limit, :offset => nil)
- primary_column_value = escape_if_string last_id #escape '
- find_options[:conditions] = [conditions, "(#{primary_column} > #{primary_column_value})"].compact.join(' AND ')
- end
-
- model.send(find_method, find_options).each do |record|
- last_mod = options[:last_modified]
- if last_mod.is_a?(Proc)
- last_mod = last_mod.call(record)
- elsif last_mod.nil?
- last_mod_method = pick_method(record, TIMESTAMP_METHODS)
- last_mod = last_mod_method.nil? ? Time.now : record.send(last_mod_method)
- end
-
- param_method = pick_method(record, PARAM_METHODS)
-
- location =
- if options[:location].is_a?(Proc)
- options[:location].call(record)
- else
- File.join @options[:base_url], options[:path], record.send(param_method).to_s
- end
-
- change_frequency = options[:change_frequency]
- freq = change_frequency.is_a?(Proc) ? change_frequency.call(record) : change_frequency
-
- priority = options[:priority]
- pri = priority.is_a?(Proc) ? priority.call(record) : priority
-
- last_id = primary_column ? record.send(primary_method) : nil
-
- sitemap.add_url!(location, {
- :last_modified => last_mod,
- :change_frequency => freq,
- :priority => pri,
- :part_number => last_id
- }) if location
- end
- end
- end
- end
- end
- self
- end
-
- # TODO: Deprecate
- def generate_static
- return self if Array(@static_pages).empty?
- with_sitemap({:name => 'static', :type => 'static'}) do |sitemap|
- @static_pages.each do |location, last_mod, freq, pri|
- sitemap.add_url!(location, {
- :last_modified => last_mod,
- :change_frequency => freq,
- :priority => pri
- })
- end
- end
- self
- end
-
- # TODO: Deprecate
- def prepare_update
- @files_to_move = []
- @sources.each do |model, options|
- if options[:partial_update] && (primary_column = options[:primary_column]) && (last_id = get_last_id(options[:filename]))
- primary_column_value = escape_if_string last_id #escape '
- options[:conditions] = [options[:conditions], "(#{table_name(model)}.#{primary_column} >= #{primary_column_value})"].compact.join(' AND ')
- options[:start_part_id] = last_id
- end
- end
- end
-
def lock!(lock_file = 'generator.lock')
lock_file = File.join(@options[:document_full], lock_file)
File.open(lock_file, 'w', File::EXCL)
end
@@ -401,14 +265,9 @@
method = candidate
break
end
end
method
- end
-
- # TODO: Deprecate
- def escape_if_string(value)
- (value.to_i.to_s == value.to_s) ? value.to_i : "'#{value.gsub("'", %q(\\\'))}'"
end
def url_for_sitemap(path)
File.join @options[:base_url], @options[:url_path], File.basename(path)
end