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 +4 -4
- data/README.md +20 -15
- data/lib/filter_chain.rb +4 -2
- data/lib/filter_chain/chain.rb +19 -18
- data/lib/filter_chain/{unsplit_header_filter.rb → demultiplex_filter.rb} +7 -2
- data/lib/filter_chain/deserialize_filter.rb +1 -1
- data/lib/filter_chain/exceptions.rb +6 -5
- data/lib/filter_chain/filter.rb +5 -7
- data/lib/filter_chain/{split_header_filter.rb → multiplex_filter.rb} +1 -1
- data/lib/filter_chain/proc_filter.rb +2 -2
- data/lib/filter_chain/serialize_filter.rb +1 -1
- data/lib/filter_chain/terminator.rb +15 -0
- data/lib/filter_chain/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3127a90f8424ced0cc5f39ef9a4d441371dd410
|
4
|
+
data.tar.gz: c84f731c83e69370657df510046619075328d79d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
34
|
-
|
33
|
+
})
|
34
|
+
|
35
|
+
c.add(FilterChain::Collector.new)
|
36
|
+
}
|
35
37
|
|
36
|
-
chain
|
37
|
-
chain
|
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
|
-
- ````
|
51
|
-
- ````
|
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/
|
13
|
-
require "filter_chain/
|
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
|
data/lib/filter_chain/chain.rb
CHANGED
@@ -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 =
|
5
|
+
@filters = []
|
6
|
+
@state = :initializing
|
7
|
+
yield(self)
|
8
|
+
@state = :initialized
|
8
9
|
end
|
9
10
|
|
10
|
-
def
|
11
|
-
@
|
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
|
15
|
-
|
19
|
+
def <<(data)
|
20
|
+
@filters.first << data
|
16
21
|
|
17
|
-
|
22
|
+
nil
|
18
23
|
end
|
19
24
|
|
20
|
-
|
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
|
-
|
26
|
-
|
27
|
-
results.last.next_filter = filter unless results.empty?
|
28
|
-
results << filter
|
29
|
-
end
|
28
|
+
@filters.last.collection
|
29
|
+
end
|
30
30
|
|
31
|
-
|
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
|
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
|
-
|
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
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module FilterChain
|
2
2
|
class FilterChainError < RuntimeError; end
|
3
|
-
|
4
|
-
class
|
5
|
-
class
|
6
|
-
class
|
7
|
-
class
|
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
|
data/lib/filter_chain/filter.rb
CHANGED
@@ -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
|
14
|
+
def <<(data)
|
13
15
|
on_input(data)
|
14
|
-
|
15
|
-
nil
|
16
16
|
end
|
17
17
|
|
18
18
|
def pass(data)
|
19
|
-
|
20
|
-
|
21
|
-
next_filter.input(data)
|
19
|
+
next_filter ? next_filter << data : data
|
22
20
|
end
|
23
21
|
|
24
22
|
private
|
@@ -3,11 +3,11 @@ module FilterChain
|
|
3
3
|
private
|
4
4
|
|
5
5
|
def on_initialize
|
6
|
-
raise
|
6
|
+
raise MissingBlockError unless block
|
7
7
|
end
|
8
8
|
|
9
9
|
def on_input(data)
|
10
|
-
result =
|
10
|
+
result = block.call(data)
|
11
11
|
|
12
12
|
pass(result)
|
13
13
|
end
|
data/lib/filter_chain/version.rb
CHANGED
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
|
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-
|
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/
|
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:
|