xml-sitemap 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,38 +1,63 @@
1
- XmlSitemap
2
- ==========
1
+ # XmlSitemap
3
2
 
4
3
  XmlSitemap is a Ruby library that provides an easy way to generate XML sitemaps and indexes.
5
4
 
6
- It does not have any web-framework dependencies thus might be used almost anywhere.
5
+ It does not have any web-framework dependencies and could be used in any ruby-based application.
7
6
 
8
- ## Installing XmlSitemap
7
+ ## Installation
8
+
9
+ Install via rubygems:
9
10
 
10
11
  $ gem install xml-sitemap
11
12
 
12
- ## Sample usage
13
+ ## Configuration
14
+
15
+ Add gem to your Gemfile and you're ready to go:
13
16
 
14
- In your Gemfile:
17
+ ```ruby
18
+ gem 'xml-sitemap'
19
+ ```
15
20
 
16
- gem 'xml-sitemap'
21
+ ## Usage
17
22
 
18
- Simple map usage:
19
-
20
- map = XmlSitemap::Map.new('domain.com') do |m|
21
- # Adds a simple page
22
- m.add '/page1'
23
-
24
- # You can drop leading slash, it will be automatically added
25
- m.add 'page2'
26
-
27
- # Set the page priority
28
- m.add 'page3', :priority => 0.2
29
-
30
- # Specify last modification date and update frequiency
31
- m.add 'page4', :updated => Date.today, :period => :never
32
- end
33
-
34
- map.render # => render to XML
35
- map.render_to('/path/to/file.xml') # => render into a file
23
+ Simple usage:
24
+
25
+ ```ruby
26
+ map = XmlSitemap::Map.new('domain.com') do |m|
27
+ # Adds a simple page
28
+ m.add '/page1'
29
+
30
+ # You can drop leading slash, it will be automatically added
31
+ m.add 'page2'
32
+
33
+ # Set the page priority (default is 0.5)
34
+ m.add 'page3', :priority => 0.2
35
+
36
+ # Specify last modification date and update frequiency
37
+ m.add 'page4', :updated => Date.today, :period => :never
38
+ end
39
+
40
+ # Render the sitemap XML
41
+ map.render
42
+
43
+ # Render and save XML to the output file
44
+ map.render_to('/path/to/file.xml')
45
+
46
+ # You can also use compression
47
+ map.render_to('/path/to/file.xml.gz', :gzip => true)
48
+
49
+ # If you didnt specify .gz extension to the filename,
50
+ # XmlSitemap will automatically append it
51
+ # => content will be saved to /path/to/file.xml.gz
52
+ map.render_to('/path/to/file.xml', :gzip => true)
53
+ ```
54
+
55
+ You can also create a map via shortcut:
56
+
57
+ ```ruby
58
+ map = XmlSitemap.new('foobar.com')
59
+ map = XmlSitemap.map('foobar.com')
60
+ ```
36
61
 
37
62
  By default XmlSitemap creates a map with link to homepage of your domain. It's a priority 1.0. Default priority is 0.5.
38
63
 
@@ -51,38 +76,47 @@ List of periods:
51
76
 
52
77
  When creating a new map object, you can specify a set of options.
53
78
 
54
- map = XmlSitemap::Map.new('mydomain.com', options)
79
+ ```ruby
80
+ map = XmlSitemap::Map.new('mydomain.com', options)
81
+ ```
55
82
 
56
83
  Available options:
57
84
 
58
- - :secure - Will add all sitemap items with https prefix. (default: false)
59
- - :home - Disable homepage autocreation, but you still can do that manually. (default: true)
60
- - :root - Force all links to fall under the main domain. You can add full urls (not paths) if set to false. (default: true)
85
+ - :secure - Will add all sitemap items with https prefix. *(default: false)*
86
+ - :home - Disable homepage autocreation, but you still can do that manually. *(default: true)*
87
+ - :root - Force all links to fall under the main domain. You can add full urls (not paths) if set to false. *(default: true)*
61
88
  - :time - Provide a creation time for the sitemap. (default: current time)
89
+ - :group - Group name for sitemap index. *(default: sitemap)*
62
90
 
63
91
  ## XmlSitemap::Index
64
92
 
65
- Regular sitemap does not support more than 50k records, so if you generation a huge sitemap you need to use XmlSitemap::Index.
93
+ Regular sitemap does not support more than 50k records, so if you're generating a huge sitemap you need to use XmlSitemap::Index.
94
+
66
95
  Index is just a collection of links to all the website's sitemaps.
67
96
 
68
97
  Usage:
69
98
 
70
- map = XmlSitemap::Map.new('domain.com')
71
- map.add 'page'
99
+ ```ruby
100
+ map = XmlSitemap::Map.new('domain.com')
101
+ map.add 'page'
72
102
 
73
- index = XmlSitemap::Index.new
74
- index.add map
75
-
76
- index.render # => render index to XML
77
- index.render_to('/path/to/file.xml') # => render into a file
78
-
79
- ## TODO
103
+ index = XmlSitemap::Index.new
104
+ # or via shortcut
105
+ index = XmlSitemap.index
106
+
107
+ # Add a map to the index
108
+ index.add(map)
109
+
110
+ # Render as XML
111
+ index.render
80
112
 
81
- - Gzip sitemaps and index file
113
+ # Render XML to the output file
114
+ index.render_to('/path/to/file.xml')
115
+ ```
82
116
 
83
117
  ## License
84
118
 
85
- Copyright © 2010 Dan Sosedoff.
119
+ Copyright © 2010-2011 Dan Sosedoff.
86
120
 
87
121
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
88
122
 
@@ -3,8 +3,8 @@ module XmlSitemap
3
3
  attr_reader :maps
4
4
 
5
5
  def initialize(opts={})
6
- @maps = []
7
- @offset = 0
6
+ @maps = []
7
+ @offsets = Hash.new(0)
8
8
 
9
9
  yield self if block_given?
10
10
  end
@@ -15,10 +15,10 @@ module XmlSitemap
15
15
  raise ArgumentError, 'Map is empty!' if map.empty?
16
16
 
17
17
  @maps << {
18
- :loc => map.index_url(@offset),
18
+ :loc => map.index_url(@offsets[map.group]),
19
19
  :lastmod => map.created_at.utc.iso8601
20
20
  }
21
- @offset += 1
21
+ @offsets[map.group] += 1
22
22
  end
23
23
 
24
24
  # Generate sitemap XML index
@@ -1,11 +1,13 @@
1
1
  module XmlSitemap
2
2
  class Item
3
+ DEFAULT_PRIORITY = 0.5
4
+
3
5
  attr_reader :target, :updated, :priority, :changefreq
4
6
 
5
7
  def initialize(target, opts={})
6
8
  @target = target.to_s.strip
7
9
  @updated = opts[:updated] || Time.now
8
- @priority = opts[:priority] || 0.5
10
+ @priority = opts[:priority] || DEFAULT_PRIORITY
9
11
  @changefreq = opts[:period] || :weekly
10
12
 
11
13
  # allow only date or time object
@@ -23,6 +25,7 @@ module XmlSitemap
23
25
  attr_reader :buffer
24
26
  attr_reader :created_at
25
27
  attr_reader :root
28
+ attr_reader :group
26
29
 
27
30
  # Creates new Map class for specified domain
28
31
  def initialize(domain, opts={})
@@ -33,6 +36,7 @@ module XmlSitemap
33
36
  @secure = opts[:secure] || false
34
37
  @home = opts.key?(:home) ? opts[:home] : true
35
38
  @root = opts.key?(:root) ? opts[:root] : true
39
+ @group = opts[:group] || "sitemap"
36
40
  @items = []
37
41
 
38
42
  self.add('/', :priority => 1.0) if @home === true
@@ -52,8 +56,14 @@ module XmlSitemap
52
56
  raise ArgumentError, 'Target required!' if target.nil?
53
57
  raise ArgumentError, 'Target is empty!' if target.to_s.strip.empty?
54
58
 
59
+ url = process_target(target)
60
+
61
+ if url.length > 2048
62
+ raise ArgumentError, "Target can't be longer than 2,048 characters!"
63
+ end
64
+
55
65
  opts[:updated] = @created_at unless opts.key?(:updated)
56
- item = XmlSitemap::Item.new(process_target(target), opts)
66
+ item = XmlSitemap::Item.new(url, opts)
57
67
  @items << item
58
68
  item
59
69
  end
@@ -75,7 +85,7 @@ module XmlSitemap
75
85
 
76
86
  # Get full url for index
77
87
  def index_url(offset)
78
- "http://#{@domain}/sitemap-#{offset}.xml"
88
+ "http://#{@domain}/#{@group}-#{offset}.xml"
79
89
  end
80
90
 
81
91
  # Render XML
@@ -95,15 +105,26 @@ module XmlSitemap
95
105
  end
96
106
 
97
107
  # Render XML sitemap into the file
98
- def render_to(path, opts={})
99
- overwrite = opts[:overwrite] || true
108
+ def render_to(path, options={})
109
+ overwrite = options[:overwrite] == true || true
110
+ compress = options[:gzip] == true || false
111
+
100
112
  path = File.expand_path(path)
113
+ path << ".gz" unless path =~ /\.gz\z/i if compress
101
114
 
102
115
  if File.exists?(path) && !overwrite
103
116
  raise RuntimeError, "File already exists and not overwritable!"
104
117
  end
105
118
 
106
- File.open(path, 'w') { |f| f.write(self.render) }
119
+ File.open(path, 'w') do |f|
120
+ unless compress
121
+ f.write(self.render)
122
+ else
123
+ gz = Zlib::GzipWriter.new(f)
124
+ gz.write(self.render)
125
+ gz.close
126
+ end
127
+ end
107
128
  end
108
129
 
109
130
  protected
@@ -1,3 +1,3 @@
1
1
  module XmlSitemap
2
- VERSION = '1.1.2'
2
+ VERSION = '1.1.3'
3
3
  end
data/lib/xml-sitemap.rb CHANGED
@@ -1,7 +1,31 @@
1
1
  require 'time'
2
2
  require 'date'
3
+ require 'zlib'
3
4
  require 'builder'
4
5
 
5
6
  require 'xml-sitemap/options'
6
7
  require 'xml-sitemap/map'
7
- require 'xml-sitemap/index'
8
+ require 'xml-sitemap/index'
9
+
10
+ module XmlSitemap
11
+ class << self
12
+ # Shortcut to XmlSitemap::Map.new
13
+ #
14
+ # domain - Primary domain
15
+ # options - Map options
16
+ #
17
+ def map(domain, options={})
18
+ XmlSitemap::Map.new(domain, options)
19
+ end
20
+
21
+ alias :new :map
22
+
23
+ # Shortcut to XmlSitemap::Index.new
24
+ #
25
+ # options - Index options
26
+ #
27
+ def index(options={})
28
+ XmlSitemap::Index.new(options)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
3
+ <sitemap>
4
+ <loc>http://foobar.com/first-0.xml</loc>
5
+ <lastmod>2011-06-01T00:00:01Z</lastmod>
6
+ </sitemap>
7
+ <sitemap>
8
+ <loc>http://foobar.com/second-0.xml</loc>
9
+ <lastmod>2011-06-01T00:00:01Z</lastmod>
10
+ </sitemap>
11
+ <sitemap>
12
+ <loc>http://foobar.com/second-1.xml</loc>
13
+ <lastmod>2011-06-01T00:00:01Z</lastmod>
14
+ </sitemap>
15
+ <sitemap>
16
+ <loc>http://foobar.com/third-0.xml</loc>
17
+ <lastmod>2011-06-01T00:00:01Z</lastmod>
18
+ </sitemap>
19
+ </urlset>
data/spec/index_spec.rb CHANGED
@@ -47,4 +47,24 @@ describe XmlSitemap::Index do
47
47
  File.read(path).should == fixture('sample_index.xml')
48
48
  File.delete(path) if File.exists?(path)
49
49
  end
50
+
51
+ it 'should have separate running offsets for different map groups' do
52
+ m1 = XmlSitemap::Map.new('foobar.com', :time => @base_time, :group => "first") { |m| m.add('about') }
53
+ m2 = XmlSitemap::Map.new('foobar.com', :time => @base_time, :group => "second") { |m| m.add('about') }
54
+ m3 = XmlSitemap::Map.new('foobar.com', :time => @base_time, :group => "second") { |m| m.add('about') }
55
+ m4 = XmlSitemap::Map.new('foobar.com', :time => @base_time, :group => "third") { |m| m.add('about') }
56
+
57
+ index = XmlSitemap::Index.new do |i|
58
+ i.add(m1)
59
+ i.add(m2)
60
+ i.add(m3)
61
+ i.add(m4)
62
+ end
63
+
64
+ path = "/tmp/index_#{Time.now.to_i}.xml"
65
+ index.render_to(path)
66
+ File.read(path).should == fixture('group_index.xml')
67
+ File.delete(path) if File.exists?(path)
68
+ end
69
+
50
70
  end
data/spec/map_spec.rb CHANGED
@@ -71,6 +71,15 @@ describe XmlSitemap::Map do
71
71
  }.should raise_error RuntimeError, 'Only less than 50k records allowed!'
72
72
  end
73
73
 
74
+ it 'should not allow urls longer than 2048 characters' do
75
+ long_string = (1..2049).to_a.map { |i| "a" }.join
76
+
77
+ map = XmlSitemap::Map.new('foobar.com')
78
+ proc {
79
+ map.add(long_string)
80
+ }.should raise_error ArgumentError, "Target can't be longer than 2,048 characters!"
81
+ end
82
+
74
83
  it 'should save contents to the filesystem' do
75
84
  map = XmlSitemap::Map.new('foobar.com', :time => @base_time) do |m|
76
85
  m.add('about')
@@ -82,4 +91,33 @@ describe XmlSitemap::Map do
82
91
  File.read(path).should == fixture('saved_map.xml')
83
92
  File.delete(path) if File.exists?(path)
84
93
  end
94
+
95
+ it 'should save gzip contents to the filesystem' do
96
+ map = XmlSitemap::Map.new('foobar.com', :time => @base_time)
97
+
98
+ path = "/tmp/sitemap.xml"
99
+ path_gzip = path + ".gz"
100
+
101
+ map.render_to(path)
102
+ map.render_to(path_gzip, :gzip => true)
103
+
104
+ checksum(File.read(path)).should == checksum(gunzip(path_gzip))
105
+
106
+ File.delete(path) if File.exists?(path)
107
+ File.delete(path_gzip) if File.exists?(path_gzip)
108
+ end
109
+
110
+ it 'should add .gz extension if comression is enabled' do
111
+ map = XmlSitemap::Map.new('foobar.com', :time => @base_time)
112
+ path = '/tmp/sitemap.xml'
113
+ path_gzip = path + ".gz"
114
+
115
+ map.render_to(path)
116
+ map.render_to(path, :gzip => true)
117
+
118
+ checksum(File.read(path)).should == checksum(gunzip(path_gzip))
119
+
120
+ File.delete(path) if File.exists?(path)
121
+ File.delete(path_gzip) if File.exists?(path_gzip)
122
+ end
85
123
  end
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@ SimpleCov.start do
5
5
  add_group 'XmlSitemap', 'lib/xml-sitemap'
6
6
  end
7
7
 
8
+ require 'digest'
8
9
  require 'xml-sitemap'
9
10
 
10
11
  def fixture_path
@@ -14,3 +15,17 @@ end
14
15
  def fixture(file)
15
16
  File.read(File.join(fixture_path, file))
16
17
  end
18
+
19
+ def checksum(content)
20
+ Digest::SHA1.hexdigest(content)
21
+ end
22
+
23
+ def gunzip(path)
24
+ contents = nil
25
+ File.open(path) do |f|
26
+ gz = Zlib::GzipReader.new(f)
27
+ contents = gz.read
28
+ gz.close
29
+ end
30
+ contents
31
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'XmlSitemap' do
4
+ it 'creates a Map via shortcut' do
5
+ XmlSitemap.new('foo.com').should be_a XmlSitemap::Map
6
+ XmlSitemap.map('foo.com').should be_a XmlSitemap::Map
7
+ end
8
+
9
+ it 'creates an Index via shortcut' do
10
+ XmlSitemap.index.should be_a XmlSitemap::Index
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml-sitemap
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-25 00:00:00.000000000Z
12
+ date: 2011-09-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &2165753020 !ruby/object:Gem::Requirement
16
+ requirement: &2154904800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0.8'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2165753020
24
+ version_requirements: *2154904800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2165752460 !ruby/object:Gem::Requirement
27
+ requirement: &2154904300 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.6'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2165752460
35
+ version_requirements: *2154904300
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: simplecov
38
- requirement: &2165751940 !ruby/object:Gem::Requirement
38
+ requirement: &2154903840 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0.4'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2165751940
46
+ version_requirements: *2154903840
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: builder
49
- requirement: &2165751460 !ruby/object:Gem::Requirement
49
+ requirement: &2154903380 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '2.0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2165751460
57
+ version_requirements: *2154903380
58
58
  description: Provides a wrapper to generate XML sitemaps and sitemap indexes.
59
59
  email:
60
60
  - dan.sosedoff@gmail.com
@@ -75,12 +75,14 @@ files:
75
75
  - lib/xml-sitemap/version.rb
76
76
  - spec/fixtures/empty_index.xml
77
77
  - spec/fixtures/encoded_map.xml
78
+ - spec/fixtures/group_index.xml
78
79
  - spec/fixtures/sample_index.xml
79
80
  - spec/fixtures/saved_map.xml
80
81
  - spec/fixtures/simple_map.xml
81
82
  - spec/index_spec.rb
82
83
  - spec/map_spec.rb
83
84
  - spec/spec_helper.rb
85
+ - spec/xmlsitemap_spec.rb
84
86
  - xml-sitemap.gemspec
85
87
  homepage: http://github.com/sosedoff/xml-sitemap
86
88
  licenses: []
@@ -102,16 +104,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
104
  version: '0'
103
105
  requirements: []
104
106
  rubyforge_project:
105
- rubygems_version: 1.8.6
107
+ rubygems_version: 1.8.10
106
108
  signing_key:
107
109
  specification_version: 3
108
110
  summary: Simple XML sitemap generator for Ruby/Rails applications.
109
111
  test_files:
110
112
  - spec/fixtures/empty_index.xml
111
113
  - spec/fixtures/encoded_map.xml
114
+ - spec/fixtures/group_index.xml
112
115
  - spec/fixtures/sample_index.xml
113
116
  - spec/fixtures/saved_map.xml
114
117
  - spec/fixtures/simple_map.xml
115
118
  - spec/index_spec.rb
116
119
  - spec/map_spec.rb
117
120
  - spec/spec_helper.rb
121
+ - spec/xmlsitemap_spec.rb