Skip to content

Instantly share code, notes, and snippets.

@benolee
Forked from tenderlove/ugh.rb
Created April 25, 2013 01:30
Show Gist options
  • Save benolee/5456874 to your computer and use it in GitHub Desktop.
Save benolee/5456874 to your computer and use it in GitHub Desktop.
####
# 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