@@ -9,7 +9,9 @@ large sequential chunks of logic into small pieces.
9
9
10
10
Middleware is distributed as a RubyGem, so simply gem install:
11
11
12
- gem install middleware
12
+ ``` console
13
+ $ gem install middleware
14
+ ```
13
15
14
16
## A Basic Example
15
17
@@ -83,24 +85,28 @@ problems.
83
85
One method of creating middleware, and by far the most common, is to define
84
86
a class that duck types to the following interface:
85
87
86
- class MiddlewareExample
87
- def initialize(app); end
88
- def call(env); end
89
- end
88
+ ``` ruby
89
+ class MiddlewareExample
90
+ def initialize (app ); end
91
+ def call (env ); end
92
+ end
93
+ ` ` `
90
94
91
95
Therefore, a basic middleware example follows:
92
96
93
- class Trace
94
- def initialize(app)
95
- @app = app
96
- end
97
+ ` ` ` ruby
98
+ class Trace
99
+ def initialize (app )
100
+ @app = app
101
+ end
97
102
98
- def call(env)
99
- puts "Trace up"
100
- @app.call(env)
101
- puts "Trace down"
102
- end
103
- end
103
+ def call (env )
104
+ puts " Trace up"
105
+ @app .call(env)
106
+ puts " Trace down"
107
+ end
108
+ end
109
+ ` ` `
104
110
105
111
A basic description of the two methods that a middleware must implement:
106
112
@@ -118,7 +124,9 @@ A middleware can also be a simple lambda. The downside of using a lambda is that
118
124
it only has access to the state on the initial call, there is no "post" step for
119
125
lambdas. A basic example, in the context of a web request:
120
126
121
- lambda { |env| puts "You requested: #{env["http.request_url"]}" }
127
+ ` ` ` ruby
128
+ lambda { |env | puts " You requested: #{ env[" http.request_url" ] } " }
129
+ ` ` `
122
130
123
131
## Middleware Stacks
124
132
@@ -131,15 +139,19 @@ executed in the order given.
131
139
The middleware library comes with a ` Builder ` class which provides a nice DSL
132
140
for building a stack of middlewares:
133
141
134
- stack = Middleware::Builder.new do
135
- use Trace
136
- use lambda { |env| puts "LAMBDA!" }
137
- end
142
+ ` ` ` ruby
143
+ stack = Middleware ::Builder .new do
144
+ use Trace
145
+ use lambda { |env | puts " LAMBDA!" }
146
+ end
147
+ ` ` `
138
148
139
149
This ` stack` variable itself is now a valid middleware and has the same interface,
140
150
so to execute the stack, just call ` call` on it:
141
151
142
- stack.call
152
+ ` ` ` ruby
153
+ stack.call
154
+ ` ` `
143
155
144
156
The call method takes an optional parameter which is the state to pass into the
145
157
initial middleware.
@@ -152,37 +164,43 @@ created. Given the `stack` variable created above, we can manipulate it as
152
164
follows. Please imagine that each example runs with the original ` stack` variable,
153
165
so that the order of the examples doesn't actually matter:
154
166
155
- # Insert a new item after the Trace middleware
156
- stack.insert_after(Trace, SomeOtherMiddleware)
167
+ ` ` ` ruby
168
+ # Insert a new item after the Trace middleware
169
+ stack.insert_after(Trace , SomeOtherMiddleware )
157
170
158
- # Replace the lambda
159
- stack.replace(1, SomeOtherMiddleware)
171
+ # Replace the lambda
172
+ stack.replace(1 , SomeOtherMiddleware )
160
173
161
- # Delete the lambda
162
- stack.delete(1)
174
+ # Delete the lambda
175
+ stack.delete(1 )
176
+ ```
163
177
164
178
### Passing Additional Constructor Arguments
165
179
166
180
When using middleware in a stack, you can also pass in additional constructor
167
181
arguments. Given the following middleware:
168
182
169
- class Echo
170
- def initialize(app, message)
171
- @app = app
172
- @message = message
173
- end
183
+ ``` ruby
184
+ class Echo
185
+ def initialize (app , message )
186
+ @app = app
187
+ @message = message
188
+ end
174
189
175
- def call(env)
176
- puts @message
177
- @app.call(env)
178
- end
179
- end
190
+ def call (env )
191
+ puts @message
192
+ @app .call(env)
193
+ end
194
+ end
195
+ ```
180
196
181
197
We can initialize ` Echo ` with a proper message as follows:
182
198
183
- Middleware::Builder.new do
184
- use Echo, "Hello, World!"
185
- end
199
+ ``` ruby
200
+ Middleware ::Builder .new do
201
+ use Echo , " Hello, World!"
202
+ end
203
+ ```
186
204
187
205
Then when the stack is called, it will output "Hello, World!"
188
206
0 commit comments