massive_sitemap 2.0.0.rc3 → 2.0.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -7,6 +7,7 @@
7
7
  * updated Docu
8
8
  * switch to writer chain
9
9
  * add BigSitemap API
10
+ * FileManifest -> read all files into streams take that as reference
10
11
 
11
12
  ## v2.0.0 - 13-02-2012
12
13
  _inital release_
@@ -26,3 +27,4 @@
26
27
  * manifest handling:
27
28
  * moved Amazon S3 integration to [massive_sitemap-writer-s3](https://github.com/rngtng/massive_sitemap-writer-s3)
28
29
  * updated/fixed Ping
30
+ * move LockingFile into MassiveSitemap scope
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0.rc3
1
+ 2.0.0.rc4
@@ -11,10 +11,10 @@ module MassiveSitemap
11
11
  def initialize(writer, options = {}, &block)
12
12
  super(writer, options) do
13
13
  writer.each do |path, last_modified|
14
+ next if writer.current && path.include?(writer.current)
14
15
  add path, :last_modified => last_modified
15
16
  end
16
17
  end
17
-
18
18
  end
19
19
 
20
20
  def self.generate(writer, options = {}, &block)
@@ -22,7 +22,7 @@ module MassiveSitemap
22
22
  end
23
23
 
24
24
  def self.index_url(builder, writer)
25
- ::File.join(builder.send(:url), writer.options[:filename])
25
+ ::File.join(builder.send(:url), writer.current)
26
26
  end
27
27
 
28
28
  def add_url!(location, attrs = {})
@@ -7,17 +7,13 @@ module MassiveSitemap
7
7
  NUM_URLS = 1..50_000
8
8
 
9
9
  OPTS = Base::OPTS.merge(
10
- :max_per_sitemap => NUM_URLS.max
10
+ :max_urls => NUM_URLS.max
11
11
  )
12
12
 
13
13
  def initialize(writer, options = {}, &block)
14
14
  @rotations = 0
15
15
  @urls = 0
16
16
 
17
- if options[:max_per_sitemap] && !NUM_URLS.member?(options[:max_per_sitemap])
18
- raise ArgumentError, %Q(":max_per_sitemap" must be greater than #{NUM_URLS.min} and smaller than #{NUM_URLS.max})
19
- end
20
-
21
17
  super
22
18
  end
23
19
 
@@ -34,7 +30,7 @@ module MassiveSitemap
34
30
  end
35
31
 
36
32
  def add_url!(location, attrs = {})
37
- if @urls >= @options[:max_per_sitemap]
33
+ if @urls >= @options[:max_urls]
38
34
  close!
39
35
  end
40
36
  super
@@ -0,0 +1,20 @@
1
+ require 'fileutils'
2
+
3
+ # Create Lock
4
+
5
+ module MassiveSitemap
6
+ LOCK_FILE = 'generator.lock'
7
+
8
+ def lock!(&block)
9
+ if block
10
+ raise Errno::EACCES if ::File.exists?(LOCK_FILE)
11
+ ::File.open(LOCK_FILE, 'w', ::File::EXCL)
12
+ begin
13
+ block.call
14
+ ensure
15
+ FileUtils.rm(LOCK_FILE) #unlock!
16
+ end
17
+ end
18
+ end
19
+ module_function :lock!
20
+ end
@@ -11,22 +11,6 @@ module MassiveSitemap
11
11
  @stream = nil
12
12
  end
13
13
 
14
- # Interface
15
- def open_stream
16
- @string ||= StringIO.new
17
- end
18
-
19
- def close_stream(stream)
20
- end
21
-
22
- def init?
23
- true
24
- end
25
-
26
- def streams
27
- []
28
- end
29
-
30
14
  # API
31
15
  def init!(options = {})
32
16
  close!
@@ -54,6 +38,35 @@ module MassiveSitemap
54
38
  def each(&block)
55
39
  streams.each(&block)
56
40
  end
41
+
42
+ def current
43
+ stream
44
+ end
45
+
46
+ # def flush!
47
+ # @streams = []
48
+ # end
49
+
50
+ # Interface
51
+ protected
52
+ def open_stream
53
+ @string ||= StringIO.new
54
+ end
55
+
56
+ def close_stream(stream)
57
+ end
58
+
59
+ def init?
60
+ true
61
+ end
62
+
63
+ def streams
64
+ @streams ||= []
65
+ end
66
+
67
+ def stream
68
+ nil
69
+ end
57
70
  end
58
71
 
59
72
  end
@@ -13,9 +13,9 @@ module MassiveSitemap
13
13
  :root => '.',
14
14
  :force_overwrite => false,
15
15
  :filename => "sitemap.xml",
16
- :index_filename => "sitemap_index.xml",
17
16
  )
18
17
 
18
+ protected
19
19
  def open_stream
20
20
  ::File.dirname(tmp_filename).tap do |dir|
21
21
  FileUtils.mkdir_p(dir) unless ::File.exists?(dir)
@@ -39,11 +39,14 @@ module MassiveSitemap
39
39
 
40
40
  def streams
41
41
  files.map do |path|
42
- next if path.include?(options[:index_filename])
43
42
  [::File.basename(path), ::File.stat(path).mtime]
44
43
  end.compact
45
44
  end
46
45
 
46
+ def stream
47
+ options[:filename]
48
+ end
49
+
47
50
  private
48
51
  def filename
49
52
  ::File.join options[:root], options[:filename]
@@ -9,6 +9,7 @@ module MassiveSitemap
9
9
  class GzipFile < File
10
10
  OPTS = File::OPTS
11
11
 
12
+ protected
12
13
  def open_stream
13
14
  ::Zlib::GzipWriter.new(super)
14
15
  end
@@ -7,11 +7,7 @@ module MassiveSitemap
7
7
  module Writer
8
8
 
9
9
  class String < Base
10
-
11
- def open_stream
12
- @string ||= StringIO.new
13
- end
14
-
10
+ # accessors to conent, mainly used in tests
15
11
  def to_s
16
12
  @string.string rescue ""
17
13
  end
@@ -23,6 +19,11 @@ module MassiveSitemap
23
19
  def include?(other_string)
24
20
  to_s.include?(other_string)
25
21
  end
22
+
23
+ protected
24
+ def open_stream
25
+ @string ||= StringIO.new
26
+ end
26
27
  end
27
28
 
28
29
  end
@@ -2,6 +2,7 @@ require 'massive_sitemap/writer/file'
2
2
  require 'massive_sitemap/writer/gzip_file'
3
3
  require 'massive_sitemap/builder/rotating'
4
4
  require 'massive_sitemap/builder/index'
5
+ require 'massive_sitemap/lock'
5
6
  require 'massive_sitemap/ping'
6
7
 
7
8
  # Page at -> <url>
@@ -32,20 +33,26 @@ module MassiveSitemap
32
33
  }
33
34
 
34
35
  def generate(options = {}, &block)
35
- @options = DEFAULTS.merge options
36
+ lock! do
37
+ @options = DEFAULTS.merge options
36
38
 
37
- unless @options[:url]
38
- raise ArgumentError, 'you must specify ":url" string'
39
- end
40
- @options[:index_url] ||= @options[:url]
39
+ unless @options[:url]
40
+ raise ArgumentError, 'you must specify ":url" string'
41
+ end
42
+ @options[:index_url] ||= @options[:url]
43
+
44
+ if @options[:max_urls] && !Builder::Rotating::NUM_URLS.member?(@options[:max_urls])
45
+ raise ArgumentError, %Q(":max_urls" must be greater than #{NUM_URLS.min} and smaller than #{NUM_URLS.max})
46
+ end
41
47
 
42
- @options[:writer] = Writer::GzipFile if @options[:gzip]
48
+ @options[:writer] = Writer::GzipFile if @options[:gzip]
43
49
 
44
- @writer = @options[:writer].new @options
45
- Builder::Rotating.generate(@writer, @options, &block)
50
+ @writer = @options[:writer].new @options
51
+ Builder::Rotating.generate(@writer, @options, &block)
46
52
 
47
- @writer.init!(:filename => @options[:index_filename], :force_overwrite => true)
48
- Builder::Index.generate(@writer, @options.merge(:url => @options[:index_url]))
53
+ @writer.init!(:filename => @options[:index_filename], :force_overwrite => true)
54
+ Builder::Index.generate(@writer, @options.merge(:url => @options[:index_url]))
55
+ end
49
56
  end
50
57
  module_function :generate
51
58
 
@@ -23,8 +23,16 @@ describe MassiveSitemap::Builder::Index do
23
23
  writer.should include("<loc>http://test.de/test</loc>")
24
24
  end
25
25
 
26
- it 'returns index_url' do
27
- writer = MassiveSitemap::Writer::File.new(:filename => "sitemap_index.xml")
28
- MassiveSitemap::Builder::Index.generate(writer, :url => "test.de").should == "http://test.de/sitemap_index.xml"
26
+ context "with file writer" do
27
+ let(:index_filename) { "sitemap_index.xml" }
28
+ let(:writer) { MassiveSitemap::Writer::File.new(:filename => index_filename) }
29
+
30
+ after do
31
+ FileUtils.rm(index_filename)
32
+ end
33
+
34
+ it 'returns index_url' do
35
+ MassiveSitemap::Builder::Index.generate(writer, :url => "test.de").should == "http://test.de/sitemap_index.xml"
36
+ end
29
37
  end
30
38
  end
@@ -8,12 +8,6 @@ describe MassiveSitemap::Builder::Rotating do
8
8
  let(:writer) { MassiveSitemap::Writer::String.new }
9
9
  let(:builder) { MassiveSitemap::Builder::Rotating.new(writer) }
10
10
 
11
- it 'raises error when max_per_sitemap > MAX_URLS' do
12
- expect do
13
- MassiveSitemap::Builder::Rotating.new(writer, :max_per_sitemap => MassiveSitemap::Builder::Rotating::NUM_URLS.max + 1)
14
- end.to raise_error(ArgumentError)
15
- end
16
-
17
11
  it 'generates one url' do
18
12
  MassiveSitemap::Builder::Rotating.new(writer) do
19
13
  add_url! 'test'
@@ -22,7 +16,7 @@ describe MassiveSitemap::Builder::Rotating do
22
16
  end
23
17
 
24
18
  it 'generates two url' do
25
- MassiveSitemap::Builder::Rotating.new(writer, :max_per_sitemap => 1) do
19
+ MassiveSitemap::Builder::Rotating.new(writer, :max_urls => 1) do
26
20
  add_url! 'test'
27
21
  add_url! 'test2'
28
22
  end
@@ -42,7 +36,7 @@ describe MassiveSitemap::Builder::Rotating do
42
36
  it 'generates two url' do
43
37
  expect do
44
38
  expect do
45
- MassiveSitemap::Builder::Rotating.new(writer, :max_per_sitemap => 1) do
39
+ MassiveSitemap::Builder::Rotating.new(writer, :max_urls => 1) do
46
40
  add 'test'
47
41
  add 'test2'
48
42
  end
@@ -54,7 +48,7 @@ describe MassiveSitemap::Builder::Rotating do
54
48
  File.open(filename, 'w') {}
55
49
  expect do
56
50
  expect do
57
- MassiveSitemap::Builder::Rotating.new(writer, :max_per_sitemap => 1) do
51
+ MassiveSitemap::Builder::Rotating.new(writer, :max_urls => 1) do
58
52
  begin
59
53
  add 'test'
60
54
  rescue MassiveSitemap::Writer::File::FileExistsException => e
data/spec/lock_spec.rb ADDED
@@ -0,0 +1,46 @@
1
+ require "massive_sitemap/lock"
2
+
3
+ describe MassiveSitemap do
4
+ describe "lock!" do
5
+ let(:lock_file) { MassiveSitemap::LOCK_FILE }
6
+
7
+ after do
8
+ FileUtils.rm(lock_file) rescue nil
9
+ end
10
+
11
+ it 'does nothing without block' do
12
+ MassiveSitemap.lock!
13
+ ::File.exists?(lock_file).should be_false
14
+ end
15
+
16
+ it 'creates lockfile' do
17
+ File.exists?(lock_file).should be_false
18
+ MassiveSitemap.lock! do
19
+ ::File.exists?(lock_file).should be_true
20
+ end
21
+ end
22
+
23
+ it 'deletes lockfile' do
24
+ MassiveSitemap.lock! {}
25
+ ::File.exists?(lock_file).should be_false
26
+ end
27
+
28
+ it 'deletes lockfile in case of error' do
29
+ expect do
30
+ MassiveSitemap.lock! do
31
+ raise ArgumentError
32
+ end
33
+ end.to raise_error
34
+ ::File.exists?(lock_file).should be_false
35
+ end
36
+
37
+ it 'fails if lockfile exists' do
38
+ ::File.open(lock_file, 'w',) {}
39
+ expect do
40
+ MassiveSitemap.lock! do
41
+ puts "Hi"
42
+ end
43
+ end.to raise_error
44
+ end
45
+ end
46
+ end
@@ -29,6 +29,12 @@ describe MassiveSitemap do
29
29
  end.to raise_error(ArgumentError)
30
30
  end
31
31
 
32
+ it 'raises error when max_urls > MAX_URLS' do
33
+ expect do
34
+ MassiveSitemap.generate(:max_urls => MassiveSitemap::Builder::Rotating::NUM_URLS.max + 1)
35
+ end.to raise_error(ArgumentError)
36
+ end
37
+
32
38
  it "does not create empty sitemap file" do
33
39
  expect do
34
40
  MassiveSitemap.generate(:url => 'test.de/')
@@ -47,13 +47,13 @@ describe MassiveSitemap::Writer::File do
47
47
  end
48
48
  end
49
49
 
50
- it 'create file' do
50
+ it 'creates file' do
51
51
  expect do
52
52
  writer.close!
53
53
  end.to change { File.exists?(filename) }.to(true)
54
54
  end
55
55
 
56
- it 'create second file on rotation' do
56
+ it 'creates second file on rotation' do
57
57
  expect do
58
58
  expect do
59
59
  writer.close!
@@ -63,19 +63,19 @@ describe MassiveSitemap::Writer::File do
63
63
  end.to change { File.exists?(filename2) }.to(true)
64
64
  end
65
65
 
66
- it 'write into file' do
66
+ it 'writes into file' do
67
67
  writer.print 'test'
68
68
  writer.close!
69
69
  `cat '#{filename}'`.should == "test"
70
70
  end
71
71
 
72
- it 'init new file closes current' do
72
+ it 'inits new file closes current' do
73
73
  writer.print 'test'
74
74
  writer.init!(:filename => filename2)
75
75
  `cat '#{filename}'`.should == "test"
76
76
  end
77
77
 
78
- it 'write into second file' do
78
+ it 'writes into second file' do
79
79
  writer.print 'test'
80
80
  writer.init!(:filename => filename2)
81
81
  writer.print 'test2'
@@ -83,15 +83,11 @@ describe MassiveSitemap::Writer::File do
83
83
  `cat '#{filename2}'`.should == "test2"
84
84
  end
85
85
 
86
- context "opening write file" do
86
+ context "file exists" do
87
87
  before do
88
88
  File.open(filename, 'w') {}
89
89
  end
90
90
 
91
- after do
92
- FileUtils.rm(filename) rescue nil
93
- end
94
-
95
91
  it 'raises when file exits' do
96
92
  writer = MassiveSitemap::Writer::File.new
97
93
  expect do
@@ -99,10 +95,11 @@ describe MassiveSitemap::Writer::File do
99
95
  end.to raise_error(MassiveSitemap::Writer::File::FileExistsException)
100
96
  end
101
97
 
102
- it 'raises when file exits' do
98
+ it "dosn't raise when overwrite set" do
103
99
  writer = MassiveSitemap::Writer::File.new(:force_overwrite => true)
104
100
  expect do
105
101
  writer.init!
102
+ writer.close!
106
103
  end.to_not raise_error(MassiveSitemap::Writer::File::FileExistsException)
107
104
  end
108
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: massive_sitemap
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.rc3
4
+ version: 2.0.0.rc4
5
5
  prerelease: 6
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: 2012-02-12 00:00:00.000000000Z
12
+ date: 2012-02-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70334535721800 !ruby/object:Gem::Requirement
16
+ requirement: &70276344811920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70334535721800
24
+ version_requirements: *70276344811920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70334535721340 !ruby/object:Gem::Requirement
27
+ requirement: &70276340400560 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70334535721340
35
+ version_requirements: *70276340400560
36
36
  description: MassiveSitemap - build huge sitemaps painfree. Differential updates keeps
37
37
  generation time short and reduces load on DB. It's heavealy inspired by BigSitemaps
38
38
  and offers compatiable API
@@ -55,22 +55,22 @@ files:
55
55
  - lib/massive_sitemap/builder/base.rb
56
56
  - lib/massive_sitemap/builder/index.rb
57
57
  - lib/massive_sitemap/builder/rotating.rb
58
+ - lib/massive_sitemap/lock.rb
58
59
  - lib/massive_sitemap/ping.rb
59
60
  - lib/massive_sitemap/writer/base.rb
60
61
  - lib/massive_sitemap/writer/file.rb
61
62
  - lib/massive_sitemap/writer/gzip_file.rb
62
- - lib/massive_sitemap/writer/locking_file.rb
63
63
  - lib/massive_sitemap/writer/string.rb
64
64
  - massive_sitemap.gemspec
65
65
  - spec/builder/base_spec.rb
66
66
  - spec/builder/index_spec.rb
67
67
  - spec/builder/rotating_spec.rb
68
+ - spec/lock_spec.rb
68
69
  - spec/massive_sitemap_spec.rb
69
70
  - spec/ping_spec.rb
70
71
  - spec/spec_helper.rb
71
72
  - spec/writer/file_spec.rb
72
73
  - spec/writer/gzip_file_spec.rb
73
- - spec/writer/locking_file_spec.rb
74
74
  homepage: http://github.com/rngtng/massive_sitemap
75
75
  licenses: []
76
76
  post_install_message:
@@ -99,9 +99,9 @@ test_files:
99
99
  - spec/builder/base_spec.rb
100
100
  - spec/builder/index_spec.rb
101
101
  - spec/builder/rotating_spec.rb
102
+ - spec/lock_spec.rb
102
103
  - spec/massive_sitemap_spec.rb
103
104
  - spec/ping_spec.rb
104
105
  - spec/spec_helper.rb
105
106
  - spec/writer/file_spec.rb
106
107
  - spec/writer/gzip_file_spec.rb
107
- - spec/writer/locking_file_spec.rb
@@ -1,33 +0,0 @@
1
- require 'zlib'
2
-
3
- require "massive_sitemap/writer/file"
4
- # Create Lock before writing to file
5
-
6
- module MassiveSitemap
7
- module Writer
8
-
9
- class LockingFile < File
10
- OPTS = File::OPTS
11
-
12
- LOCK_FILE = 'generator.lock'
13
-
14
- def open_stream
15
- ::File.open(LOCK_FILE, 'w', ::File::EXCL) #lock!
16
- super
17
- end
18
-
19
- def close_stream(stream)
20
- super
21
- FileUtils.rm(LOCK_FILE) #unlock!
22
- end
23
-
24
- def init?
25
- if ::File.exists?(LOCK_FILE)
26
- raise Errno::EACCES
27
- end
28
- super
29
- end
30
- end
31
-
32
- end
33
- end
@@ -1,34 +0,0 @@
1
- require "massive_sitemap/writer/locking_file"
2
-
3
- describe MassiveSitemap::Writer::LockingFile do
4
- let(:filename) { 'sitemap.xml' }
5
- let(:tmp_filename) { "#{filename}.tmp" }
6
- let(:lock_file) { MassiveSitemap::Writer::LockingFile::LOCK_FILE }
7
- let(:writer) { MassiveSitemap::Writer::LockingFile.new.tap { |w| w.init! } }
8
-
9
- after do
10
- FileUtils.rm(filename) rescue nil
11
- FileUtils.rm(tmp_filename) rescue nil
12
- FileUtils.rm(lock_file) rescue nil
13
- end
14
-
15
- it 'creates lockfile' do
16
- expect do
17
- writer
18
- end.to change { File.exists?(lock_file) }.to(true)
19
- end
20
-
21
- it 'deletes lockfile' do
22
- writer
23
- expect do
24
- writer.close!
25
- end.to change { File.exists?(lock_file) }.to(false)
26
- end
27
-
28
- it 'fails if lockfile exists' do
29
- File.open(lock_file, 'w') {}
30
- expect do
31
- writer
32
- end.to raise_error
33
- end
34
- end