diff --git a/.gitignore b/.gitignore index 37ef81a..ccfff1d 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ test/tmp test/version_tmp tmp tmtags +.ruby-version diff --git a/README.md b/README.md index c1746bb..9be47e5 100644 --- a/README.md +++ b/README.md @@ -129,12 +129,18 @@ index = XmlSitemap::Index.new # or if you want the URLs to use HTTPS index = XmlSitemap::Index.new(:secure => true) +# or gzip compression for all sitemaps +index = XmlSitemap::Index.new(:gzip => true) + # or via shortcut index = XmlSitemap.index # Add a map to the index index.add(map) +# With a gzip option for current map +index.add(map, :gzip => true) + # Render as XML index.render diff --git a/lib/xml-sitemap/index.rb b/lib/xml-sitemap/index.rb index 7c486a3..e9858cd 100644 --- a/lib/xml-sitemap/index.rb +++ b/lib/xml-sitemap/index.rb @@ -1,36 +1,42 @@ module XmlSitemap class Index attr_reader :maps - + # Initialize a new Index instance # # opts - Index options # # opts[:secure] - Force HTTPS for all items. (default: false) + # opts[:gzip] - Force .gzip extension for all items. (default: false) # def initialize(opts={}) - @maps = [] - @offsets = Hash.new(0) - @secure = opts[:secure] || false - + @maps = [] + @offsets = Hash.new(0) + @secure = opts[:secure] || false + @global_gzip = opts[:gzip] || false + yield self if block_given? end - + # Add map object to index # # map - XmlSitemap::Map instance # - def add(map, use_offsets=true) + #def add(map, use_offsets=true) + def add(map, opts={}) raise ArgumentError, 'XmlSitemap::Map object required!' unless map.kind_of?(XmlSitemap::Map) raise ArgumentError, 'Map is empty!' if map.empty? - + + @local_gzip = opts[:gzip] || false + @use_offsets = !opts[:use_offsets].nil? ? opts[:use_offsets] : true + @maps << { - :loc => use_offsets ? map.index_url(@offsets[map.group], @secure) : map.plain_index_url(@secure), + :loc => set_map_loc(map), :lastmod => map.created_at.utc.iso8601 } @offsets[map.group] += 1 end - + # Generate sitemap XML index # def render @@ -45,7 +51,7 @@ def render end }.to_s end - + # Render XML sitemap index into the file # # path - Output filename @@ -56,12 +62,28 @@ def render def render_to(path, options={}) overwrite = options[:overwrite] || true path = File.expand_path(path) - + if File.exists?(path) && !overwrite raise RuntimeError, "File already exists and not overwritable!" end - + File.open(path, 'w') { |f| f.write(self.render) } end + + private + + # Set location for selected map + # + # map - XmlSitemap::Map instance + def set_map_loc(map) + loc = if @use_offsets + map.index_url(@offsets[map.group], @secure) + else + map.plain_index_url(@secure) + end + + loc += '.gz' if @global_gzip || @local_gzip + loc + end end end diff --git a/spec/fixtures/sample_index_with_gz.xml b/spec/fixtures/sample_index_with_gz.xml new file mode 100644 index 0000000..c6a0d5e --- /dev/null +++ b/spec/fixtures/sample_index_with_gz.xml @@ -0,0 +1,11 @@ + + + + http://foobar.com/sitemap-0.xml.gz + 2011-06-01T00:00:01Z + + + http://foobar.com/sitemap-1.xml.gz + 2011-06-01T00:00:01Z + + diff --git a/spec/index_spec.rb b/spec/index_spec.rb index 90b173c..00b7eec 100644 --- a/spec/index_spec.rb +++ b/spec/index_spec.rb @@ -51,8 +51,8 @@ m2 = XmlSitemap::Map.new('two.foobar.com', :time => base_time) { |m| m.add('about') } index = XmlSitemap::Index.new do |i| - i.add(m1, false) - i.add(m2, false) + i.add(m1, :use_offsets => false) + i.add(m2, :use_offsets => false) end index.render.split("\n")[2..-1].join("\n").should == fixture('sample_many_subdomains_index.xml').split("\n")[2..-1].join("\n") @@ -91,5 +91,31 @@ index.render_to(index_path) File.read(index_path).split("\n")[2..-1].join("\n").should eq(fixture('group_index.xml').split("\n")[2..-1].join("\n")) end + + it 'saves index contents to the filesystem with local gzip options for each map' do + m1 = XmlSitemap::Map.new('foobar.com', :time => base_time) { |m| m.add('about') } + m2 = XmlSitemap::Map.new('foobar.com', :time => base_time) { |m| m.add('about') } + + index = XmlSitemap::Index.new do |i| + i.add(m1, :gzip => true) + i.add(m2, :gzip => true) + end + + index.render_to(index_path) + File.read(index_path).split("\n")[2..-1].join("\n").should eq(fixture('sample_index_with_gz.xml').split("\n")[2..-1].join("\n")) + end + + it 'saves index contents to the filesystem with global gzip options for all maps' do + m1 = XmlSitemap::Map.new('foobar.com', :time => base_time) { |m| m.add('about') } + m2 = XmlSitemap::Map.new('foobar.com', :time => base_time) { |m| m.add('about') } + + index = XmlSitemap::Index.new(:gzip => true) do |i| + i.add(m1) + i.add(m2) + end + + index.render_to(index_path) + File.read(index_path).split("\n")[2..-1].join("\n").should eq(fixture('sample_index_with_gz.xml').split("\n")[2..-1].join("\n")) + end end end