em-zipper 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/examples/io.rb CHANGED
@@ -40,7 +40,7 @@ EM.run do
40
40
 
41
41
  # remote to IO to file
42
42
  #
43
- sites = %w(http://www.google.com/ https://www.github.com/)
43
+ sites = %w(http://s3.amazonaws.com/ping-em-assets/sample1.txt http://s3.amazonaws.com/ping-em-assets/sample2.txt)
44
44
  rzip = EM::Zipper.new sites, StringIO.new
45
45
  rzip.callback do |io|
46
46
  write_io_to_file(io, 'io-remote-non-blocking.zip')
@@ -33,42 +33,44 @@ module EventMachine
33
33
  end
34
34
 
35
35
  def next_entry_non_block
36
- proc do |file_path|
37
- new_entry = ::Zip::ZipEntry.new '-', file_name(file_path)
38
- #, "", "", file.size, 0, Zip::ZipEntry::STORED, file.size
39
- zos.put_next_entry(new_entry)
40
- EM.next_tick { write_entry_non_block file_path }
41
- end
42
- end
36
+ proc do |file_path|
37
+ cqueue = EM::Queue.new
38
+ chunky = proc do |chunk|
39
+ zos << chunk
40
+
41
+ if cqueue.empty?
42
+ close_zos and return if fqueue.empty?
43
+ EM.next_tick { fqueue.pop(&next_entry_non_block) }
44
+ else
45
+ EM.next_tick { cqueue.pop(&chunky) }
46
+ end
47
+ end
43
48
 
44
- def write_entry_non_block(file_path)
45
- cqueue = EM::Queue.new
46
- chunky = proc do |chunk|
47
- zos << chunk
49
+ defer = EM::DefaultDeferrable.new
50
+ defer.callback do
51
+ # begin the entry when we have all the data
52
+ new_entry = ::Zip::ZipEntry.new '-', file_name(file_path)
53
+ zos.put_next_entry(new_entry)
48
54
 
49
- if cqueue.empty?
50
- close_zos and return if fqueue.empty?
51
- EM.next_tick { fqueue.pop(&next_entry_non_block) }
52
- else
53
- EM.next_tick { cqueue.pop(&chunky) }
55
+ EM.next_tick { cqueue.pop(&chunky) } if cqueue.size > 0
54
56
  end
55
- end
56
-
57
- defer = EM::DefaultDeferrable.new
58
- defer.callback { cqueue.pop(&chunky) if cqueue.size > 0 }
57
+ defer.errback { puts 'error' }
59
58
 
60
- _chunk_method = file_path.match(/\Ahttps?:\/\//) ? :chunk_http : :chunk_file
61
- __send__ _chunk_method, file_path, cqueue, defer
59
+ _chunk_method = file_path.match(/\Ahttps?:\/\//) ? :chunk_http : :chunk_file
60
+ __send__ _chunk_method, file_path, cqueue, defer
61
+ end
62
62
  end
63
63
 
64
64
  def chunk_file(file_path, q, d)
65
65
  file = File.open file_path
66
66
 
67
67
  fr = proc do
68
- q.push file.read(16384)
69
-
70
- if file.eof? then EM.next_tick { d.succeed }
71
- else EM.next_tick(&fr)
68
+ begin
69
+ q.push file.read_nonblock(16384)
70
+ EM.next_tick(&fr)
71
+ rescue EOFError
72
+ file.close
73
+ EM.next_tick { d.succeed }
72
74
  end
73
75
  end
74
76
 
@@ -77,9 +79,9 @@ module EventMachine
77
79
 
78
80
  def chunk_http(uri, q, d)
79
81
  http = EventMachine::HttpRequest.new(uri).get
80
- http.errback { puts 'error' }
81
- http.stream { |chunk| q.push chunk }
82
+ http.errback { EM.next_tick { d.fail } }
82
83
  http.callback { EM.next_tick { d.succeed } }
84
+ http.stream { |chunk| q.push chunk }
83
85
  end
84
86
  end
85
87
  end
@@ -1,5 +1,5 @@
1
1
  module EventMachine
2
2
  module Zipper
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-zipper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yung Hwa Kwon
@@ -63,6 +63,7 @@ files:
63
63
  - .ruby-version
64
64
  - Gemfile
65
65
  - Gemfile.lock
66
+ - benchmarkz/em-zipper.rb
66
67
  - em-zipper.gemspec
67
68
  - examples/async_sinatra.md
68
69
  - examples/file.rb