metaphor 0.2.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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(headers, body) # => [ new_headers, new_body ]
29
- # => false (if halted by processor)
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 #get and return an array of headers
37
- and the message body:
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 get
41
- # return an array like this:
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(headers, body)
53
- puts "Headers: #{headers.inspect}"
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
- * An array of headers and the message body
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
- * Any other value that is not false
66
- - Metaphor passes the same headers and message that were passed into
67
- this processor to the next processor in the chain
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.
@@ -1,33 +1,32 @@
1
1
  class Metaphor
2
- VERSION = '0.2.1'
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(*args)
10
- case args.size
11
- when 1
12
- while message = args.first.get
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
- when 2
16
- process_message(*args)
14
+ else
15
+ process_message input
17
16
  end
18
17
  end
19
18
 
20
19
  private
21
- def process_message(headers, body)
20
+ def process_message message
22
21
  processors.each do |processor|
23
- processor_output = processor.call(headers, body)
22
+ processor_output = processor.call(message)
24
23
  case
25
24
  when processor_output === false
26
25
  return false
27
- when processor_output.respond_to?(:size) && processor_output.size == 2
28
- headers, body = processor_output
26
+ when processor_output != nil
27
+ message = processor_output
29
28
  end
30
29
  end
31
- [ headers, body ]
30
+ message
32
31
  end
33
- end
32
+ end
@@ -21,8 +21,8 @@ class Metaphor
21
21
  @active
22
22
  end
23
23
 
24
- def call(headers, body)
25
- active_processor.call headers, body
24
+ def call message
25
+ active_processor.call message
26
26
  end
27
27
 
28
28
  private
@@ -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: 'application/x-ruby'
12
- when String: 'application/json'
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(headers, body)
9
- headers.each_pair do |header, value|
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
@@ -19,9 +19,9 @@ class Metaphor
19
19
  @active = false
20
20
  end
21
21
 
22
- def call(headers, body)
23
- @wiretap.call(headers, body) if active?
24
- @default.call(headers, body)
22
+ def call message
23
+ @wiretap.call(message) if active?
24
+ @default.call(message)
25
25
  end
26
26
  end
27
27
  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
- version: 0.2.1
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-08-20 00:00:00 +01:00
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.7
68
+ rubygems_version: 1.3.6
74
69
  signing_key:
75
70
  specification_version: 3
76
71
  summary: Generic pipeline processing