Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ test/tmp
test/version_tmp
tmp
tmtags
.ruby-version
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 35 additions & 13 deletions lib/xml-sitemap/index.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -45,7 +51,7 @@ def render
end
}.to_s
end

# Render XML sitemap index into the file
#
# path - Output filename
Expand All @@ -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
11 changes: 11 additions & 0 deletions spec/fixtures/sample_index_with_gz.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://foobar.com/sitemap-0.xml.gz</loc>
<lastmod>2011-06-01T00:00:01Z</lastmod>
</sitemap>
<sitemap>
<loc>http://foobar.com/sitemap-1.xml.gz</loc>
<lastmod>2011-06-01T00:00:01Z</lastmod>
</sitemap>
</sitemapindex>
30 changes: 28 additions & 2 deletions spec/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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