sitemap_generator_ftbpro 5.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +13 -0
  3. data/Gemfile.lock +35 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +1139 -0
  6. data/Rakefile +43 -0
  7. data/VERSION +1 -0
  8. data/lib/capistrano/sitemap_generator.rb +1 -0
  9. data/lib/capistrano/tasks/sitemap_generator.cap +36 -0
  10. data/lib/sitemap_generator/adapters/file_adapter.rb +43 -0
  11. data/lib/sitemap_generator/adapters/fog_adapter.rb +28 -0
  12. data/lib/sitemap_generator/adapters/s3_adapter.rb +41 -0
  13. data/lib/sitemap_generator/adapters/wave_adapter.rb +21 -0
  14. data/lib/sitemap_generator/adapters.rb +0 -0
  15. data/lib/sitemap_generator/application.rb +49 -0
  16. data/lib/sitemap_generator/builder/sitemap_file.rb +171 -0
  17. data/lib/sitemap_generator/builder/sitemap_index_file.rb +149 -0
  18. data/lib/sitemap_generator/builder/sitemap_index_url.rb +28 -0
  19. data/lib/sitemap_generator/builder/sitemap_url.rb +250 -0
  20. data/lib/sitemap_generator/builder.rb +8 -0
  21. data/lib/sitemap_generator/core_ext/big_decimal.rb +45 -0
  22. data/lib/sitemap_generator/core_ext/numeric.rb +48 -0
  23. data/lib/sitemap_generator/core_ext.rb +3 -0
  24. data/lib/sitemap_generator/helpers/number_helper.rb +237 -0
  25. data/lib/sitemap_generator/interpreter.rb +80 -0
  26. data/lib/sitemap_generator/link_set.rb +665 -0
  27. data/lib/sitemap_generator/railtie.rb +7 -0
  28. data/lib/sitemap_generator/sitemap_location.rb +192 -0
  29. data/lib/sitemap_generator/sitemap_namer.rb +75 -0
  30. data/lib/sitemap_generator/tasks.rb +53 -0
  31. data/lib/sitemap_generator/templates.rb +41 -0
  32. data/lib/sitemap_generator/utilities.rb +181 -0
  33. data/lib/sitemap_generator.rb +82 -0
  34. data/lib/tasks/sitemap_generator_tasks.rake +1 -0
  35. data/rails/install.rb +2 -0
  36. data/rails/uninstall.rb +2 -0
  37. data/spec/blueprint.rb +15 -0
  38. data/spec/files/sitemap.create.rb +12 -0
  39. data/spec/files/sitemap.groups.rb +49 -0
  40. data/spec/sitemap_generator/adapters/s3_adapter_spec.rb +23 -0
  41. data/spec/sitemap_generator/alternate_sitemap_spec.rb +79 -0
  42. data/spec/sitemap_generator/application_spec.rb +69 -0
  43. data/spec/sitemap_generator/builder/sitemap_file_spec.rb +110 -0
  44. data/spec/sitemap_generator/builder/sitemap_index_file_spec.rb +124 -0
  45. data/spec/sitemap_generator/builder/sitemap_index_url_spec.rb +28 -0
  46. data/spec/sitemap_generator/builder/sitemap_url_spec.rb +186 -0
  47. data/spec/sitemap_generator/core_ext/bigdecimal_spec.rb +20 -0
  48. data/spec/sitemap_generator/core_ext/numeric_spec.rb +43 -0
  49. data/spec/sitemap_generator/file_adaptor_spec.rb +20 -0
  50. data/spec/sitemap_generator/geo_sitemap_spec.rb +30 -0
  51. data/spec/sitemap_generator/helpers/number_helper_spec.rb +196 -0
  52. data/spec/sitemap_generator/interpreter_spec.rb +90 -0
  53. data/spec/sitemap_generator/link_set_spec.rb +864 -0
  54. data/spec/sitemap_generator/mobile_sitemap_spec.rb +27 -0
  55. data/spec/sitemap_generator/news_sitemap_spec.rb +42 -0
  56. data/spec/sitemap_generator/pagemap_sitemap_spec.rb +57 -0
  57. data/spec/sitemap_generator/sitemap_generator_spec.rb +582 -0
  58. data/spec/sitemap_generator/sitemap_groups_spec.rb +144 -0
  59. data/spec/sitemap_generator/sitemap_location_spec.rb +210 -0
  60. data/spec/sitemap_generator/sitemap_namer_spec.rb +96 -0
  61. data/spec/sitemap_generator/templates_spec.rb +24 -0
  62. data/spec/sitemap_generator/utilities/existence_spec.rb +26 -0
  63. data/spec/sitemap_generator/utilities/hash_spec.rb +57 -0
  64. data/spec/sitemap_generator/utilities/rounding_spec.rb +31 -0
  65. data/spec/sitemap_generator/utilities_spec.rb +101 -0
  66. data/spec/sitemap_generator/video_sitemap_spec.rb +117 -0
  67. data/spec/spec_helper.rb +24 -0
  68. data/spec/support/file_macros.rb +39 -0
  69. data/spec/support/schemas/siteindex.xsd +73 -0
  70. data/spec/support/schemas/sitemap-geo.xsd +41 -0
  71. data/spec/support/schemas/sitemap-mobile.xsd +32 -0
  72. data/spec/support/schemas/sitemap-news.xsd +159 -0
  73. data/spec/support/schemas/sitemap-pagemap.xsd +97 -0
  74. data/spec/support/schemas/sitemap-video.xsd +643 -0
  75. data/spec/support/schemas/sitemap.xsd +115 -0
  76. data/spec/support/xml_macros.rb +67 -0
  77. data/templates/sitemap.rb +27 -0
  78. metadata +226 -0
@@ -0,0 +1,49 @@
1
+ SitemapGenerator::Sitemap.default_host = "http://www.example.com"
2
+
3
+ SitemapGenerator::Sitemap.create(
4
+ :include_root => true, :include_index => true,
5
+ :filename => :new_sitemaps, :sitemaps_path => 'fr/') do
6
+
7
+ add('/one', :priority => 0.7, :changefreq => 'daily')
8
+
9
+ # Test a new location and filename and sitemaps host
10
+ group(:sitemaps_path => 'en/', :filename => :xxx,
11
+ :sitemaps_host => "http://newhost.com") do
12
+
13
+ add '/two'
14
+ add '/three'
15
+ end
16
+
17
+ # Test a simple namer.
18
+ group(:namer => SitemapGenerator::SimpleNamer.new(:abc, :start => 4, :zero => 3)) do
19
+ add '/four'
20
+ add '/five'
21
+ add '/six'
22
+ end
23
+
24
+ # Test a simple namer
25
+ group(:namer => SitemapGenerator::SimpleNamer.new(:def)) do
26
+ add '/four'
27
+ add '/five'
28
+ add '/six'
29
+ end
30
+
31
+ add '/seven'
32
+
33
+ # This should be in a file of its own.
34
+ # Not technically valid to have a link with a different host, but people like
35
+ # to do strange things sometimes.
36
+ group(:sitemaps_host => "http://exceptional.com") do
37
+ add '/eight'
38
+ add '/nine'
39
+ end
40
+
41
+ add '/ten'
42
+
43
+ # This should have no effect. Already added default links.
44
+ group(:include_root => true, :include_index => true) {}
45
+
46
+ # Not technically valid to have a link with a different host, but people like
47
+ # to do strange things sometimes
48
+ add "/merchant_path", :host => "https://www.merchanthost.com"
49
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ # Don't run this test as part of the unit testing suite as we don't want
6
+ # Fog to be a dependency of SitemapGenerator core. This is an integration
7
+ # test. Unfortunately it doesn't really test much, so its usefullness is
8
+ # questionable.
9
+ describe 'SitemapGenerator::S3Adapter', :integration => true do
10
+
11
+ let(:location) { SitemapGenerator::SitemapLocation.new(:namer => SitemapGenerator::SitemapNamer.new(:sitemap), :public_path => 'tmp/', :sitemaps_path => 'test/', :host => 'http://example.com/') }
12
+ let(:directory) { stub(:files => stub(:create)) }
13
+ let(:directories) { stub(:directories => stub(:new => directory)) }
14
+
15
+ before do
16
+ SitemapGenerator::S3Adapter # eager load
17
+ Fog::Storage.stubs(:new => directories)
18
+ end
19
+
20
+ it 'should create the file in S3 with a single operation' do
21
+ subject.write(location, 'payload')
22
+ end
23
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SitemapGenerator" do
4
+ it "should not include media element unless provided" do
5
+ xml_fragment = SitemapGenerator::Builder::SitemapUrl.new('link_with_alternates.html',
6
+ :host => 'http://www.example.com',
7
+ :alternates => [
8
+ {
9
+ :lang => 'de',
10
+ :href => 'http://www.example.de/link_with_alternate.html'
11
+ }
12
+ ]
13
+ ).to_xml
14
+
15
+ doc = Nokogiri::XML.parse("<root xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml'>#{xml_fragment}</root>")
16
+ url = doc.css('url')
17
+ url.should_not be_nil
18
+ url.css('loc').text.should == 'http://www.example.com/link_with_alternates.html'
19
+
20
+ alternate = url.at_xpath('xhtml:link')
21
+ alternate.should_not be_nil
22
+ alternate.attribute('rel').value.should == 'alternate'
23
+ alternate.attribute('hreflang').value.should == 'de'
24
+ alternate.attribute('media').should be_nil
25
+ end
26
+
27
+ it "should add alternate links to sitemap" do
28
+ xml_fragment = SitemapGenerator::Builder::SitemapUrl.new('link_with_alternates.html',
29
+ :host => 'http://www.example.com',
30
+ :alternates => [
31
+ {
32
+ :lang => 'de',
33
+ :href => 'http://www.example.de/link_with_alternate.html',
34
+ :media => 'only screen and (max-width: 640px)'
35
+ }
36
+ ]
37
+ ).to_xml
38
+
39
+ doc = Nokogiri::XML.parse("<root xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml'>#{xml_fragment}</root>")
40
+ url = doc.css('url')
41
+ url.should_not be_nil
42
+ url.css('loc').text.should == 'http://www.example.com/link_with_alternates.html'
43
+
44
+ alternate = url.at_xpath('xhtml:link')
45
+ alternate.should_not be_nil
46
+ alternate.attribute('rel').value.should == 'alternate'
47
+ alternate.attribute('hreflang').value.should == 'de'
48
+ alternate.attribute('href').value.should == 'http://www.example.de/link_with_alternate.html'
49
+ alternate.attribute('media').value.should == 'only screen and (max-width: 640px)'
50
+ end
51
+
52
+ it "should add alternate links to sitemap with rel nofollow" do
53
+ xml_fragment = SitemapGenerator::Builder::SitemapUrl.new('link_with_alternates.html',
54
+ :host => 'http://www.example.com',
55
+ :alternates => [
56
+ {
57
+ :lang => 'de',
58
+ :href => 'http://www.example.de/link_with_alternate.html',
59
+ :nofollow => true,
60
+ :media => 'only screen and (max-width: 640px)'
61
+ }
62
+ ]
63
+ ).to_xml
64
+
65
+ doc = Nokogiri::XML.parse("<root xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml'>#{xml_fragment}</root>")
66
+ url = doc.css('url')
67
+ url.should_not be_nil
68
+ url.css('loc').text.should == 'http://www.example.com/link_with_alternates.html'
69
+
70
+ alternate = url.at_xpath('xhtml:link')
71
+ alternate.should_not be_nil
72
+ alternate.attribute('rel').value.should == 'alternate nofollow'
73
+ alternate.attribute('hreflang').value.should == 'de'
74
+ alternate.attribute('href').value.should == 'http://www.example.de/link_with_alternate.html'
75
+ alternate.attribute('media').value.should == 'only screen and (max-width: 640px)'
76
+ end
77
+
78
+ end
79
+
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe SitemapGenerator::Application do
4
+ before :all do
5
+ SitemapGenerator::Utilities.with_warnings(nil) do
6
+ Object.const_set(:Rails, Object.new)
7
+ end
8
+ end
9
+
10
+ after :all do
11
+ SitemapGenerator::Utilities.with_warnings(nil) do
12
+ Object.const_set(:Rails, nil)
13
+ end
14
+ end
15
+
16
+ before :each do
17
+ @app = SitemapGenerator::Application.new
18
+ end
19
+
20
+ describe "rails3?" do
21
+ tests = {
22
+ :nil => false,
23
+ '2.3.11' => false,
24
+ '3.0.1' => true,
25
+ '3.0.11' => true
26
+ }
27
+
28
+ it "should identify the rails version correctly" do
29
+ tests.each do |version, result|
30
+ Rails.expects(:version).returns(version)
31
+ @app.rails3?.should == result
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "with Rails" do
37
+ before :each do
38
+ @root = '/test'
39
+ Rails.expects(:root).returns(@root).at_least_once
40
+ end
41
+
42
+ it "should use the Rails.root" do
43
+ @app.root.should be_a(Pathname)
44
+ @app.root.to_s.should == @root
45
+ (@app.root + 'public/').to_s.should == File.join(@root, 'public/')
46
+ end
47
+ end
48
+
49
+ describe "with no Rails" do
50
+ before :each do
51
+ @rails = Rails
52
+ Object.send(:remove_const, :Rails)
53
+ end
54
+
55
+ after :each do
56
+ Object::Rails = @rails
57
+ end
58
+
59
+ it "should not be Rails" do
60
+ @app.rails?.should be_false
61
+ end
62
+
63
+ it "should use the current working directory" do
64
+ @app.root.should be_a(Pathname)
65
+ @app.root.to_s.should == Dir.getwd
66
+ (@app.root + 'public/').to_s.should == File.join(Dir.getwd, 'public/')
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'SitemapGenerator::Builder::SitemapFile' do
4
+ let(:location) { SitemapGenerator::SitemapLocation.new(:namer => SitemapGenerator::SimpleNamer.new(:sitemap, :start => 2, :zero => 1), :public_path => 'tmp/', :sitemaps_path => 'test/', :host => 'http://example.com/') }
5
+ let(:sitemap) { SitemapGenerator::Builder::SitemapFile.new(location) }
6
+
7
+ it "should have a default namer" do
8
+ sitemap = SitemapGenerator::Builder::SitemapFile.new
9
+ sitemap.location.filename.should == 'sitemap1.xml.gz'
10
+ end
11
+
12
+ it "should return the name of the sitemap file" do
13
+ sitemap.location.filename.should == 'sitemap1.xml.gz'
14
+ end
15
+
16
+ it "should return the URL" do
17
+ sitemap.location.url.should == 'http://example.com/test/sitemap1.xml.gz'
18
+ end
19
+
20
+ it "should return the path" do
21
+ sitemap.location.path.should == File.expand_path('tmp/test/sitemap1.xml.gz')
22
+ end
23
+
24
+ it "should be empty" do
25
+ sitemap.empty?.should be_true
26
+ sitemap.link_count.should == 0
27
+ end
28
+
29
+ it "should not be finalized" do
30
+ sitemap.finalized?.should be_false
31
+ end
32
+
33
+ it "should raise if no default host is set" do
34
+ lambda { SitemapGenerator::Builder::SitemapFile.new.location.url }.should raise_error(SitemapGenerator::SitemapError)
35
+ end
36
+
37
+ describe "lastmod" do
38
+ it "should be the file last modified time" do
39
+ lastmod = (Time.now - 1209600)
40
+ sitemap.location.reserve_name
41
+ File.expects(:mtime).with(sitemap.location.path).returns(lastmod)
42
+ sitemap.lastmod.should == lastmod
43
+ end
44
+
45
+ it "should be nil if the location has not reserved a name" do
46
+ File.expects(:mtime).never
47
+ sitemap.lastmod.should be_nil
48
+ end
49
+
50
+ it "should be nil if location has reserved a name and the file DNE" do
51
+ sitemap.location.reserve_name
52
+ File.expects(:mtime).raises(Errno::ENOENT)
53
+ sitemap.lastmod.should be_nil
54
+ end
55
+ end
56
+
57
+ describe "new" do
58
+ let(:original_sitemap) { sitemap }
59
+ let(:new_sitemap) { sitemap.new }
60
+
61
+ before :each do
62
+ original_sitemap
63
+ new_sitemap
64
+ end
65
+
66
+ it "should inherit the same options" do
67
+ # The name is the same because the original sitemap was not finalized
68
+ new_sitemap.location.url.should == 'http://example.com/test/sitemap1.xml.gz'
69
+ new_sitemap.location.path.should == File.expand_path('tmp/test/sitemap1.xml.gz')
70
+ end
71
+
72
+ it "should not share the same location instance" do
73
+ new_sitemap.location.should_not be(original_sitemap.location)
74
+ end
75
+
76
+ it "should inherit the same namer instance" do
77
+ new_sitemap.location.namer.should == original_sitemap.location.namer
78
+ end
79
+ end
80
+
81
+ describe "reserve_name" do
82
+ it "should reserve the name from the location" do
83
+ sitemap.reserved_name?.should be_false
84
+ sitemap.location.expects(:reserve_name).returns('name')
85
+ sitemap.reserve_name
86
+ sitemap.reserved_name?.should be_true
87
+ sitemap.instance_variable_get(:@reserved_name).should == 'name'
88
+ end
89
+
90
+ it "should be safe to call multiple times" do
91
+ sitemap.location.expects(:reserve_name).returns('name').once
92
+ sitemap.reserve_name
93
+ sitemap.reserve_name
94
+ end
95
+ end
96
+
97
+ describe "add" do
98
+ it "should use the host provided" do
99
+ url = SitemapGenerator::Builder::SitemapUrl.new('/one', :host => 'http://newhost.com/')
100
+ SitemapGenerator::Builder::SitemapUrl.expects(:new).with('/one', :host => 'http://newhost.com').returns(url)
101
+ sitemap.add '/one', :host => 'http://newhost.com'
102
+ end
103
+
104
+ it "should use the host from the location" do
105
+ url = SitemapGenerator::Builder::SitemapUrl.new('/one', :host => 'http://example.com/')
106
+ SitemapGenerator::Builder::SitemapUrl.expects(:new).with('/one', :host => 'http://example.com/').returns(url)
107
+ sitemap.add '/one'
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'SitemapGenerator::Builder::SitemapIndexFile' do
4
+ let(:location) { SitemapGenerator::SitemapLocation.new(:filename => 'sitemap.xml.gz', :public_path => '/public/', :host => 'http://example.com/') }
5
+ let(:index) { SitemapGenerator::Builder::SitemapIndexFile.new(location) }
6
+
7
+ before :each do
8
+ index.location[:sitemaps_path] = 'test/'
9
+ end
10
+
11
+ it "should return the URL" do
12
+ index.location.url.should == 'http://example.com/test/sitemap.xml.gz'
13
+ end
14
+
15
+ it "should return the path" do
16
+ index.location.path.should == '/public/test/sitemap.xml.gz'
17
+ end
18
+
19
+ it "should be empty" do
20
+ index.empty?.should be_true
21
+ index.link_count.should == 0
22
+ end
23
+
24
+ it "should not have a last modification data" do
25
+ index.lastmod.should be_nil
26
+ end
27
+
28
+ it "should not be finalized" do
29
+ index.finalized?.should be_false
30
+ end
31
+
32
+ it "filename should be set" do
33
+ index.location.filename.should == 'sitemap.xml.gz'
34
+ end
35
+
36
+ it "should have a default namer" do
37
+ index = SitemapGenerator::Builder::SitemapIndexFile.new
38
+ index.location.filename.should == 'sitemap.xml.gz'
39
+ end
40
+
41
+ describe "link_count" do
42
+ it "should return the link count" do
43
+ index.instance_variable_set(:@link_count, 10)
44
+ index.link_count.should == 10
45
+ end
46
+ end
47
+
48
+ describe "create_index?" do
49
+ it "should return false" do
50
+ index.location[:create_index] = false
51
+ index.create_index?.should be_false
52
+
53
+ index.instance_variable_set(:@link_count, 10)
54
+ index.create_index?.should be_false
55
+ end
56
+
57
+ it "should return true" do
58
+ index.location[:create_index] = true
59
+ index.create_index?.should be_true
60
+
61
+ index.instance_variable_set(:@link_count, 1)
62
+ index.create_index?.should be_true
63
+ end
64
+
65
+ it "when :auto, should be true if more than one link" do
66
+ index.instance_variable_set(:@link_count, 1)
67
+ index.location[:create_index] = :auto
68
+ index.create_index?.should be_false
69
+
70
+ index.instance_variable_set(:@link_count, 2)
71
+ index.create_index?.should be_true
72
+ end
73
+ end
74
+
75
+ describe "add" do
76
+ it "should use the host provided" do
77
+ url = SitemapGenerator::Builder::SitemapIndexUrl.new('/one', :host => 'http://newhost.com/')
78
+ SitemapGenerator::Builder::SitemapIndexUrl.expects(:new).with('/one', :host => 'http://newhost.com').returns(url)
79
+ index.add '/one', :host => 'http://newhost.com'
80
+ end
81
+
82
+ it "should use the host from the location" do
83
+ url = SitemapGenerator::Builder::SitemapIndexUrl.new('/one', :host => 'http://example.com/')
84
+ SitemapGenerator::Builder::SitemapIndexUrl.expects(:new).with('/one', :host => 'http://example.com/').returns(url)
85
+ index.add '/one'
86
+ end
87
+
88
+ describe "when adding manually" do
89
+ it "should reserve a name" do
90
+ index.expects(:reserve_name)
91
+ index.add '/link'
92
+ end
93
+
94
+ it "should create index" do
95
+ index.create_index?.should be_false
96
+ index.add '/one'
97
+ index.create_index?.should be_true
98
+ end
99
+ end
100
+ end
101
+
102
+ describe "index_url" do
103
+ it "when not creating an index, should be the first sitemap url" do
104
+ index.instance_variable_set(:@create_index, false)
105
+ index.instance_variable_set(:@first_sitemap_url, 'http://test.com/index.xml')
106
+ index.create_index?.should be_false
107
+ index.index_url.should == 'http://test.com/index.xml'
108
+ end
109
+
110
+ it "if there's no first sitemap url, should default to the index location url" do
111
+ index.instance_variable_set(:@create_index, false)
112
+ index.instance_variable_set(:@first_sitemap_url, nil)
113
+ index.create_index?.should be_false
114
+ index.index_url.should == index.location.url
115
+ index.index_url.should == 'http://example.com/test/sitemap.xml.gz'
116
+ end
117
+
118
+ it "when creating an index, should be the index location url" do
119
+ index.instance_variable_set(:@create_index, true)
120
+ index.index_url.should == index.location.url
121
+ index.index_url.should == 'http://example.com/test/sitemap.xml.gz'
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe SitemapGenerator::Builder::SitemapIndexUrl do
4
+ let(:index) {
5
+ SitemapGenerator::Builder::SitemapIndexFile.new(
6
+ :sitemaps_path => 'sitemaps/',
7
+ :host => 'http://test.com',
8
+ :filename => 'sitemap_index.xml.gz'
9
+ )
10
+ }
11
+ let(:url) { SitemapGenerator::Builder::SitemapUrl.new(index) }
12
+
13
+ it "should return the correct url" do
14
+ url[:loc].should == 'http://test.com/sitemaps/sitemap_index.xml.gz'
15
+ end
16
+
17
+ it "should use the host from the index" do
18
+ host = 'http://myexample.com'
19
+ index.location.expects(:host).returns(host)
20
+ url[:host].should == host
21
+ end
22
+
23
+ it "should use the public path for the link" do
24
+ path = '/path'
25
+ index.location.expects(:path_in_public).returns(path)
26
+ url[:loc].should == 'http://test.com/path'
27
+ end
28
+ end
@@ -0,0 +1,186 @@
1
+ require 'spec_helper'
2
+
3
+ describe SitemapGenerator::Builder::SitemapUrl do
4
+ let(:loc) {
5
+ SitemapGenerator::SitemapLocation.new(
6
+ :sitemaps_path => 'sitemaps/',
7
+ :public_path => '/public',
8
+ :host => 'http://test.com',
9
+ :namer => SitemapGenerator::SimpleNamer.new(:sitemap)
10
+ )}
11
+ let(:sitemap_file) { SitemapGenerator::Builder::SitemapFile.new(loc) }
12
+
13
+ def new_url(*args)
14
+ if args.empty?
15
+ args = ['/home', { :host => 'http://example.com' }]
16
+ end
17
+ SitemapGenerator::Builder::SitemapUrl.new(*args)
18
+ end
19
+
20
+ it "should build urls for sitemap files" do
21
+ url = SitemapGenerator::Builder::SitemapUrl.new(sitemap_file)
22
+ url[:loc].should == 'http://test.com/sitemaps/sitemap.xml.gz'
23
+ end
24
+
25
+ it "lastmod should default to the last modified date for sitemap files" do
26
+ lastmod = (Time.now - 1209600)
27
+ sitemap_file.expects(:lastmod).returns(lastmod)
28
+ url = SitemapGenerator::Builder::SitemapUrl.new(sitemap_file)
29
+ url[:lastmod].should == lastmod
30
+ end
31
+
32
+ it "should support subdirectory routing" do
33
+ url = SitemapGenerator::Builder::SitemapUrl.new('/profile', :host => 'http://example.com/subdir/')
34
+ url[:loc].should == 'http://example.com/subdir/profile'
35
+ url = SitemapGenerator::Builder::SitemapUrl.new('profile', :host => 'http://example.com/subdir/')
36
+ url[:loc].should == 'http://example.com/subdir/profile'
37
+ url = SitemapGenerator::Builder::SitemapUrl.new('/deep/profile/', :host => 'http://example.com/subdir/')
38
+ url[:loc].should == 'http://example.com/subdir/deep/profile/'
39
+ url = SitemapGenerator::Builder::SitemapUrl.new('/deep/profile', :host => 'http://example.com/subdir')
40
+ url[:loc].should == 'http://example.com/subdir/deep/profile'
41
+ url = SitemapGenerator::Builder::SitemapUrl.new('deep/profile', :host => 'http://example.com/subdir')
42
+ url[:loc].should == 'http://example.com/subdir/deep/profile'
43
+ url = SitemapGenerator::Builder::SitemapUrl.new('deep/profile/', :host => 'http://example.com/subdir/')
44
+ url[:loc].should == 'http://example.com/subdir/deep/profile/'
45
+ url = SitemapGenerator::Builder::SitemapUrl.new('/', :host => 'http://example.com/subdir/')
46
+ url[:loc].should == 'http://example.com/subdir/'
47
+ end
48
+
49
+ it "should not fail on a nil path segment" do
50
+ lambda do
51
+ SitemapGenerator::Builder::SitemapUrl.new(nil, :host => 'http://example.com')[:loc].should == 'http://example.com'
52
+ end.should_not raise_error
53
+ end
54
+
55
+ it "should support a :videos option" do
56
+ loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :videos => [1,2,3])
57
+ loc[:videos].should == [1,2,3]
58
+ end
59
+
60
+ it "should support a singular :video option" do
61
+ loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :video => 1)
62
+ loc[:videos].should == [1]
63
+ end
64
+
65
+ it "should support an array :video option" do
66
+ loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :video => [1,2], :videos => [3,4])
67
+ loc[:videos].should == [3,4,1,2]
68
+ end
69
+
70
+ it "should support a :alternates option" do
71
+ loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :alternates => [1,2,3])
72
+ loc[:alternates].should == [1,2,3]
73
+ end
74
+
75
+ it "should support a singular :alternate option" do
76
+ loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :alternate => 1)
77
+ loc[:alternates].should == [1]
78
+ end
79
+
80
+ it "should support an array :alternate option" do
81
+ loc = SitemapGenerator::Builder::SitemapUrl.new('', :host => 'http://test.com', :alternate => [1,2], :alternates => [3,4])
82
+ loc[:alternates].should == [3,4,1,2]
83
+ end
84
+
85
+ it "should not fail if invalid characters are used in the URL" do
86
+ special = ':$&+,;:=?@'
87
+ url = SitemapGenerator::Builder::SitemapUrl.new("/#{special}", :host => "http://example.com/#{special}/")
88
+ url[:loc].should == "http://example.com/#{special}/#{special}"
89
+ end
90
+
91
+ describe "w3c_date" do
92
+ it "should convert dates and times to W3C format" do
93
+ url = new_url
94
+ url.send(:w3c_date, Date.new(0)).should == '0000-01-01'
95
+ url.send(:w3c_date, Time.at(0).utc).should == '1970-01-01T00:00:00+00:00'
96
+ url.send(:w3c_date, DateTime.new(0)).should == '0000-01-01T00:00:00+00:00'
97
+ end
98
+
99
+ it "should return strings unmodified" do
100
+ new_url.send(:w3c_date, '2010-01-01').should == '2010-01-01'
101
+ end
102
+
103
+ it "should try to convert to utc" do
104
+ time = Time.at(0)
105
+ time.expects(:respond_to?).times(2).returns(false, true) # iso8601, utc
106
+ new_url.send(:w3c_date, time).should == '1970-01-01T00:00:00+00:00'
107
+ end
108
+
109
+ it "should include timezone for objects which do not respond to iso8601 or utc" do
110
+ time = Time.at(0)
111
+ time.expects(:respond_to?).times(2).returns(false, false) # iso8601, utc
112
+ time.expects(:strftime).times(2).returns('+0800', '1970-01-01T00:00:00')
113
+ new_url.send(:w3c_date, time).should == '1970-01-01T00:00:00+08:00'
114
+ end
115
+
116
+ it "should support integers" do
117
+ new_url.send(:w3c_date, Time.at(0).to_i).should == '1970-01-01T00:00:00+00:00'
118
+ end
119
+ end
120
+
121
+ describe "yes_or_no" do
122
+ it "should recognize truthy values" do
123
+ new_url.send(:yes_or_no, 1).should == 'yes'
124
+ new_url.send(:yes_or_no, 0).should == 'yes'
125
+ new_url.send(:yes_or_no, 'yes').should == 'yes'
126
+ new_url.send(:yes_or_no, 'Yes').should == 'yes'
127
+ new_url.send(:yes_or_no, 'YES').should == 'yes'
128
+ new_url.send(:yes_or_no, true).should == 'yes'
129
+ new_url.send(:yes_or_no, Object.new).should == 'yes'
130
+ end
131
+
132
+ it "should recognize falsy values" do
133
+ new_url.send(:yes_or_no, nil).should == 'no'
134
+ new_url.send(:yes_or_no, 'no').should == 'no'
135
+ new_url.send(:yes_or_no, 'No').should == 'no'
136
+ new_url.send(:yes_or_no, 'NO').should == 'no'
137
+ new_url.send(:yes_or_no, false).should == 'no'
138
+ end
139
+
140
+ it "should raise on unrecognized strings" do
141
+ lambda { new_url.send(:yes_or_no, 'dunno') }.should raise_error(ArgumentError)
142
+ lambda { new_url.send(:yes_or_no, 'yessir') }.should raise_error(ArgumentError)
143
+ end
144
+ end
145
+
146
+ describe "yes_or_no_with_default" do
147
+ it "should use the default if the value is nil" do
148
+ url = new_url
149
+ url.expects(:yes_or_no).with(true).returns('surely')
150
+ url.send(:yes_or_no_with_default, nil, true).should == 'surely'
151
+ end
152
+
153
+ it "should use the value if it is not nil" do
154
+ url = new_url
155
+ url.expects(:yes_or_no).with('surely').returns('absolutely')
156
+ url.send(:yes_or_no_with_default, 'surely', true).should == 'absolutely'
157
+ end
158
+ end
159
+
160
+ describe "format_float" do
161
+ it "should not modify if a string" do
162
+ new_url.send(:format_float, '0.4').should == '0.4'
163
+ end
164
+
165
+ it "should round to one decimal place" do
166
+ url = new_url
167
+ url.send(:format_float, 0.499999).should == '0.5'
168
+ url.send(:format_float, 3.444444).should == '3.4'
169
+ end
170
+ end
171
+
172
+ describe "expires" do
173
+ let(:url) { SitemapGenerator::Builder::SitemapUrl.new('/path', :host => 'http://example.com', :expires => time) }
174
+ let(:time) { Time.at(0).utc }
175
+
176
+ it "should include the option" do
177
+ url[:expires].should == time
178
+ end
179
+
180
+ it "should format it and include it in the XML" do
181
+ xml = url.to_xml
182
+ doc = Nokogiri::XML("<root xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml'>#{xml}</root>")
183
+ doc.css('url expires').text.should == url.send(:w3c_date, time)
184
+ end
185
+ end
186
+ end