middlestack 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +4 -0
- data/.travis.yml +10 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +6 -0
- data/lib/middlestack.rb +6 -0
- data/lib/middlestack/helper.rb +21 -0
- data/lib/middlestack/stack.rb +44 -0
- data/lib/middlestack/version.rb +3 -0
- data/middlestack.gemspec +23 -0
- data/spec/lib/middlestack/helper_spec.rb +32 -0
- data/spec/spec_helper.rb +26 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 77a993d07a4c6f1838cdb49096ef378d9426162f
|
4
|
+
data.tar.gz: ca06e9c42248831980e845896808e3eb3cfc2da3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1dff26c51578fc8d70a788c4deac5f50d93eb645a8e861081c187df121583daeb62608cd384d7e7c95f114692725731be577b9c7e49a6d85a2af96f77efba9e5
|
7
|
+
data.tar.gz: f05a886949e2dbae472221b99e18f61b08a61292d6551387a1a1180b9df4dda4124dccdb054945612214c2719f897168df91b972c4a12b1f35f0f0e87208292b
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 undr
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Middlestack
|
2
|
+
|
3
|
+
The Middlestack is a simple and convenient way to build and run of your own middleware stack for ruby
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/undr/middlestack.svg)](https://travis-ci.org/undr/middlestack)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/undr/middlestack/badges/gpa.svg)](https://codeclimate.com/github/undr/middlestack)
|
7
|
+
[![Test Coverage](https://codeclimate.com/github/undr/middlestack/badges/coverage.svg)](https://codeclimate.com/github/undr/middlestack)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'middlestack'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
```
|
20
|
+
$ bundle
|
21
|
+
```
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
```
|
26
|
+
$ gem install middlestack
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
It's pretty simple.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class TraceValue < Struct.new(:env, :value)
|
35
|
+
def before
|
36
|
+
env << ('->' + value)
|
37
|
+
end
|
38
|
+
|
39
|
+
def after
|
40
|
+
env << ('<-' + value)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Executor
|
45
|
+
include Middlestack::Helper
|
46
|
+
|
47
|
+
middlewares do
|
48
|
+
use TraceValue, 'A'
|
49
|
+
use TraceValue, 'B'
|
50
|
+
use TraceValue, 'C'
|
51
|
+
end
|
52
|
+
|
53
|
+
def execute
|
54
|
+
run_middlewares([]){|env| env << 'run' }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
Executor.new.execute
|
59
|
+
# => ['->A', '->B', '->C', 'run', '<-C', '<-B', '<-A']
|
60
|
+
```
|
61
|
+
|
62
|
+
That's all. Have a nice code!
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
1. Fork it ( https://github.com/undr/middlestack/fork )
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
68
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/middlestack.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Middlestack
|
2
|
+
module Helper
|
3
|
+
class << self
|
4
|
+
def included(base)
|
5
|
+
base.send(:extend, ClassMethods)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def run_middlewares(*args, &block)
|
10
|
+
self.class.stack.to_app(block).call(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
attr_reader :stack
|
15
|
+
|
16
|
+
def middlewares(&block)
|
17
|
+
@stack = Stack.new(&block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Middlestack
|
2
|
+
class Stack
|
3
|
+
class Proxy
|
4
|
+
attr_reader :target_class, :app, :args, :block
|
5
|
+
|
6
|
+
def initialize(target_class, app, *args, &block)
|
7
|
+
@target_class, @app, @args, @block = target_class, app, args, block
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
with_env(env) do |target|
|
12
|
+
target.before if target.respond_to?(:before)
|
13
|
+
app.call(env)
|
14
|
+
target.after if target.respond_to?(:after)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def with_env(env)
|
21
|
+
yield target_class.new(env, *args, &block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(default_app = nil, &block)
|
26
|
+
@use, @run = [], default_app
|
27
|
+
instance_eval(&block) if block_given?
|
28
|
+
end
|
29
|
+
|
30
|
+
def use(middleware, *args, &block)
|
31
|
+
@use << ->(app){ Proxy.new(middleware, app, *args, &block) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_app(app)
|
35
|
+
app ||= @run
|
36
|
+
fail 'missing run statement' unless app
|
37
|
+
@use.reverse.inject(app){|a, e| e[a] }
|
38
|
+
end
|
39
|
+
|
40
|
+
def call(env)
|
41
|
+
to_app.call(env)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/middlestack.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'middlestack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'middlestack'
|
8
|
+
spec.version = Middlestack::VERSION
|
9
|
+
spec.authors = ['undr']
|
10
|
+
spec.email = ['undr@yandex.ru']
|
11
|
+
spec.summary = %q{The Middlestack is a simple and convenient way to build and run your own middleware stack for ruby.}
|
12
|
+
spec.description = %q{The Middlestack is a simple and convenient way to build and run your own middleware stack for ruby.}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TraceValue < Struct.new(:env, :value)
|
4
|
+
def before
|
5
|
+
env << ('->' + value)
|
6
|
+
end
|
7
|
+
|
8
|
+
def after
|
9
|
+
env << ('<-' + value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Executor
|
14
|
+
include Middlestack::Helper
|
15
|
+
|
16
|
+
middlewares do
|
17
|
+
use TraceValue, 'A'
|
18
|
+
use TraceValue, 'B'
|
19
|
+
use TraceValue, 'C'
|
20
|
+
end
|
21
|
+
|
22
|
+
def execute
|
23
|
+
run_middlewares([]){|env| env << 'run' }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
RSpec.describe Middlestack::Helper do
|
28
|
+
describe '#run_middlewares' do
|
29
|
+
subject{ Executor.new }
|
30
|
+
specify{ expect(subject.execute).to eq(['->A', '->B', '->C', 'run', '<-C', '<-B', '<-A']) }
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
if ENV['CODECLIMATE_REPO_TOKEN']
|
2
|
+
require "codeclimate-test-reporter"
|
3
|
+
CodeClimate::TestReporter.start
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'bundler'
|
7
|
+
Bundler.require(:default, :test)
|
8
|
+
|
9
|
+
require './lib/middlestack'
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.expect_with :rspec do |expectations|
|
13
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
14
|
+
end
|
15
|
+
|
16
|
+
config.mock_with :rspec do |mocks|
|
17
|
+
mocks.verify_partial_doubles = true
|
18
|
+
end
|
19
|
+
|
20
|
+
if config.files_to_run.one?
|
21
|
+
config.default_formatter = 'doc'
|
22
|
+
end
|
23
|
+
|
24
|
+
config.disable_monkey_patching!
|
25
|
+
config.order = :random
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: middlestack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- undr
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-01 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: The Middlestack is a simple and convenient way to build and run your
|
42
|
+
own middleware stack for ruby.
|
43
|
+
email:
|
44
|
+
- undr@yandex.ru
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".travis.yml"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/middlestack.rb
|
57
|
+
- lib/middlestack/helper.rb
|
58
|
+
- lib/middlestack/stack.rb
|
59
|
+
- lib/middlestack/version.rb
|
60
|
+
- middlestack.gemspec
|
61
|
+
- spec/lib/middlestack/helper_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
homepage: ''
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.4.6
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: The Middlestack is a simple and convenient way to build and run your own
|
87
|
+
middleware stack for ruby.
|
88
|
+
test_files:
|
89
|
+
- spec/lib/middlestack/helper_spec.rb
|
90
|
+
- spec/spec_helper.rb
|