lib/big_sitemap/builder.rb in big_sitemap-0.8.3 vs lib/big_sitemap/builder.rb in big_sitemap-1.0.0
- old
+ new
@@ -12,39 +12,40 @@
def initialize(options)
@gzip = options.delete(:gzip)
@max_urls = options.delete(:max_urls) || MAX_URLS
@type = options.delete(:type)
- @paths = []
+ @filepaths = []
@parts = options.delete(:start_part_id) || 0
- @custom_part_nr = options.delete(:partial_update)
+ @partial_update = options.delete(:partial_update)
- @filename = options.delete(:filename)
+ @filename = options.delete(:filename)
@current_filename = nil
@tmp_filename = nil
- @target = _get_writer
+ @target = _get_writer
@level = 0
@opened_tags = []
_init_document
end
- def add_url!(url, time = nil, frequency = nil, priority = nil, part_nr = nil)
- _rotate(part_nr) if @max_urls == @urls
-
+ def add_url!(location, options={})
+ _rotate(options[:id]) if @max_urls == @urls
_open_tag 'url'
- tag! 'loc', url
- tag! 'lastmod', time.utc.strftime('%Y-%m-%dT%H:%M:%S+00:00') if time
- tag! 'changefreq', frequency if frequency
- tag! 'priority', priority if priority
+
+ tag! 'loc', location
+ tag! 'lastmod', options[:last_modified].utc.strftime('%Y-%m-%dT%H:%M:%S+00:00') if options[:last_modified]
+ tag! 'changefreq', options[:change_frequency] || 'weekly'
+ tag! 'priority', options[:priority] if options[:priority]
+
_close_tag 'url'
@urls += 1
end
- def paths!
- @paths
+ def filepaths!
+ @filepaths
end
def close!
_close_document
target!.close if target!.respond_to?(:close)
@@ -58,50 +59,50 @@
private
def _get_writer
filename = @filename.dup
- filename << "_#{@parts}" if @parts > 0
+ filename << "_#{@parts}" if @parts > 0 && @type != 'index'
filename << '.xml'
filename << '.gz' if @gzip
_open_writer(filename)
end
def _open_writer(filename)
@current_filename = filename
@tmp_filename = filename + ".tmp"
- @paths << filename
- file = ::File.open(@tmp_filename, 'w+')
+ @filepaths << filename
+ file = ::File.open(@tmp_filename, 'w+:ASCII-8BIT')
@gzip ? ::Zlib::GzipWriter.new(file) : file
end
- def _init_document( name = 'urlset', attrs = HEADER_ATTRIBUTES)
+ def _init_document(name='urlset', attrs=HEADER_ATTRIBUTES)
@urls = 0
target!.print '<?xml version="1.0" encoding="UTF-8"?>'
_newline
_open_tag name, attrs
end
- def _rotate(part_nr = nil)
+ def _rotate(part_nr=nil)
# write out the current document and start writing into a new file
close!
- @parts = (part_nr && @custom_part_nr) ? part_nr : @parts + 1
+ @parts = part_nr || @parts + 1
@target = _get_writer
_init_document
end
# opens a tag, bumps up level but doesn't require a block
- def _open_tag(name, attrs = {})
+ def _open_tag(name, attrs={})
_indent
_start_tag(name, attrs)
_newline
@level += 1
@opened_tags << name
end
- def _start_tag(name, attrs = {})
- attrs = attrs.map { |attr,value| %Q( #{attr}="#{value}") }.join('')
+ def _start_tag(name, attrs={})
+ attrs = attrs.map { |attr, value| %Q( #{attr}="#{value}") }.join('')
target!.print "<#{name}#{attrs}>"
end
def tag!(name, content, attrs = {})
_indent
@@ -129,29 +130,29 @@
_close_tag(name)
end
end
def _indent
- return if @gzip
target!.print " " * @level
end
def _newline
- return if @gzip
target!.puts ''
end
end
class IndexBuilder < Builder
def _init_document(name = 'sitemapindex', attrs = {'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9'})
attrs.merge('xmlns:geo' => "http://www.google.com/geo/schemas/sitemap/1.0")
super(name, attrs)
end
- def add_url!(url, time = nil)
+ def add_url!(location, options={})
_open_tag 'sitemap'
- tag! 'loc', url
- tag! 'lastmod', time.utc.strftime('%Y-%m-%dT%H:%M:%S+00:00') if time
+
+ tag! 'loc', location
+ tag! 'lastmod', options[:last_modified].utc.strftime('%Y-%m-%dT%H:%M:%S+00:00') if options[:last_modified]
+
_close_tag 'sitemap'
end
end
class GeoBuilder < Builder