Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.

Commit a589a8a

Browse files
committed
Merge pull request #1 from bearded/markdown
Ruby colors in User guide
2 parents 8334443 + 1045b5a commit a589a8a

File tree

2 files changed

+61
-41
lines changed

2 files changed

+61
-41
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ To get started, the best place to look is [the user guide](https://github.com/mi
1111

1212
This project is distributed as a RubyGem:
1313

14-
$ gem install middleware
14+
```console
15+
$ gem install middleware
16+
```
1517

1618
## Usage
1719

user_guide.md

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ large sequential chunks of logic into small pieces.
99

1010
Middleware is distributed as a RubyGem, so simply gem install:
1111

12-
gem install middleware
12+
```console
13+
$ gem install middleware
14+
```
1315

1416
## A Basic Example
1517

@@ -83,24 +85,28 @@ problems.
8385
One method of creating middleware, and by far the most common, is to define
8486
a class that duck types to the following interface:
8587

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+
```
9094
9195
Therefore, a basic middleware example follows:
9296
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
97102

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+
```
104110
105111
A basic description of the two methods that a middleware must implement:
106112
@@ -118,7 +124,9 @@ A middleware can also be a simple lambda. The downside of using a lambda is that
118124
it only has access to the state on the initial call, there is no "post" step for
119125
lambdas. A basic example, in the context of a web request:
120126
121-
lambda { |env| puts "You requested: #{env["http.request_url"]}" }
127+
```ruby
128+
lambda { |env| puts "You requested: #{env["http.request_url"]}" }
129+
```
122130
123131
## Middleware Stacks
124132
@@ -131,15 +139,19 @@ executed in the order given.
131139
The middleware library comes with a `Builder` class which provides a nice DSL
132140
for building a stack of middlewares:
133141
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+
```
138148
139149
This `stack` variable itself is now a valid middleware and has the same interface,
140150
so to execute the stack, just call `call` on it:
141151
142-
stack.call
152+
```ruby
153+
stack.call
154+
```
143155
144156
The call method takes an optional parameter which is the state to pass into the
145157
initial middleware.
@@ -152,37 +164,43 @@ created. Given the `stack` variable created above, we can manipulate it as
152164
follows. Please imagine that each example runs with the original `stack` variable,
153165
so that the order of the examples doesn't actually matter:
154166
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)
157170

158-
# Replace the lambda
159-
stack.replace(1, SomeOtherMiddleware)
171+
# Replace the lambda
172+
stack.replace(1, SomeOtherMiddleware)
160173

161-
# Delete the lambda
162-
stack.delete(1)
174+
# Delete the lambda
175+
stack.delete(1)
176+
```
163177

164178
### Passing Additional Constructor Arguments
165179

166180
When using middleware in a stack, you can also pass in additional constructor
167181
arguments. Given the following middleware:
168182

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
174189

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+
```
180196

181197
We can initialize `Echo` with a proper message as follows:
182198

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+
```
186204

187205
Then when the stack is called, it will output "Hello, World!"
188206

0 commit comments

Comments
 (0)