sitemap_generator 4.3.1 → 5.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,66 +1,6 @@
1
1
  module SitemapGenerator
2
- # A class for generating sitemap names given the base for the filename.
3
- # Deprecated. Rather use the <tt>SitemapGenerator::SimpleNamer</tt> class and the
4
- # +namer+ option on your sitemap object.
2
+ # A class for generating sitemap filenames.
5
3
  #
6
- # === Example
7
- # namer = SitemapNamer.new(:sitemap)
8
- # namer.to_s => 'sitemap1.xml.gz'
9
- # namer.next.to_s => 'sitemap2.xml.gz'
10
- class SitemapNamer
11
- NameError = Class.new(StandardError)
12
-
13
- # Params:
14
- # base - string or symbol that forms the base of the generated filename
15
- #
16
- # Options include:
17
- # :extension - Default: '.xml.gz'. File extension to append.
18
- # :start - Default: 1. Numerical index at which to start counting.
19
- def initialize(base, options={});
20
- @options = SitemapGenerator::Utilities.reverse_merge(options,
21
- :extension => '.xml.gz',
22
- :start => 1
23
- )
24
- @base = base
25
- reset
26
- end
27
-
28
- def to_s
29
- "#{@base}#{@count}#{@options[:extension]}"
30
- end
31
-
32
- # Increment count and return self
33
- def next
34
- @count += 1
35
- self
36
- end
37
-
38
- # Decrement count and return self
39
- def previous
40
- raise NameError, "Already at the start of the series" if start?
41
- @count -= 1
42
- self
43
- end
44
-
45
- # Reset count to the starting index
46
- def reset
47
- @count = @options[:start]
48
- end
49
-
50
- def start?
51
- @count <= @options[:start]
52
- end
53
- end
54
-
55
- # A Namer for Sitemap Indexes.
56
- # Deprecated. Rather use the <tt>SitemapGenerator::SimpleNamer</tt> class and the
57
- # +namer+ option on your sitemap object.
58
- class SitemapIndexNamer < SitemapNamer
59
- def to_s
60
- "#{@base}#{@options[:extension]}"
61
- end
62
- end
63
-
64
4
  # The SimpleNamer uses the same namer instance for the sitemap index and the sitemaps.
65
5
  # If no index is needed, the first sitemap gets the first name. However, if
66
6
  # an index is needed, the index gets the first name.
@@ -85,16 +25,20 @@ module SitemapGenerator
85
25
  # the first name. Thereafter, the numerical index defined by +start+
86
26
  # is used, and subsequent names would be 'sitemap1.xml.gz', 'sitemap2.xml.gz', etc.
87
27
  # In these examples the `base` string is assumed to be 'sitemap'.
88
- class SimpleNamer < SitemapNamer
28
+ class SimpleNamer
89
29
  def initialize(base, options={})
90
- super_options = SitemapGenerator::Utilities.reverse_merge(options,
91
- :zero => nil # identifies the marker for the start of the series
30
+ @options = SitemapGenerator::Utilities.reverse_merge(options,
31
+ :zero => nil, # identifies the marker for the start of the series
32
+ :extension => '.xml.gz',
33
+ :start => 1
92
34
  )
93
- super(base, super_options)
35
+ @base = base
36
+ reset
94
37
  end
95
38
 
96
39
  def to_s
97
- "#{@base}#{@count}#{@options[:extension]}"
40
+ extension = @options[:extension]
41
+ "#{@base}#{@count}#{extension}"
98
42
  end
99
43
 
100
44
  # Reset to the first name
@@ -14,15 +14,15 @@ SitemapGenerator::Sitemap.create(
14
14
  add '/three'
15
15
  end
16
16
 
17
- # Test a deprecated namer
18
- group(:sitemaps_namer => SitemapGenerator::SitemapNamer.new(:abc, :start => 3)) do
17
+ # Test a simple namer.
18
+ group(:namer => SitemapGenerator::SimpleNamer.new(:abc, :start => 4, :zero => 3)) do
19
19
  add '/four'
20
20
  add '/five'
21
21
  add '/six'
22
22
  end
23
23
 
24
24
  # Test a simple namer
25
- group(:sitemaps_namer => SitemapGenerator::SimpleNamer.new(:def)) do
25
+ group(:namer => SitemapGenerator::SimpleNamer.new(:def)) do
26
26
  add '/four'
27
27
  add '/five'
28
28
  add '/six'
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'SitemapGenerator::Builder::SitemapFile' do
4
- let(:location) { SitemapGenerator::SitemapLocation.new(:namer => SitemapGenerator::SitemapNamer.new(:sitemap), :public_path => 'tmp/', :sitemaps_path => 'test/', :host => 'http://example.com/') }
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
5
  let(:sitemap) { SitemapGenerator::Builder::SitemapFile.new(location) }
6
6
 
7
7
  it "should have a default namer" do
@@ -6,7 +6,7 @@ describe SitemapGenerator::Builder::SitemapUrl do
6
6
  :sitemaps_path => 'sitemaps/',
7
7
  :public_path => '/public',
8
8
  :host => 'http://test.com',
9
- :namer => SitemapGenerator::SitemapNamer.new(:sitemap)
9
+ :namer => SitemapGenerator::SimpleNamer.new(:sitemap)
10
10
  )}
11
11
  let(:sitemap_file) { SitemapGenerator::Builder::SitemapFile.new(loc) }
12
12
 
@@ -19,7 +19,7 @@ describe SitemapGenerator::Builder::SitemapUrl do
19
19
 
20
20
  it "should build urls for sitemap files" do
21
21
  url = SitemapGenerator::Builder::SitemapUrl.new(sitemap_file)
22
- url[:loc].should == 'http://test.com/sitemaps/sitemap1.xml.gz'
22
+ url[:loc].should == 'http://test.com/sitemaps/sitemap.xml.gz'
23
23
  end
24
24
 
25
25
  it "lastmod should default to the last modified date for sitemap files" do
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SitemapGenerator::FileAdapter" do
4
+ let(:location) { SitemapGenerator::SitemapLocation.new }
5
+ let(:adapter) { SitemapGenerator::FileAdapter.new }
6
+
7
+ describe "write" do
8
+ it "should gzip contents if filename ends in .gz" do
9
+ adapter.expects(:gzip).once
10
+ location.stubs(:filename).returns('sitemap.xml.gz')
11
+ adapter.write(location, 'data')
12
+ end
13
+
14
+ it "should not gzip contents if filename does not end in .gz" do
15
+ adapter.expects(:plain).once
16
+ location.stubs(:filename).returns('sitemap.xml')
17
+ adapter.write(location, 'data')
18
+ end
19
+ end
20
+ end
@@ -41,7 +41,7 @@ describe SitemapGenerator::LinkSet do
41
41
  ls = SitemapGenerator::LinkSet.new(:default_host => default_host, :include_root => true, :include_index => true)
42
42
  ls.include_root.should be_true
43
43
  ls.include_index.should be_true
44
- ls.add_links { |sitemap| }
44
+ ls.create { |sitemap| }
45
45
  ls.sitemap.link_count.should == 2
46
46
  end
47
47
 
@@ -49,7 +49,7 @@ describe SitemapGenerator::LinkSet do
49
49
  ls = SitemapGenerator::LinkSet.new(:default_host => default_host, :include_root => false)
50
50
  ls.include_root.should be_false
51
51
  ls.include_index.should be_false
52
- ls.add_links { |sitemap| }
52
+ ls.create { |sitemap| }
53
53
  ls.sitemap.link_count.should == 0
54
54
  end
55
55
 
@@ -57,7 +57,7 @@ describe SitemapGenerator::LinkSet do
57
57
  ls = SitemapGenerator::LinkSet.new(:default_host => default_host, :include_index => false)
58
58
  ls.include_root.should be_true
59
59
  ls.include_index.should be_false
60
- ls.add_links { |sitemap| }
60
+ ls.create { |sitemap| }
61
61
  ls.sitemap.link_count.should == 1
62
62
  end
63
63
 
@@ -65,7 +65,7 @@ describe SitemapGenerator::LinkSet do
65
65
  ls = SitemapGenerator::LinkSet.new(:default_host => default_host, :include_root => false, :include_index => false)
66
66
  ls.include_root.should be_false
67
67
  ls.include_index.should be_false
68
- ls.add_links { |sitemap| }
68
+ ls.create { |sitemap| }
69
69
  ls.sitemap.link_count.should == 0
70
70
  end
71
71
  end
@@ -208,8 +208,8 @@ describe SitemapGenerator::LinkSet do
208
208
  let(:ls) { SitemapGenerator::LinkSet.new(:default_host => default_host, :verbose => true, :create_index => true) }
209
209
 
210
210
  it "should output summary lines" do
211
- ls.sitemap.expects(:summary)
212
- ls.sitemap_index.expects(:summary)
211
+ ls.sitemap.location.expects(:summary)
212
+ ls.sitemap_index.location.expects(:summary)
213
213
  ls.finalize!
214
214
  end
215
215
  end
@@ -365,22 +365,22 @@ describe SitemapGenerator::LinkSet do
365
365
  ls.group(:sitemaps_host => 'http://test.com') {}
366
366
  end
367
367
 
368
- it "should use the same sitemaps_namer" do
368
+ it "should use the same namer" do
369
369
  @group = ls.group(:sitemaps_host => 'http://test.com') {}
370
370
  @group.sitemap.location.namer.should == ls.sitemap.location.namer
371
371
  end
372
372
  end
373
373
 
374
- describe "sitemaps_namer" do
374
+ describe "namer" do
375
375
  it "should inherit the value" do
376
- ls.group.sitemaps_namer.should == ls.sitemaps_namer
377
- ls.group.sitemap.location.namer.should == ls.sitemaps_namer
376
+ ls.group.namer.should == ls.namer
377
+ ls.group.sitemap.location.namer.should == ls.namer
378
378
  end
379
379
 
380
380
  it "should set the value" do
381
- namer = SitemapGenerator::SitemapNamer.new(:xxx)
382
- group = ls.group(:sitemaps_namer => namer)
383
- group.sitemaps_namer.should == namer
381
+ namer = SitemapGenerator::SimpleNamer.new(:xxx)
382
+ group = ls.group(:namer => namer)
383
+ group.namer.should == namer
384
384
  group.sitemap.location.namer.should == namer
385
385
  group.sitemap.location.filename.should =~ /xxx/
386
386
  end
@@ -412,7 +412,7 @@ describe SitemapGenerator::LinkSet do
412
412
  :filename => :xxx,
413
413
  :sitemaps_path => 'en/',
414
414
  :filename => :example,
415
- :sitemaps_namer => SitemapGenerator::SitemapNamer.new(:sitemap)
415
+ :namer => SitemapGenerator::SimpleNamer.new(:sitemap)
416
416
  }.each do |key, value|
417
417
  it "if #{key} is present" do
418
418
  ls.group(key => value).sitemap.should_not == ls.sitemap
@@ -434,7 +434,8 @@ describe SitemapGenerator::LinkSet do
434
434
 
435
435
  {:sitemaps_path => 'en/',
436
436
  :filename => :example,
437
- :sitemaps_namer => SitemapGenerator::SitemapNamer.new(:sitemap)}.each do |k, v|
437
+ :namer => SitemapGenerator::SimpleNamer.new(:sitemap)
438
+ }.each do |k, v|
438
439
 
439
440
  it "should not finalize the sitemap if #{k} is present" do
440
441
  ls.expects(:finalize_sitemap!).never
@@ -524,30 +525,30 @@ describe SitemapGenerator::LinkSet do
524
525
  ls.sitemap.location.host.should == host
525
526
  end
526
527
 
527
- it "should set the sitemaps_namer" do
528
- namer = SitemapGenerator::SitemapNamer.new(:xxx)
529
- ls.create(:sitemaps_namer => namer)
530
- ls.sitemaps_namer.should == namer
528
+ it "should set the namer" do
529
+ namer = SitemapGenerator::SimpleNamer.new(:xxx)
530
+ ls.create(:namer => namer)
531
+ ls.namer.should == namer
531
532
  ls.sitemap.location.namer.should == namer
532
533
  ls.sitemap.location.filename.should =~ /xxx/
533
534
  end
534
535
 
535
- it "should support both sitemaps_namer and filename options" do
536
- namer = SitemapGenerator::SitemapNamer.new("sitemap1_")
537
- ls.create(:sitemaps_namer => namer, :filename => "sitemap1")
538
- ls.sitemaps_namer.should == namer
536
+ it "should support both namer and filename options" do
537
+ namer = SitemapGenerator::SimpleNamer.new("sitemap2")
538
+ ls.create(:namer => namer, :filename => "sitemap1")
539
+ ls.namer.should == namer
539
540
  ls.sitemap.location.namer.should == namer
540
- ls.sitemap.location.filename.should =~ /sitemap1_1/
541
- ls.sitemap_index.location.filename.should =~ /^sitemap1/
541
+ ls.sitemap.location.filename.should =~ /^sitemap2/
542
+ ls.sitemap_index.location.filename.should =~ /^sitemap2/
542
543
  end
543
544
 
544
- it "should support both sitemaps_namer and filename options no matter the order" do
545
- namer = SitemapGenerator::SitemapNamer.new("sitemap1_")
546
- options = {} #ActiveSupport::OrderedHash.new
547
- options[:sitemaps_namer] = namer
548
- options[:filename] = "sitemap1"
545
+ it "should support both namer and filename options no matter the order" do
546
+ options = {
547
+ :namer => SitemapGenerator::SimpleNamer.new('sitemap1'),
548
+ :filename => 'sitemap2'
549
+ }
549
550
  ls.create(options)
550
- ls.sitemap.location.filename.should =~ /sitemap1_1/
551
+ ls.sitemap.location.filename.should =~ /^sitemap1/
551
552
  ls.sitemap_index.location.filename.should =~ /^sitemap1/
552
553
  end
553
554
 
@@ -574,13 +575,6 @@ describe SitemapGenerator::LinkSet do
574
575
  SitemapGenerator::Sitemap.create(:default_host => 'http://cnn.com')
575
576
  SitemapGenerator::Sitemap.instance_variable_set(:@added_default_links, false)
576
577
  end
577
-
578
- it "should reset the deprecated sitemaps_namer, if set" do
579
- ls.sitemaps_namer = stub(:reset => nil)
580
- ls.sitemaps_namer.expects(:reset)
581
- ls.send(:reset!)
582
- end
583
-
584
578
  end
585
579
 
586
580
  describe "include_root?" do
@@ -668,26 +662,6 @@ describe SitemapGenerator::LinkSet do
668
662
  end
669
663
  end
670
664
 
671
- describe "add_links" do
672
- it "should not change the value of yield_sitemap" do
673
- ls.stubs(:create)
674
- ls.yield_sitemap = false
675
- ls.add_links
676
- ls.yield_sitemap.should be_false
677
- ls.yield_sitemap = true
678
- ls.add_links
679
- ls.yield_sitemap.should be_true
680
- end
681
-
682
- it "should always yield the sitemap instance" do
683
- ls.send(:interpreter).expects(:eval).with(:yield_sitemap => true).twice
684
- ls.yield_sitemap = false
685
- ls.add_links
686
- ls.yield_sitemap = true
687
- ls.add_links
688
- end
689
- end
690
-
691
665
  describe "add" do
692
666
  it "should not modify the options hash" do
693
667
  options = { :host => 'http://newhost.com' }
@@ -745,7 +719,7 @@ describe SitemapGenerator::LinkSet do
745
719
  end
746
720
 
747
721
  describe "create_index" do
748
- let(:location) { SitemapGenerator::SitemapLocation.new(:namer => SitemapGenerator::SitemapNamer.new(:sitemap), :public_path => 'tmp/', :sitemaps_path => 'test/', :host => 'http://example.com/') }
722
+ let(:location) { SitemapGenerator::SitemapLocation.new(:namer => SitemapGenerator::SimpleNamer.new(:sitemap), :public_path => 'tmp/', :sitemaps_path => 'test/', :host => 'http://example.com/') }
749
723
  let(:sitemap) { SitemapGenerator::Builder::SitemapFile.new(location) }
750
724
 
751
725
  describe "when false" do
@@ -851,4 +825,40 @@ describe SitemapGenerator::LinkSet do
851
825
  ls.send(:finalize_sitemap!)
852
826
  end
853
827
  end
828
+
829
+ describe "compress" do
830
+ it "should be true by default" do
831
+ ls.compress.should be_true
832
+ end
833
+
834
+ it "should be set on the location objects" do
835
+ ls.sitemap.location[:compress].should be_true
836
+ ls.sitemap_index.location[:compress].should be_true
837
+ end
838
+
839
+ it "should be settable and gettable" do
840
+ ls.compress = false
841
+ ls.compress.should be_false
842
+ ls.compress = :all_but_first
843
+ ls.compress.should == :all_but_first
844
+ end
845
+
846
+ it "should update the location objects when set" do
847
+ ls.compress = false
848
+ ls.sitemap.location[:compress].should be_false
849
+ ls.sitemap_index.location[:compress].should be_false
850
+ end
851
+
852
+ describe "in groups" do
853
+ it "should inherit the current compress setting" do
854
+ ls.compress = false
855
+ ls.group.compress.should be_false
856
+ end
857
+
858
+ it "should set the compress value" do
859
+ group = ls.group(:compress => false)
860
+ group.compress.should be_false
861
+ end
862
+ end
863
+ end
854
864
  end
@@ -41,43 +41,6 @@ describe "SitemapGenerator" do
41
41
  end
42
42
  end
43
43
 
44
- describe "generate sitemap with deprecated config" do
45
- before :all do
46
- SitemapGenerator::Sitemap.reset!
47
- clean_sitemap_files_from_rails_app
48
- copy_sitemap_file_to_rails_app(:deprecated)
49
- with_max_links(10) { execute_sitemap_config }
50
- end
51
-
52
- it "should create sitemaps" do
53
- file_should_exist(rails_path('public/sitemap_index.xml.gz'))
54
- file_should_exist(rails_path('public/sitemap1.xml.gz'))
55
- file_should_exist(rails_path('public/sitemap2.xml.gz'))
56
- file_should_not_exist(rails_path('public/sitemap3.xml.gz'))
57
- end
58
-
59
- it "should have 13 links" do
60
- SitemapGenerator::Sitemap.link_count.should == 13
61
- end
62
-
63
- it "index XML should validate" do
64
- gzipped_xml_file_should_validate_against_schema rails_path('public/sitemap_index.xml.gz'), 'siteindex'
65
- end
66
-
67
- it "sitemap XML should validate" do
68
- gzipped_xml_file_should_validate_against_schema rails_path('public/sitemap1.xml.gz'), 'sitemap'
69
- gzipped_xml_file_should_validate_against_schema rails_path('public/sitemap2.xml.gz'), 'sitemap'
70
- end
71
-
72
- it "index XML should not have excess whitespace" do
73
- gzipped_xml_file_should_have_minimal_whitespace rails_path('public/sitemap_index.xml.gz')
74
- end
75
-
76
- it "sitemap XML should not have excess whitespace" do
77
- gzipped_xml_file_should_have_minimal_whitespace rails_path('public/sitemap1.xml.gz')
78
- end
79
- end
80
-
81
44
  describe "generate sitemap with normal config" do
82
45
  before :all do
83
46
  SitemapGenerator::Sitemap.reset!
@@ -493,6 +456,99 @@ describe "SitemapGenerator" do
493
456
  end
494
457
  end
495
458
 
459
+ describe "compress" do
460
+ let(:ls) { SitemapGenerator::LinkSet.new(:default_host => 'http://test.local', :include_root => false) }
461
+
462
+ before :each do
463
+ clean_sitemap_files_from_rails_app
464
+ end
465
+
466
+ describe "when false" do
467
+ before :each do
468
+ ls.compress = false
469
+ end
470
+
471
+ it "should not compress files" do
472
+ with_max_links(1) do
473
+ ls.create do
474
+ add('/one')
475
+ add('/two')
476
+ group(:filename => :group) {
477
+ add('/group1')
478
+ add('/group2')
479
+ }
480
+ end
481
+ end
482
+ file_should_exist(rails_path('public/sitemap.xml'))
483
+ file_should_exist(rails_path('public/sitemap1.xml'))
484
+ file_should_exist(rails_path('public/group.xml'))
485
+ file_should_exist(rails_path('public/group1.xml'))
486
+ end
487
+ end
488
+
489
+ describe "when :all_but_first" do
490
+ before :each do
491
+ ls.compress = :all_but_first
492
+ end
493
+
494
+ it "should not compress first file" do
495
+ with_max_links(1) do
496
+ ls.create do
497
+ add('/one')
498
+ add('/two')
499
+ add('/three')
500
+ group(:filename => :group) {
501
+ add('/group1')
502
+ add('/group2')
503
+ }
504
+ group(:filename => :group2, :compress => true) {
505
+ add('/group1')
506
+ add('/group2')
507
+ }
508
+ group(:filename => :group2, :compress => false) {
509
+ add('/group1')
510
+ add('/group2')
511
+ }
512
+ end
513
+ end
514
+ file_should_exist(rails_path('public/sitemap.xml'))
515
+ file_should_exist(rails_path('public/sitemap1.xml.gz'))
516
+ file_should_exist(rails_path('public/sitemap2.xml.gz'))
517
+ file_should_exist(rails_path('public/group.xml'))
518
+ file_should_exist(rails_path('public/group1.xml.gz'))
519
+ file_should_exist(rails_path('public/group2.xml.gz'))
520
+ file_should_exist(rails_path('public/group21.xml.gz'))
521
+ end
522
+ end
523
+
524
+ describe "in groups" do
525
+ it "should respect passed in compress option" do
526
+ with_max_links(1) do
527
+ ls.create do
528
+ group(:filename => :group1, :compress => :all_but_first) {
529
+ add('/group1')
530
+ add('/group2')
531
+ }
532
+ group(:filename => :group2, :compress => true) {
533
+ add('/group1')
534
+ add('/group2')
535
+ }
536
+ group(:filename => :group3, :compress => false) {
537
+ add('/group1')
538
+ add('/group2')
539
+ }
540
+ end
541
+ end
542
+ file_should_exist(rails_path('public/group1.xml'))
543
+ file_should_exist(rails_path('public/group11.xml.gz'))
544
+ file_should_exist(rails_path('public/group2.xml.gz'))
545
+ file_should_exist(rails_path('public/group21.xml.gz'))
546
+ file_should_exist(rails_path('public/group3.xml'))
547
+ file_should_exist(rails_path('public/group31.xml'))
548
+ end
549
+ end
550
+ end
551
+
496
552
  protected
497
553
 
498
554
  #