filter_chain 0.1.0 → 0.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8d9ea641ac0e79a4b90fcfeac8b25c67e0ea1cb
4
- data.tar.gz: f80d61ce2db22a454f3bb07649553eb1eca1e572
3
+ metadata.gz: b3127a90f8424ced0cc5f39ef9a4d441371dd410
4
+ data.tar.gz: c84f731c83e69370657df510046619075328d79d
5
5
  SHA512:
6
- metadata.gz: bb0f65a45c63ef67c807b7cbf76d7fefc2703237882127eff315f219c4bc9dda330abe27f0b1229ee8e6168ccfdfa0d56f7d49442be32881a6b4e5bd1fa8a474
7
- data.tar.gz: c5a60682c76b7b1e286b56acb5942d0dbce7cca652dcedbe668666f06f95b55c12d93fcfdb4aad48ef4e1fb9cae567c5e64279f132daf1b246ce736b2c1a1b3b
6
+ metadata.gz: 9d493c676a3683688f39931a2bea3437121942344a0340e4aa334d892f6d6f4168fa8b6381faf200144de60b7cf5554a49f0b97d8aab2de11573ca306a24cb69
7
+ data.tar.gz: 0d01dc929f450d07f789d9b93aa8920e608e870dd5f674633de093d13c541277b9cf92b015bbcd69bc26a0741ccc604d0d8eb6ccb3921b026500a59b68902218
data/README.md CHANGED
@@ -18,23 +18,25 @@ Or install it yourself as:
18
18
 
19
19
  ## Chains
20
20
 
21
- A ````Chain```` is a container for a set of filters and makes it easy to create them and retrieve the results.
21
+ A ````Chain```` is a container for a set of filters.
22
22
 
23
- The constructor takes a schema that defines the set of filters you want to use.
24
23
  The below example serializes each input object to a JSON string, compresses the strings, prints the byte size of each string, and collects the results for future retrieval:
25
24
 
26
- chain = FilterChain::Chain.new(:filters => [
27
- {:class => FilterChain::SerializeFilter, :opts => {:format => :json}},
28
- {:class => FilterChain::DeflateFilter},
29
- {:class => FilterChain::ProcFilter, :opts => {:proc => proc { |data|
25
+ chain = FilterChain::Chain.new { |c|
26
+ c.add(FilterChain::SerializeFilter.new(:format => :json))
27
+
28
+ c.add(FilterChain::DeflateFilter.new)
29
+
30
+ c.add(FilterChain::ProcFilter.new { |data|
30
31
  puts "Byte size: #{data.bytesize}"
31
32
  data
32
- }}},
33
- {:class => FilterChain::Collector}
34
- ])
33
+ })
34
+
35
+ c.add(FilterChain::Collector.new)
36
+ }
35
37
 
36
- chain.input("Hello world")
37
- chain.input("How are you?")
38
+ chain << "Hello world"
39
+ chain << "How are you?"
38
40
 
39
41
  puts chain.output.inspect
40
42
 
@@ -47,10 +49,11 @@ A number of filters are pre-defined:
47
49
  - ````InflateFilter````
48
50
  - ````SerializeFilter````
49
51
  - ````DeserializeFilter````
50
- - ````SplitHeaderFilter````
51
- - ````UnsplitHeaderFilter````
52
+ - ````MultiplexFilter````
53
+ - ````DemultiplexFilter````
52
54
  - ````ProcFilter````
53
55
  - ````Collector````
56
+ - ````Terminator````
54
57
 
55
58
  You can easily create your own filters by inheriting from the ````Filter```` class and overriding the following handlers:
56
59
 
@@ -62,9 +65,12 @@ You pass data to the next filter by calling the ````pass```` method in your ````
62
65
  ## Collectors
63
66
 
64
67
  A ````Collector```` is a specialized filter designed to be the last link in a chain.
65
- Typically, all chains should end in a collector.
66
68
  An array based collector is included, but you can easily define custom collectors using other data structures.
67
69
 
70
+ ## Terminators
71
+
72
+ A ````Terminator```` takes the place of a collector when you want your last link to run a block of code.
73
+
68
74
  ## Contributing
69
75
 
70
76
  Bug reports and pull requests are welcome on GitHub at https://github.com/chadrem/filter_chain.
@@ -73,4 +79,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/chadre
73
79
  ## License
74
80
 
75
81
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
76
-
data/lib/filter_chain.rb CHANGED
@@ -9,8 +9,8 @@ require "filter_chain/filter"
9
9
  require "filter_chain/serialize_filter"
10
10
  require "filter_chain/deserialize_filter"
11
11
 
12
- require "filter_chain/split_header_filter"
13
- require "filter_chain/unsplit_header_filter"
12
+ require "filter_chain/multiplex_filter"
13
+ require "filter_chain/demultiplex_filter"
14
14
 
15
15
  require "filter_chain/deflate_filter"
16
16
  require "filter_chain/inflate_filter"
@@ -19,5 +19,7 @@ require "filter_chain/proc_filter"
19
19
 
20
20
  require "filter_chain/collector"
21
21
 
22
+ require "filter_chain/terminator"
23
+
22
24
  module FilterChain
23
25
  end
@@ -1,34 +1,35 @@
1
1
  module FilterChain
2
2
  class Chain
3
- attr_reader :filters
4
-
5
3
  def initialize(opts = {})
6
4
  @opts = opts
7
- @filters = create_filters(opts[:filters] || [])
5
+ @filters = []
6
+ @state = :initializing
7
+ yield(self)
8
+ @state = :initialized
8
9
  end
9
10
 
10
- def input(data)
11
- @filters.first.input(data)
11
+ def add(filter)
12
+ raise InvalidStateError unless @state == :initializing
13
+ @filters.last.next_filter = filter if @filters.last
14
+ @filters << filter
15
+
16
+ nil
12
17
  end
13
18
 
14
- def output
15
- raise MissingCollector if @filters.empty? || !@filters.last.is_a?(Collector)
19
+ def <<(data)
20
+ @filters.first << data
16
21
 
17
- @filters.last.collection
22
+ nil
18
23
  end
19
24
 
20
- private
21
-
22
- def create_filters(filter_schema)
23
- results = []
25
+ def output
26
+ raise MissingCollectorError if @filters.empty? || !@filters.last.is_a?(Collector)
24
27
 
25
- filter_schema.each do |elem|
26
- filter = elem[:class].new(elem[:opts])
27
- results.last.next_filter = filter unless results.empty?
28
- results << filter
29
- end
28
+ @filters.last.collection
29
+ end
30
30
 
31
- results
31
+ def length
32
+ @filters.length
32
33
  end
33
34
  end
34
35
  end
@@ -1,5 +1,5 @@
1
1
  module FilterChain
2
- class UnsplitHeaderFilter < Filter
2
+ class DemultiplexFilter < Filter
3
3
  private
4
4
 
5
5
  def on_initialize
@@ -8,15 +8,20 @@ module FilterChain
8
8
 
9
9
  def on_input(data)
10
10
  @buffer << data
11
+ payloads = []
11
12
 
12
13
  while @buffer.size >= 4
13
14
  if @buffer.size >= 4+(size=@buffer.unpack('N').first)
14
15
  @buffer.slice!(0,4)
15
- pass(@buffer.slice!(0,size))
16
+ payload = @buffer.slice!(0,size)
17
+ payloads << payload
18
+ pass(payload)
16
19
  else
17
20
  break
18
21
  end
19
22
  end
23
+
24
+ payloads
20
25
  end
21
26
  end
22
27
  end
@@ -26,7 +26,7 @@ module FilterChain
26
26
  end
27
27
 
28
28
  def raise_unknown_format
29
- raise FilterChainError, "Unknown format:#{opts[:format]}"
29
+ raise UnknownFormatError, "Unknown format:#{opts[:format]}"
30
30
  end
31
31
  end
32
32
  end
@@ -1,8 +1,9 @@
1
1
  module FilterChain
2
2
  class FilterChainError < RuntimeError; end
3
-
4
- class NextFilterMissing < FilterChainError; end
5
- class MissingCollector < FilterChainError; end
6
- class UnknownFormat < FilterChainError; end
7
- class MissingRequiredOpt < FilterChainError; end
3
+ class NextFilterMissingError < FilterChainError; end
4
+ class MissingCollectorError < FilterChainError; end
5
+ class UnknownFormatError < FilterChainError; end
6
+ class MissingRequiredOptError < FilterChainError; end
7
+ class InvalidStateError < FilterChainError; end
8
+ class MissingBlockError < FilterChainError; end
8
9
  end
@@ -1,24 +1,22 @@
1
1
  module FilterChain
2
2
  class Filter
3
3
  attr_reader :opts
4
+ attr_reader :block
4
5
 
5
6
  attr_accessor :next_filter
6
7
 
7
- def initialize(opts = {})
8
+ def initialize(opts = {}, &block)
8
9
  @opts = opts
10
+ @block = block
9
11
  on_initialize
10
12
  end
11
13
 
12
- def input(data)
14
+ def <<(data)
13
15
  on_input(data)
14
-
15
- nil
16
16
  end
17
17
 
18
18
  def pass(data)
19
- raise NextFilterMissing unless next_filter
20
-
21
- next_filter.input(data)
19
+ next_filter ? next_filter << data : data
22
20
  end
23
21
 
24
22
  private
@@ -1,5 +1,5 @@
1
1
  module FilterChain
2
- class SplitHeaderFilter < Filter
2
+ class MultiplexFilter < Filter
3
3
  private
4
4
 
5
5
  def on_initialize
@@ -3,11 +3,11 @@ module FilterChain
3
3
  private
4
4
 
5
5
  def on_initialize
6
- raise MissingRequiredOpt, ":proc is required" unless opts[:proc]
6
+ raise MissingBlockError unless block
7
7
  end
8
8
 
9
9
  def on_input(data)
10
- result = opts[:proc].call(data)
10
+ result = block.call(data)
11
11
 
12
12
  pass(result)
13
13
  end
@@ -26,7 +26,7 @@ module FilterChain
26
26
  end
27
27
 
28
28
  def raise_unknown_format
29
- raise FilterChainError, "Unknown format:#{opts[:format]}"
29
+ raise UnknownFormatError, "Unknown format:#{opts[:format]}"
30
30
  end
31
31
  end
32
32
  end
@@ -0,0 +1,15 @@
1
+ module FilterChain
2
+ class Terminator < Filter
3
+ private
4
+
5
+ def on_initialize
6
+ raise MissingBlockError unless block
7
+ end
8
+
9
+ def on_input(data)
10
+ block.call(data)
11
+
12
+ data
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module FilterChain
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filter_chain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chad Remesch
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-07 00:00:00.000000000 Z
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,14 +72,15 @@ files:
72
72
  - lib/filter_chain/chain.rb
73
73
  - lib/filter_chain/collector.rb
74
74
  - lib/filter_chain/deflate_filter.rb
75
+ - lib/filter_chain/demultiplex_filter.rb
75
76
  - lib/filter_chain/deserialize_filter.rb
76
77
  - lib/filter_chain/exceptions.rb
77
78
  - lib/filter_chain/filter.rb
78
79
  - lib/filter_chain/inflate_filter.rb
80
+ - lib/filter_chain/multiplex_filter.rb
79
81
  - lib/filter_chain/proc_filter.rb
80
82
  - lib/filter_chain/serialize_filter.rb
81
- - lib/filter_chain/split_header_filter.rb
82
- - lib/filter_chain/unsplit_header_filter.rb
83
+ - lib/filter_chain/terminator.rb
83
84
  - lib/filter_chain/version.rb
84
85
  homepage: https://github.com/chadrem/filter_chain
85
86
  licenses: