-
-
Save benolee/5456874 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### | |
# It must output this: | |
# | |
# hello one two | |
# world one two | |
# | |
class Filter | |
def initialize name | |
@name = name | |
end | |
def call app, request | |
app.call(request) do |body, status, headers| | |
yield ["#{body} #{@name}", status, headers] | |
end | |
end | |
end | |
class App | |
def call request | |
yield ["hello", 200, {}] | |
yield ["world", nil, nil] | |
end | |
end | |
class Context | |
def initialize(app, filters) | |
@chain = filters.inject(app) do |a, filter| | |
lambda do |request, &block| | |
filter.call(a, request, &block) | |
end | |
end | |
end | |
def serve request | |
@chain.call(request) do |triple| | |
write triple.first | |
end | |
end | |
def write str | |
puts str | |
end | |
end | |
app = App.new | |
filters = [Filter.new("one"), Filter.new("two")] | |
ctx = Context.new(app, filters) | |
ctx.serve({}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment