filter_chain 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8d9ea641ac0e79a4b90fcfeac8b25c67e0ea1cb
4
+ data.tar.gz: f80d61ce2db22a454f3bb07649553eb1eca1e572
5
+ SHA512:
6
+ metadata.gz: bb0f65a45c63ef67c807b7cbf76d7fefc2703237882127eff315f219c4bc9dda330abe27f0b1229ee8e6168ccfdfa0d56f7d49442be32881a6b4e5bd1fa8a474
7
+ data.tar.gz: c5a60682c76b7b1e286b56acb5942d0dbce7cca652dcedbe668666f06f95b55c12d93fcfdb4aad48ef4e1fb9cae567c5e64279f132daf1b246ce736b2c1a1b3b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in filter_chain.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Chad Remesch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Filter Chain [![Build Status](https://travis-ci.org/chadrem/filter_chain.svg)](https://travis-ci.org/chadrem/filter_chain)
2
+
3
+ Filter Chain is a Ruby gem for processing data through a chain of filters in an object oriented style.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem "filter_chain"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install filter_chain
18
+
19
+ ## Chains
20
+
21
+ A ````Chain```` is a container for a set of filters and makes it easy to create them and retrieve the results.
22
+
23
+ The constructor takes a schema that defines the set of filters you want to use.
24
+ 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
+
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|
30
+ puts "Byte size: #{data.bytesize}"
31
+ data
32
+ }}},
33
+ {:class => FilterChain::Collector}
34
+ ])
35
+
36
+ chain.input("Hello world")
37
+ chain.input("How are you?")
38
+
39
+ puts chain.output.inspect
40
+
41
+ ## Filters
42
+
43
+ A ````Filter```` is an object that takes an input and passes it's output to the next link in a chain of filters.
44
+ A number of filters are pre-defined:
45
+
46
+ - ````DeflateFilter````
47
+ - ````InflateFilter````
48
+ - ````SerializeFilter````
49
+ - ````DeserializeFilter````
50
+ - ````SplitHeaderFilter````
51
+ - ````UnsplitHeaderFilter````
52
+ - ````ProcFilter````
53
+ - ````Collector````
54
+
55
+ You can easily create your own filters by inheriting from the ````Filter```` class and overriding the following handlers:
56
+
57
+ - ````on_initialize````
58
+ - ````on_input(data)````
59
+
60
+ You pass data to the next filter by calling the ````pass```` method in your ````on_input```` method.
61
+
62
+ ## Collectors
63
+
64
+ 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
+ An array based collector is included, but you can easily define custom collectors using other data structures.
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/chadrem/filter_chain.
71
+
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
76
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "filter_chain"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'filter_chain/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "filter_chain"
8
+ spec.version = FilterChain::VERSION
9
+ spec.authors = ["Chad Remesch"]
10
+ spec.email = ["chad@remesch.com"]
11
+
12
+ spec.summary = %q{Object oriented filter chain and filter collection.}
13
+ spec.homepage = "https://github.com/chadrem/filter_chain"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest"
24
+ end
@@ -0,0 +1,34 @@
1
+ module FilterChain
2
+ class Chain
3
+ attr_reader :filters
4
+
5
+ def initialize(opts = {})
6
+ @opts = opts
7
+ @filters = create_filters(opts[:filters] || [])
8
+ end
9
+
10
+ def input(data)
11
+ @filters.first.input(data)
12
+ end
13
+
14
+ def output
15
+ raise MissingCollector if @filters.empty? || !@filters.last.is_a?(Collector)
16
+
17
+ @filters.last.collection
18
+ end
19
+
20
+ private
21
+
22
+ def create_filters(filter_schema)
23
+ results = []
24
+
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
30
+
31
+ results
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ module FilterChain
2
+ class Collector < Filter
3
+ attr_reader :collection
4
+
5
+ private
6
+
7
+ def on_initialize
8
+ @collection = []
9
+ end
10
+
11
+ def on_input(data)
12
+ @collection << data
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ module FilterChain
2
+ class DeflateFilter < Filter
3
+ private
4
+
5
+ def on_initialize
6
+ end
7
+
8
+ def on_input(data)
9
+ result = Zlib::Deflate.deflate(data)
10
+
11
+ pass(result)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ module FilterChain
2
+ class DeserializeFilter < Filter
3
+ private
4
+
5
+ def on_initialize
6
+ unless known_formats.include?(opts[:format])
7
+ raise_unknown_format
8
+ end
9
+ end
10
+
11
+ def on_input(data)
12
+ result = case opts[:format]
13
+ when :json
14
+ JSON.load(data)
15
+ when :marshal
16
+ Marshal.load(data)
17
+ else
18
+ raise_unknown_format
19
+ end
20
+
21
+ pass(result)
22
+ end
23
+
24
+ def known_formats
25
+ [:json, :marshal]
26
+ end
27
+
28
+ def raise_unknown_format
29
+ raise FilterChainError, "Unknown format:#{opts[:format]}"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,8 @@
1
+ module FilterChain
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
8
+ end
@@ -0,0 +1,37 @@
1
+ module FilterChain
2
+ class Filter
3
+ attr_reader :opts
4
+
5
+ attr_accessor :next_filter
6
+
7
+ def initialize(opts = {})
8
+ @opts = opts
9
+ on_initialize
10
+ end
11
+
12
+ def input(data)
13
+ on_input(data)
14
+
15
+ nil
16
+ end
17
+
18
+ def pass(data)
19
+ raise NextFilterMissing unless next_filter
20
+
21
+ next_filter.input(data)
22
+ end
23
+
24
+ private
25
+
26
+ #
27
+ # Event handlers (override these).
28
+ #
29
+
30
+ def on_initialize
31
+ end
32
+
33
+ def on_input(data)
34
+ pass(data)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ module FilterChain
2
+ class InflateFilter < Filter
3
+ private
4
+
5
+ def on_initialize
6
+ end
7
+
8
+ def on_input(data)
9
+ result = Zlib::Inflate.inflate(data)
10
+
11
+ pass(result)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module FilterChain
2
+ class ProcFilter < Filter
3
+ private
4
+
5
+ def on_initialize
6
+ raise MissingRequiredOpt, ":proc is required" unless opts[:proc]
7
+ end
8
+
9
+ def on_input(data)
10
+ result = opts[:proc].call(data)
11
+
12
+ pass(result)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ module FilterChain
2
+ class SerializeFilter < Filter
3
+ private
4
+
5
+ def on_initialize
6
+ unless known_formats.include?(opts[:format])
7
+ raise_unknown_format
8
+ end
9
+ end
10
+
11
+ def on_input(data)
12
+ result = case opts[:format]
13
+ when :json
14
+ JSON.dump(data)
15
+ when :marshal
16
+ Marshal.dump(data)
17
+ else
18
+ raise_unknown_format
19
+ end
20
+
21
+ pass(result)
22
+ end
23
+
24
+ def known_formats
25
+ [:json, :marshal]
26
+ end
27
+
28
+ def raise_unknown_format
29
+ raise FilterChainError, "Unknown format:#{opts[:format]}"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ module FilterChain
2
+ class SplitHeaderFilter < Filter
3
+ private
4
+
5
+ def on_initialize
6
+ end
7
+
8
+ def on_input(data)
9
+ result = [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*')
10
+
11
+ pass(result)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module FilterChain
2
+ class UnsplitHeaderFilter < Filter
3
+ private
4
+
5
+ def on_initialize
6
+ @buffer = ""
7
+ end
8
+
9
+ def on_input(data)
10
+ @buffer << data
11
+
12
+ while @buffer.size >= 4
13
+ if @buffer.size >= 4+(size=@buffer.unpack('N').first)
14
+ @buffer.slice!(0,4)
15
+ pass(@buffer.slice!(0,size))
16
+ else
17
+ break
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module FilterChain
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ require "json"
2
+ require "zlib"
3
+
4
+ require "filter_chain/version"
5
+ require "filter_chain/exceptions"
6
+ require "filter_chain/chain"
7
+ require "filter_chain/filter"
8
+
9
+ require "filter_chain/serialize_filter"
10
+ require "filter_chain/deserialize_filter"
11
+
12
+ require "filter_chain/split_header_filter"
13
+ require "filter_chain/unsplit_header_filter"
14
+
15
+ require "filter_chain/deflate_filter"
16
+ require "filter_chain/inflate_filter"
17
+
18
+ require "filter_chain/proc_filter"
19
+
20
+ require "filter_chain/collector"
21
+
22
+ module FilterChain
23
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filter_chain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chad Remesch
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - chad@remesch.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - filter_chain.gemspec
71
+ - lib/filter_chain.rb
72
+ - lib/filter_chain/chain.rb
73
+ - lib/filter_chain/collector.rb
74
+ - lib/filter_chain/deflate_filter.rb
75
+ - lib/filter_chain/deserialize_filter.rb
76
+ - lib/filter_chain/exceptions.rb
77
+ - lib/filter_chain/filter.rb
78
+ - lib/filter_chain/inflate_filter.rb
79
+ - lib/filter_chain/proc_filter.rb
80
+ - 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/version.rb
84
+ homepage: https://github.com/chadrem/filter_chain
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.4.5
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Object oriented filter chain and filter collection.
108
+ test_files: []