middlewear 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: d0a549a0b6aa91d2825af567d6dde24249ec950c
4
+ data.tar.gz: 776faf538a84ffbf99f4dfa48f045b45501115fc
5
+ SHA512:
6
+ metadata.gz: c0afe5943351fbbd86dcc85e81e204158c1d92546b2d4391b8f7c93cfdb2bb80e8e63d0cdf42556d300d9867c4f95cb5f9d7a5b9977f69c36f68e961adf23492
7
+ data.tar.gz: ca81bc8865e8b0271146cac52a13d2b2a720fbabe9b52149990af2e38c10e9be89c42c6316e047e98a5c67c96f688849683975a28957ac14a27d9824ad2ee52a
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.7
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at sax@livinginthepast.org. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in middlewear.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Eric Saxby
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,80 @@
1
+ Middlewear
2
+ ==========
3
+
4
+ Add a middleware stack to any project.
5
+
6
+
7
+ ## Usage
8
+
9
+ To avoid possible contamination of stacks if multiple projects are used that depend on it, create your own middleware
10
+ module:
11
+
12
+ ```ruby
13
+ require 'middlewear'
14
+
15
+ module MyApp
16
+ module Middleware
17
+ include Middlewear
18
+ end
19
+ end
20
+ ```
21
+
22
+ Now you can add middleware to the stack. Middleware classes can be written in such a way as to accept arguments.
23
+
24
+ ```ruby
25
+ MyApp::Middleware.add(MyMiddleware)
26
+ MyApp::Middleware.add(MyArgumentativeMiddleware, 'foo')
27
+ ```
28
+
29
+ Users will be able to manipulate a middleware stack after the fact:
30
+
31
+ ```ruby
32
+ MyApp::Middleware.delete(SomeMiddleware)
33
+ MyApp::Middleware.insert_before(SomeMiddleware, OtherMiddleware)
34
+ MyApp::Middleware.insert_after(SomeMiddleware, OtherMiddleware)
35
+ MyApp::Middleware.insert_after(SomeMiddleware, OtherMiddleware, 'with', 'arguments')
36
+ ```
37
+
38
+ Middleware should be written in the following format:
39
+
40
+ ```ruby
41
+ class MyMiddleware
42
+ def initialize(app)
43
+ @app = app
44
+ end
45
+
46
+ def call(foo)
47
+ puts foo # do some arbitrary work
48
+ @app.call(foo) # ensure that the rest of the middleware stack is called
49
+ end
50
+ end
51
+ ```
52
+
53
+ Now in order to actually process the stack, use the `#app` method provided on the module:
54
+
55
+ ```ruby
56
+ MyApp::Middleware.app.call(foo) do |foo|
57
+ # do
58
+ # the
59
+ # actual
60
+ # application
61
+ # stuff
62
+ end
63
+ ```
64
+
65
+ Note that we pass a single argument to the `#call` method. This is arbitrary. Middleware can be written to take any
66
+ number of arguments, so long as all of the registered middleware matches the call signature and it matches the number
67
+ of arguments of the block passed to `app.call`.
68
+
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/middlewear. This project is i
73
+ ntended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the
74
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
75
+
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ module Middlewear
2
+ # An App is instantiated and given a stack of instantiated Middleware objects.
3
+ # An app can by called with multiple arguments, excepting for the fact that the
4
+ # call signature used must match every middleware in the stack. It can be called
5
+ # with a block, which gets appended to the chain.
6
+ #
7
+ # Usage:
8
+ #
9
+ # app = App.new
10
+ # middleware = Middleware.new(app)
11
+ # app.stack = [middleware]
12
+ #
13
+ # app.call(message) do |message|
14
+ # # do work
15
+ # end
16
+ #
17
+ class App
18
+ attr_accessor :stack
19
+
20
+ def call(*args, &block)
21
+ stack << block if block_given?
22
+ current_register = stack.shift
23
+ current_register.call(*args) if current_register
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Middlewear
2
+ class Error < StandardError; end
3
+ class MiddlewareNotFound < Error; end
4
+ class DuplicateMiddleware < Error; end
5
+ end
@@ -0,0 +1,46 @@
1
+ module Middlewear
2
+ # A Register of a middleware class that messages will be passed through
3
+ # on the way to being dispatched.
4
+ class Register < Struct.new(:klass, :args)
5
+ def create_new(app)
6
+ klass.new(app, *args)
7
+ end
8
+ end
9
+
10
+ # Registry holds records of each middleware class that is added to the
11
+ # consumer middleware chain.
12
+ class Registry
13
+ include Enumerable
14
+
15
+ attr_reader :registry
16
+
17
+ def initialize(registry = [])
18
+ @registry = registry
19
+ end
20
+
21
+ def all
22
+ registry
23
+ end
24
+
25
+ def each(&blk)
26
+ all.each(&blk)
27
+ end
28
+
29
+ def delete(klass)
30
+ registry.reject! { |register| register.klass == klass }
31
+ end
32
+
33
+ def <<(klass_args)
34
+ insert(-1, klass_args[0], klass_args[1])
35
+ end
36
+
37
+ def index_of(klass)
38
+ registry.find_index { |register| register.klass == klass }
39
+ end
40
+
41
+ def insert(index, klass, args)
42
+ raise DuplicateMiddleware if index_of(klass)
43
+ registry.insert(index, Register.new(klass, args))
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Middlewear
2
+ VERSION = '0.1.0'
3
+ end
data/lib/middlewear.rb ADDED
@@ -0,0 +1,51 @@
1
+ require 'middlewear/app'
2
+ require 'middlewear/errors'
3
+ require 'middlewear/registry'
4
+ require 'middlewear/version'
5
+
6
+ module Middlewear
7
+ def self.included(base)
8
+ base.send(:extend, ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ def add(klass, *args)
13
+ registry << [klass, args]
14
+ end
15
+
16
+ def add_before(before_klass, klass, *args)
17
+ idx = registry.index_of(before_klass)
18
+ raise MiddlewareNotFound.new("#{before_klass} not registered in middleware stack") unless idx
19
+ registry.insert(idx, klass, args)
20
+ end
21
+
22
+ def add_after(after_klass, klass, *args)
23
+ idx = registry.index_of(after_klass)
24
+ raise MiddlewareNotFound.new("#{after_klass} not registered in middleware stack") unless idx
25
+ registry.insert(idx + 1, klass, args)
26
+ end
27
+
28
+ def delete(klass)
29
+ registry.delete(klass)
30
+ end
31
+
32
+ # The current registry of middleware. Note that this registry is not a set
33
+ # of instantiated middleware objects, but a registry of the classes themselves.
34
+ def registry
35
+ @registry ||= Registry.new
36
+ end
37
+
38
+ def create_stack(app)
39
+ registry.map { |r| r.create_new(app) }
40
+ end
41
+
42
+ # When called, this creates a new instance of an App with new instances of
43
+ # each registered middleware. To avoid contamination between calls, this
44
+ # instantiates a new set of objects each time it is called.
45
+ def app
46
+ App.new.tap do |app|
47
+ app.stack = create_stack(app)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'middlewear/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'middlewear'
8
+ spec.version = Middlewear::VERSION
9
+ spec.authors = ['Eric Saxby']
10
+ spec.email = ['sax@livinginthepast.org']
11
+
12
+ spec.summary = %q{Generic middleware registry and runner}
13
+ spec.description = %q{Generic middleware registry and runner}
14
+ spec.homepage = 'https://github.com/messagebus/middlewear'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.11'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middlewear
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Saxby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-11 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Generic middleware registry and runner
56
+ email:
57
+ - sax@livinginthepast.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - lib/middlewear.rb
71
+ - lib/middlewear/app.rb
72
+ - lib/middlewear/errors.rb
73
+ - lib/middlewear/registry.rb
74
+ - lib/middlewear/version.rb
75
+ - middlewear.gemspec
76
+ homepage: https://github.com/messagebus/middlewear
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.5
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Generic middleware registry and runner
100
+ test_files: []