pipe-run 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,12 +1,24 @@
1
1
  Pipe Run
2
- ===============
2
+ ========
3
3
 
4
4
  **Pipe Run** runs command and returns its standard output in one call:
5
5
 
6
6
  require "pipe-run"
7
- output = Pipe.run("date")
7
+ output = Pipe.run("date") # blocking
8
8
 
9
- puts output # will print out for example 'Thu Feb 17 17:22:18 CET 2011'
9
+ puts output # will print out for example 'Thu Feb 17 17:22:18 CET 2011'
10
+
11
+ ### Asynchronous Use
12
+
13
+ In case, `eventmachine` is available, non-blocking run of the command is
14
+ possible using `#run_nonblock` or `#run` with block given, defined in
15
+ `em-pipe-run` file. So for example:
16
+
17
+ require "em-pipe-run"
18
+
19
+ Pipe.run("date") do |output| # non-blocking
20
+ puts output # will print out for example 'Thu Feb 17 17:22:18 CET 2011'
21
+ end
10
22
 
11
23
  Contributing
12
24
  ------------
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ Jeweler::Tasks.new do |gem|
16
16
  gem.name = "pipe-run"
17
17
  gem.homepage = "https://github.com/martinkozak/pipe-run"
18
18
  gem.license = "MIT"
19
- gem.summary = 'Runs command and returns its standard output in one call.'
19
+ gem.summary = 'Runs command and returns its standard output in one call. Both synchronous and asynchronous (with eventmachine) running is supported.'
20
20
  gem.email = "martinkozak@martinkozak.net"
21
21
  gem.authors = ["Martin Kozák"]
22
22
  # Include your dependencies below. Runtime dependencies are required when using your gem,
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "eventmachine"
5
+ require "pipe-run"
6
+
7
+ ##
8
+ # Pipe class of the +pipe-run+. Currently with one static method only.
9
+ #
10
+
11
+ class Pipe
12
+
13
+ ##
14
+ # EventMachine connection to pipe.
15
+ # @since 0.2.0
16
+ #
17
+
18
+
19
+ class Receiver < EM::Connection
20
+
21
+ ##
22
+ # Holds pipe output buffer.
23
+ #
24
+
25
+ @buffer
26
+
27
+ ##
28
+ # Holds callback for giving back the results.
29
+ #
30
+
31
+ @callback
32
+
33
+ ##
34
+ # Constructor.
35
+ # @param [Proc] callback callback for giving back the results
36
+ #
37
+
38
+ def initialize(callback = nil)
39
+ @callback = callback
40
+ @buffer = ""
41
+ end
42
+
43
+ ##
44
+ # Receives data from pipe.
45
+ # @param [String] data output from pipe
46
+ #
47
+
48
+ def receive_data(data)
49
+ @buffer << data
50
+ end
51
+
52
+ ##
53
+ # Action after terminating the connection. Calls callback.
54
+ #
55
+
56
+ def unbind
57
+ if not @callback.nil?
58
+ @callback.call(@buffer)
59
+ end
60
+ end
61
+ end
62
+
63
+ ##
64
+ # Runs the command and returns its standard output.
65
+ # Blocking.
66
+ #
67
+ # @param [String] command command for run
68
+ # @param [Proc] block callback for giving back the results
69
+ # @since 0.2.0
70
+ #
71
+
72
+ def self.run_nonblock(command, &block)
73
+ pipe = File.popen(command, "r")
74
+ EM::attach(pipe, Receiver, block)
75
+ end
76
+
77
+ end
data/lib/pipe-run.rb CHANGED
@@ -9,13 +9,22 @@ class Pipe
9
9
 
10
10
  ##
11
11
  # Runs the command and returns its standard output.
12
- # Blocking.
12
+ #
13
+ # If block is given, treat call as non-blocking. In that case,
14
+ # +em-pipe-run+ file must be loaded.
13
15
  #
14
16
  # @param [String] command command for run
17
+ # @param [Proc] block block for giving back the results
15
18
  # @return [String] command output
16
19
  #
17
20
 
18
- def self.run(command)
21
+ def self.run(command, &block)
22
+ if not block.nil?
23
+ return self.run_nonblock(command, &block)
24
+ end
25
+
26
+ ###
27
+
19
28
  pipe = File.popen(command, "r")
20
29
 
21
30
  result = pipe.read
data/pipe-run.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pipe-run}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Martin Kozák"]
12
- s.date = %q{2011-02-17}
12
+ s.date = %q{2011-02-22}
13
13
  s.email = %q{martinkozak@martinkozak.net}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  "README.md",
24
24
  "Rakefile",
25
25
  "VERSION",
26
+ "lib/em-pipe-run.rb",
26
27
  "lib/pipe-run.rb",
27
28
  "pipe-run.gemspec"
28
29
  ]
@@ -30,7 +31,7 @@ Gem::Specification.new do |s|
30
31
  s.licenses = ["MIT"]
31
32
  s.require_paths = ["lib"]
32
33
  s.rubygems_version = %q{1.5.2}
33
- s.summary = %q{Runs command and returns its standard output in one call.}
34
+ s.summary = %q{Runs command and returns its standard output in one call. Both synchronous and asynchronous (with eventmachine) running is supported.}
34
35
 
35
36
  if s.respond_to? :specification_version then
36
37
  s.specification_version = 3
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: pipe-run
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Martin Koz\xC3\xA1k"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-17 00:00:00 +01:00
13
+ date: 2011-02-22 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -52,6 +52,7 @@ files:
52
52
  - README.md
53
53
  - Rakefile
54
54
  - VERSION
55
+ - lib/em-pipe-run.rb
55
56
  - lib/pipe-run.rb
56
57
  - pipe-run.gemspec
57
58
  has_rdoc: true
@@ -68,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
69
  requirements:
69
70
  - - ">="
70
71
  - !ruby/object:Gem::Version
71
- hash: 3787895221996818874
72
+ hash: -1865702309634779703
72
73
  segments:
73
74
  - 0
74
75
  version: "0"
@@ -84,6 +85,6 @@ rubyforge_project:
84
85
  rubygems_version: 1.5.2
85
86
  signing_key:
86
87
  specification_version: 3
87
- summary: Runs command and returns its standard output in one call.
88
+ summary: Runs command and returns its standard output in one call. Both synchronous and asynchronous (with eventmachine) running is supported.
88
89
  test_files: []
89
90