em-zipper 0.0.1 → 0.0.2

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
@@ -1,41 +1,54 @@
1
1
  $LOAD_PATH << './lib'
2
2
  require './lib/em-zipper'
3
3
 
4
+ def write_io_to_file(io, file_name)
5
+ q = EM::Queue.new
6
+ file = File.new(file_name, 'w')
7
+
8
+ wf = proc do |chunk|
9
+ file.write chunk
10
+ if q.empty?
11
+ file.close
12
+ else
13
+ EM.next_tick { q.pop(&wf) }
14
+ end
15
+ end
16
+
17
+ io.rewind
18
+ while buffer = io.read(16348)
19
+ q.push buffer
20
+ end
21
+ q.pop(&wf)
22
+ end
23
+
24
+
4
25
  EM.run do
5
26
  EM.add_periodic_timer(1) { puts 'tick' } # tick tock
6
27
 
28
+ # file to IO to file
29
+ #
7
30
  files = %w(sample1.txt sample2.txt).map do |file_name|
8
31
  File.join(File.expand_path("./examples/files"), file_name)
9
32
  end
10
33
 
11
- # zip to IO
12
- #
13
- buff = StringIO.new
14
-
15
- zippy = EM::Zipper.new files, buff
16
- zippy.zip!
17
- zippy.callback do |io|
18
-
19
- # Write it out to a file
20
- #
21
- q = EM::Queue.new
22
- file = File.new('io-non-blocking.zip', 'w')
23
-
24
- wf = proc do |chunk|
25
- file.write chunk
26
- if q.empty?
27
- file.close
28
- EM.stop
29
- else
30
- EM.next_tick { q.pop(&wf) }
31
- end
32
- end
34
+ fzip = EM::Zipper.new files, StringIO.new
35
+ fzip.callback do |io|
36
+ write_io_to_file(io, 'io-file-non-blocking.zip')
37
+ puts 'IO zipped from files'
38
+ end
33
39
 
34
- io.rewind
35
- while buffer = io.read(16348)
36
- q.push buffer
37
- end
38
- q.pop(&wf)
40
+
41
+ # remote to IO to file
42
+ #
43
+ sites = %w(http://www.google.com/ https://www.github.com/)
44
+ rzip = EM::Zipper.new sites, StringIO.new
45
+ rzip.callback do |io|
46
+ write_io_to_file(io, 'io-remote-non-blocking.zip')
47
+ puts 'IO zipped from remotes'
39
48
  end
49
+
50
+
51
+ fzip.zip!
52
+ rzip.zip!
40
53
  end
41
54
 
@@ -13,7 +13,7 @@ module EventMachine
13
13
  @fqueue = EM::Queue.new
14
14
  end
15
15
 
16
- def zip!(zip_name=nil)
16
+ def zip!
17
17
  files.each { |file_path| fqueue.push file_path }
18
18
  fqueue.pop(&next_entry_non_block)
19
19
 
@@ -28,7 +28,7 @@ module EventMachine
28
28
  end
29
29
 
30
30
  def close_zos
31
- # zos.close
31
+ zos.close if zos.io.is_a?(File) # close the io if it's a file
32
32
  succeed zos.close_buffer # return the zos
33
33
  end
34
34
 
@@ -54,18 +54,32 @@ module EventMachine
54
54
  end
55
55
  end
56
56
 
57
- # TODO http requests
58
- file_for_non_block file_path, cqueue
57
+ defer = EM::DefaultDeferrable.new
58
+ defer.callback { cqueue.pop(&chunky) if cqueue.size > 0 }
59
59
 
60
- # begin write
61
- cqueue.pop(&chunky) if cqueue.size > 0
60
+ _chunk_method = file_path.match(/\Ahttps?:\/\//) ? :chunk_http : :chunk_file
61
+ __send__ _chunk_method, file_path, cqueue, defer
62
62
  end
63
63
 
64
- def file_for_non_block(file_path, q)
64
+ def chunk_file(file_path, q, d)
65
65
  file = File.open file_path
66
- while buffer = file.read(16384)
67
- q.push buffer
66
+
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)
72
+ end
68
73
  end
74
+
75
+ EM.next_tick(&fr)
76
+ end
77
+
78
+ def chunk_http(uri, q, d)
79
+ http = EventMachine::HttpRequest.new(uri).get
80
+ http.errback { puts 'error' }
81
+ http.stream { |chunk| q.push chunk }
82
+ http.callback { EM.next_tick { d.succeed } }
69
83
  end
70
84
  end
71
85
  end
@@ -0,0 +1,5 @@
1
+ module EventMachine
2
+ module Zipper
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -10,6 +10,10 @@ module EventMachine
10
10
  @outputStream = io
11
11
  end
12
12
 
13
+ def io
14
+ @outputStream
15
+ end
16
+
13
17
  def update_local_headers
14
18
  nil
15
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-zipper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yung Hwa Kwon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-15 00:00:00.000000000 Z
11
+ date: 2013-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -64,6 +64,7 @@ files:
64
64
  - Gemfile
65
65
  - Gemfile.lock
66
66
  - em-zipper.gemspec
67
+ - examples/async_sinatra.md
67
68
  - examples/file.rb
68
69
  - examples/files/sample1.txt
69
70
  - examples/files/sample2.txt
@@ -71,6 +72,7 @@ files:
71
72
  - lib/em-zipper.rb
72
73
  - lib/em-zipper/base.rb
73
74
  - lib/em-zipper/stream_io.rb
75
+ - lib/em-zipper/version.rb
74
76
  - lib/em-zipper/zip_output_stream.rb
75
77
  homepage: http://github.com/nowk/em-zipper
76
78
  licenses: