metaphor 0.2.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +16 -20
- data/lib/metaphor.rb +13 -14
- data/lib/metaphor/processor/detour.rb +2 -2
- data/lib/metaphor/processor/json_processor.rb +4 -2
- data/lib/metaphor/processor/print_message.rb +3 -8
- data/lib/metaphor/processor/wiretap.rb +3 -3
- metadata +5 -10
data/README.markdown
CHANGED
@@ -25,46 +25,42 @@ Programming with Metaphor
|
|
25
25
|
metaphor.processors << Metaphor::Processor::PrintMessage.new
|
26
26
|
|
27
27
|
# Process one message
|
28
|
-
metaphor.call(
|
29
|
-
|
28
|
+
metaphor.call("Hello, Metaphor!") # => "Hello, Metaphor!"
|
29
|
+
# => false (if halted by processor)
|
30
30
|
|
31
31
|
# Process messages from this class until the Ruby VM is killed or the
|
32
32
|
# input returns nil
|
33
33
|
metaphor.call(StdinInput.new)
|
34
34
|
|
35
35
|
|
36
|
-
Classes used for input must respond to #
|
37
|
-
|
36
|
+
Classes used for input must respond to #gets and return a String (or at
|
37
|
+
least something that quacks like a String):
|
38
38
|
|
39
39
|
class StdinInput
|
40
|
-
def
|
41
|
-
|
42
|
-
|
43
|
-
{ "header" => "value", ... }, # Message headers
|
44
|
-
"body" # Message body
|
45
|
-
]
|
40
|
+
def gets
|
41
|
+
line = STDIN.gets
|
42
|
+
line == "" ? nil : line
|
46
43
|
end
|
47
44
|
end
|
48
45
|
|
49
46
|
Classes used as processors must respond to #call(headers, body):
|
50
47
|
|
51
48
|
class PrintMessage
|
52
|
-
def call(
|
53
|
-
puts "
|
54
|
-
puts "Body : #{body.inspect}"
|
49
|
+
def call(message)
|
50
|
+
puts "Message : #{message.inspect}"
|
55
51
|
end
|
56
52
|
end
|
57
53
|
|
58
54
|
The return value of #call controls what happens to the message. If the
|
59
55
|
processor returns:
|
60
56
|
|
61
|
-
*
|
62
|
-
- they are passed to the next processor
|
63
|
-
* Boolean false (or something that is === to it)
|
57
|
+
* false
|
64
58
|
- Metaphor stops processing this message and discards it
|
65
|
-
*
|
66
|
-
- Metaphor passes the same
|
67
|
-
|
59
|
+
* nil
|
60
|
+
- Metaphor passes the same object that were passed into the processor to the
|
61
|
+
next processor in the chain
|
62
|
+
* Anything else
|
63
|
+
- Metaphor passes this to the next processor
|
68
64
|
|
69
65
|
Contributing
|
70
66
|
------------
|
@@ -89,4 +85,4 @@ Authors
|
|
89
85
|
License
|
90
86
|
-------
|
91
87
|
|
92
|
-
Released under the MIT licence. See the LICENSE file for details.
|
88
|
+
Released under the MIT licence. See the LICENSE file for details.
|
data/lib/metaphor.rb
CHANGED
@@ -1,33 +1,32 @@
|
|
1
1
|
class Metaphor
|
2
|
-
VERSION = '0.
|
2
|
+
VERSION = '1.0.0'
|
3
3
|
attr_accessor :processors
|
4
4
|
|
5
5
|
def initialize
|
6
6
|
self.processors = []
|
7
7
|
end
|
8
8
|
|
9
|
-
def call(
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
process_message(*message)
|
9
|
+
def call(input)
|
10
|
+
if input.respond_to?(:gets)
|
11
|
+
while message = input.gets
|
12
|
+
process_message message
|
14
13
|
end
|
15
|
-
|
16
|
-
process_message
|
14
|
+
else
|
15
|
+
process_message input
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
19
|
private
|
21
|
-
def process_message
|
20
|
+
def process_message message
|
22
21
|
processors.each do |processor|
|
23
|
-
processor_output = processor.call(
|
22
|
+
processor_output = processor.call(message)
|
24
23
|
case
|
25
24
|
when processor_output === false
|
26
25
|
return false
|
27
|
-
when processor_output
|
28
|
-
|
26
|
+
when processor_output != nil
|
27
|
+
message = processor_output
|
29
28
|
end
|
30
29
|
end
|
31
|
-
|
30
|
+
message
|
32
31
|
end
|
33
|
-
end
|
32
|
+
end
|
@@ -8,8 +8,10 @@ class Metaphor
|
|
8
8
|
def call(headers, body)
|
9
9
|
result = JSON[body]
|
10
10
|
headers['content-type'] = case result
|
11
|
-
when Hash
|
12
|
-
|
11
|
+
when Hash
|
12
|
+
'application/x-ruby'
|
13
|
+
when String
|
14
|
+
'application/json'
|
13
15
|
end
|
14
16
|
[ headers, result ]
|
15
17
|
end
|
@@ -5,14 +5,9 @@ class Metaphor
|
|
5
5
|
@target = target
|
6
6
|
end
|
7
7
|
|
8
|
-
def call
|
9
|
-
|
10
|
-
@target.puts "#{header}:#{value}"
|
11
|
-
end
|
12
|
-
@target.puts
|
13
|
-
@target.puts body
|
14
|
-
@target.puts
|
8
|
+
def call message
|
9
|
+
@target.puts message
|
15
10
|
end
|
16
11
|
end
|
17
12
|
end
|
18
|
-
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metaphor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 21
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
6
|
- 1
|
10
|
-
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Sean O'Halpin
|
@@ -16,7 +15,7 @@ autorequire:
|
|
16
15
|
bindir: bin
|
17
16
|
cert_chain: []
|
18
17
|
|
19
|
-
date: 2010-
|
18
|
+
date: 2010-10-07 00:00:00 +01:00
|
20
19
|
default_executable:
|
21
20
|
dependencies: []
|
22
21
|
|
@@ -48,20 +47,16 @@ rdoc_options: []
|
|
48
47
|
require_paths:
|
49
48
|
- lib
|
50
49
|
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
50
|
requirements:
|
53
51
|
- - ">="
|
54
52
|
- !ruby/object:Gem::Version
|
55
|
-
hash: 3
|
56
53
|
segments:
|
57
54
|
- 0
|
58
55
|
version: "0"
|
59
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
-
none: false
|
61
57
|
requirements:
|
62
58
|
- - ">="
|
63
59
|
- !ruby/object:Gem::Version
|
64
|
-
hash: 23
|
65
60
|
segments:
|
66
61
|
- 1
|
67
62
|
- 3
|
@@ -70,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
65
|
requirements: []
|
71
66
|
|
72
67
|
rubyforge_project: metaphor
|
73
|
-
rubygems_version: 1.3.
|
68
|
+
rubygems_version: 1.3.6
|
74
69
|
signing_key:
|
75
70
|
specification_version: 3
|
76
71
|
summary: Generic pipeline processing
|