ibsciss-middleware 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/middleware/builder.rb +7 -6
- data/middleware.gemspec +1 -1
- data/spec/middleware/builder_spec.rb +21 -21
- data/spec/middleware/runner_spec.rb +8 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86fc76bf275b9613ba8eaf6234356f6e897e8fa2
|
4
|
+
data.tar.gz: 40a061815db18be1438ce941abd3d8df47d80c96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20bdeb80623bfad270d6a44d5d7d7c95dc00af9ce262728649ea88b90ae0bb6a1dae6c2cecef187363631fecc8718300e8b5ee6c54d445ffe2b603f0b7d30e57
|
7
|
+
data.tar.gz: 7cfefa01cc28a9765bb715af5f1513acc8c839c47d241204ebcee26d4be57372b07c3fa74ba8ac4100e0a17218c4d507ab52070af24710ddde7f81c15d98abe2
|
data/README.md
CHANGED
@@ -204,7 +204,7 @@ end.call()
|
|
204
204
|
The call method takes an optional parameter which is the state to pass into the
|
205
205
|
initial middleware.
|
206
206
|
|
207
|
-
You can optionally set a name, that will be displayed in inspect and for logging purpose
|
207
|
+
You can optionally set a name, that will be displayed in inspect and for logging purpose:
|
208
208
|
|
209
209
|
```ruby
|
210
210
|
Middleware::Builder.new(name: 'MyPersonalMiddleware')
|
data/lib/middleware/builder.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require_relative 'logger'
|
2
|
+
|
1
3
|
module Middleware
|
2
4
|
# This provides a DSL for building up a stack of middlewares.
|
3
5
|
#
|
@@ -40,8 +42,8 @@ module Middleware
|
|
40
42
|
# like {#use} and such.
|
41
43
|
def initialize(opts = nil, &block)
|
42
44
|
opts ||= {}
|
43
|
-
@runner_class = opts
|
44
|
-
@middleware_name = opts
|
45
|
+
@runner_class = opts.fetch(:runner_class, Runner)
|
46
|
+
@middleware_name = opts.fetch(:name, 'Middleware')
|
45
47
|
|
46
48
|
if block_given?
|
47
49
|
if block.arity == 1
|
@@ -137,10 +139,9 @@ module Middleware
|
|
137
139
|
end
|
138
140
|
|
139
141
|
def inspect
|
140
|
-
name+'[' + stack.
|
142
|
+
name+'[' + stack.map do |middleware|
|
141
143
|
name = middleware[0].is_a?(Proc) ? 'Proc' : middleware[0].name
|
142
|
-
|
143
|
-
carry << "#{name}(#{middleware[1].join(', ')})"
|
144
|
+
"#{name}(#{middleware[1].join(', ')})"
|
144
145
|
end.join(', ') + ']'
|
145
146
|
end
|
146
147
|
|
@@ -173,7 +174,7 @@ module Middleware
|
|
173
174
|
end
|
174
175
|
|
175
176
|
# you shouldn't use this method
|
176
|
-
#
|
177
|
+
# used for insert_before|after_each
|
177
178
|
# @return [Array]
|
178
179
|
attr_writer :stack
|
179
180
|
|
data/middleware.gemspec
CHANGED
@@ -37,36 +37,36 @@ describe Middleware::Builder do
|
|
37
37
|
|
38
38
|
app.call(data)
|
39
39
|
|
40
|
-
expect(data[:data]).to
|
40
|
+
expect(data[:data]).to eq true
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
context 'basic `use`' do
|
46
|
-
it '
|
46
|
+
it 'adds items to the stack and make them callable' do
|
47
47
|
data = {}
|
48
48
|
proc = proc { |env| env[:data] = true }
|
49
49
|
|
50
50
|
instance.use proc
|
51
51
|
instance.call(data)
|
52
52
|
|
53
|
-
expect(data[:data]).to
|
53
|
+
expect(data[:data]).to eq true
|
54
54
|
end
|
55
55
|
|
56
|
-
it '
|
56
|
+
it 'is able to add multiple items' do
|
57
57
|
data = {}
|
58
|
-
proc1 =
|
59
|
-
proc2 =
|
58
|
+
proc1 = ->(env) { env.tap { |obj| obj[:one] = :value_1 } }
|
59
|
+
proc2 = ->(env) { env.tap { |obj| obj[:two] = :value_2 } }
|
60
60
|
|
61
61
|
instance.use proc1
|
62
62
|
instance.use proc2
|
63
63
|
instance.call(data)
|
64
64
|
|
65
|
-
expect(data[:one]).to
|
66
|
-
expect(data[:two]).to
|
65
|
+
expect(data[:one]).to eq :value_1
|
66
|
+
expect(data[:two]).to eq :value_2
|
67
67
|
end
|
68
68
|
|
69
|
-
it '
|
69
|
+
it 'can be compose with another builder' do
|
70
70
|
data = {}
|
71
71
|
proc1 = proc { |env| env[:one] = true }
|
72
72
|
|
@@ -80,17 +80,17 @@ describe Middleware::Builder do
|
|
80
80
|
|
81
81
|
# Call the 2nd and verify results
|
82
82
|
two.call(data)
|
83
|
-
expect(data[:one]).to
|
83
|
+
expect(data[:one]).to eq true
|
84
84
|
end
|
85
85
|
|
86
|
-
it '
|
86
|
+
it 'has the env to `nil` if not given' do
|
87
87
|
result = false
|
88
88
|
proc = proc { |env| result = env.nil? }
|
89
89
|
|
90
90
|
instance.use proc
|
91
91
|
instance.call
|
92
92
|
|
93
|
-
expect(result).to
|
93
|
+
expect(result).to eq true
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
@@ -103,7 +103,7 @@ describe Middleware::Builder do
|
|
103
103
|
expect(data[:data]).to eq [2, 1]
|
104
104
|
end
|
105
105
|
|
106
|
-
it 'can insert
|
106
|
+
it 'can insert after a previous object' do
|
107
107
|
proc2 = appender_proc(2)
|
108
108
|
instance.use appender_proc(1)
|
109
109
|
instance.use proc2
|
@@ -113,7 +113,7 @@ describe Middleware::Builder do
|
|
113
113
|
expect(data[:data]).to eq [1, 3, 2]
|
114
114
|
end
|
115
115
|
|
116
|
-
it 'can insert before' do
|
116
|
+
it 'can insert before a previous object' do
|
117
117
|
instance.use appender_proc(1)
|
118
118
|
instance.insert_before 0, appender_proc(2)
|
119
119
|
instance.call(data)
|
@@ -121,7 +121,7 @@ describe Middleware::Builder do
|
|
121
121
|
expect(data[:data]).to eq [2, 1]
|
122
122
|
end
|
123
123
|
|
124
|
-
it 'raises an exception if attempting to insert before an invalid
|
124
|
+
it 'raises an exception if attempting to insert before an invalid index' do
|
125
125
|
expect { instance.insert 'object', appender_proc(1) }
|
126
126
|
.to raise_error(RuntimeError)
|
127
127
|
end
|
@@ -216,18 +216,18 @@ describe Middleware::Builder do
|
|
216
216
|
expect(instance.inspect).to eq 'Middleware[Proc(), Proc(2), Echo(Hi, how are you?)]'
|
217
217
|
end
|
218
218
|
|
219
|
-
it '
|
219
|
+
it 'can have a name' do
|
220
|
+
expect(described_class.new(name: 'Name').name).to eq 'Name'
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'displays the name in the inspect' do
|
220
224
|
middleware = described_class.new(name: 'Dumb') { |b|
|
221
225
|
b.use appender_proc(1)
|
222
226
|
}
|
223
227
|
expect(middleware.inspect).to eq 'Dumb[Proc()]'
|
224
228
|
end
|
225
229
|
|
226
|
-
it
|
227
|
-
expect(described_class.new(name: 'Name').name).to eq 'Name'
|
228
|
-
end
|
229
|
-
|
230
|
-
it "can have a Logger" do
|
230
|
+
it 'makes use of a Logger' do
|
231
231
|
mocked_logger = instance_double(Logger)
|
232
232
|
expect(mocked_logger).to receive(:add).exactly(4).times
|
233
233
|
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'middleware'
|
2
2
|
|
3
3
|
describe Middleware::Runner do
|
4
|
-
it '
|
4
|
+
it 'works with an empty stack' do
|
5
5
|
instance = described_class.new([])
|
6
6
|
expect { instance.call({}) }.to_not raise_error
|
7
7
|
end
|
8
8
|
|
9
|
-
it '
|
9
|
+
it 'calls classes in the same order as given' do
|
10
10
|
a = Class.new do
|
11
11
|
def initialize(app)
|
12
12
|
@app = app
|
@@ -37,7 +37,7 @@ describe Middleware::Runner do
|
|
37
37
|
expect(env[:result]).to eq %w(A B B A)
|
38
38
|
end
|
39
39
|
|
40
|
-
it '
|
40
|
+
it 'calls lambdas in the proper order' do
|
41
41
|
data = []
|
42
42
|
a = ->(_env) { data << 'A' }
|
43
43
|
b = ->(_env) { data << 'B' }
|
@@ -48,7 +48,7 @@ describe Middleware::Runner do
|
|
48
48
|
expect(data).to eq %w(A B)
|
49
49
|
end
|
50
50
|
|
51
|
-
it '
|
51
|
+
it 'lets lambdas to change the given argument' do
|
52
52
|
a = ->(env) { env + 1 }
|
53
53
|
b = ->(env) { env + 2 }
|
54
54
|
|
@@ -104,11 +104,11 @@ describe Middleware::Runner do
|
|
104
104
|
expect(env[:result]).to eq 42
|
105
105
|
end
|
106
106
|
|
107
|
-
it '
|
107
|
+
it 'raises an error if an invalid middleware is given' do
|
108
108
|
expect { described_class.new([27]) }.to raise_error(/Invalid middleware/)
|
109
109
|
end
|
110
110
|
|
111
|
-
it "
|
111
|
+
it "not calls middlewares which aren't called" do
|
112
112
|
# A does not call B, so B should never execute
|
113
113
|
data = []
|
114
114
|
a = Class.new do
|
@@ -131,7 +131,7 @@ describe Middleware::Runner do
|
|
131
131
|
end
|
132
132
|
|
133
133
|
describe 'exceptions' do
|
134
|
-
it '
|
134
|
+
it 'propagates the exception up the middleware chain' do
|
135
135
|
# This tests a few important properties:
|
136
136
|
# * Exceptions propagate multiple middlewares
|
137
137
|
# - C raises an exception, which raises through B to A.
|
@@ -174,7 +174,7 @@ describe Middleware::Runner do
|
|
174
174
|
expect(data).to eq %w(a b e)
|
175
175
|
end
|
176
176
|
|
177
|
-
it '
|
177
|
+
it 'stops propagation if rescued' do
|
178
178
|
# This test mainly tests that if there is a sequence A, B, C, and
|
179
179
|
# an exception is raised in C, that if B rescues this, then the chain
|
180
180
|
# continues fine backwards.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ibsciss-middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mitchell Hashimoto
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-11-
|
12
|
+
date: 2015-11-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|