Module: Goliath::Rack::SimpleAroundware
- Includes:
- Validator
- Included in:
- BarrierAroundware
- Defined in:
- lib/goliath/rack/simple_aroundware.rb
Overview
This module gives you ergonomics similar to traditional Rack middleware:
-
Use instance variables! Each SimpleAroundware is unique to its request.
-
You have accessors for env and (once in post_process) status, headers, body – no more shipping them around to every method.
If in your traditional rack middleware you’d do this:
class MyRackMiddleware
def call(env)
get_ready_to_be_totally_awesome()
status, headers, body = @app.call(env)
new_body = make_totally_awesome(body)
[status, headers, new_body]
end
end
You’d now do this:
class MyAwesomeAroundware
include Goliath::Rack::SimpleAroundware
def pre_process
get_ready_to_be_totally_awesome()
end
def post_process
new_body = make_totally_awesome(body)
[status, headers, new_body]
end
end
And you’d include it in your endpoint like this:
class AwesomeApi < Goliath::API
use Goliath::Rack::SimpleAroundwareFactory, MyAwesomeAroundware
end
Instance Attribute Summary collapse
-
#body ⇒ Object
The response, set by the SimpleAroundware’s downstream.
-
#env ⇒ Object
readonly
The request environment, set in the initializer.
-
#headers ⇒ Object
The response, set by the SimpleAroundware’s downstream.
-
#status ⇒ Object
The response, set by the SimpleAroundware’s downstream.
Instance Method Summary collapse
-
#accept_response(handle, resp_succ, resp) ⇒ Object
On receipt of an async result, * call the setter for that handle if any (on receipt of :shortened_url,.
-
#downstream_resp=(status_headers_body) ⇒ Object
Virtual setter for the downstream middleware/endpoint response.
- #initialize(env) ⇒ Goliath::Rack::SimpleAroundware
-
#post_process ⇒ Array
Override this method in your middleware to perform any postprocessing.
-
#pre_process ⇒ Array
Override this method in your middleware to perform any preprocessing (launching a deferred request, perhaps).
Methods included from Validator
Instance Attribute Details
#body ⇒ Object
The response, set by the SimpleAroundware’s downstream
74 75 76 |
# File 'lib/goliath/rack/simple_aroundware.rb', line 74 def body @body end |
#env ⇒ Object (readonly)
The request environment, set in the initializer
72 73 74 |
# File 'lib/goliath/rack/simple_aroundware.rb', line 72 def env @env end |
#headers ⇒ Object
The response, set by the SimpleAroundware’s downstream
74 75 76 |
# File 'lib/goliath/rack/simple_aroundware.rb', line 74 def headers @headers end |
#status ⇒ Object
The response, set by the SimpleAroundware’s downstream
74 75 76 |
# File 'lib/goliath/rack/simple_aroundware.rb', line 74 def status @status end |
Instance Method Details
#accept_response(handle, resp_succ, resp) ⇒ Object
On receipt of an async result,
-
call the setter for that handle if any (on receipt of :shortened_url,
108 109 110 |
# File 'lib/goliath/rack/simple_aroundware.rb', line 108 def accept_response(handle, resp_succ, resp) self.downstream_resp = resp end |
#downstream_resp=(status_headers_body) ⇒ Object
Virtual setter for the downstream middleware/endpoint response
102 103 104 |
# File 'lib/goliath/rack/simple_aroundware.rb', line 102 def downstream_resp=(status_headers_body) @status, @headers, @body = status_headers_body end |
#initialize(env) ⇒ Goliath::Rack::SimpleAroundware
78 79 80 |
# File 'lib/goliath/rack/simple_aroundware.rb', line 78 def initialize(env) @env = env end |
#post_process ⇒ Array
Override this method in your middleware to perform any postprocessing. This will only be invoked when all deferred requests (including the response) have completed.
97 98 99 |
# File 'lib/goliath/rack/simple_aroundware.rb', line 97 def post_process [status, headers, body] end |
#pre_process ⇒ Array
Override this method in your middleware to perform any preprocessing (launching a deferred request, perhaps).
You must return Goliath::Connection::AsyncResponse if you want processing to continue
88 89 90 |
# File 'lib/goliath/rack/simple_aroundware.rb', line 88 def pre_process Goliath::Connection::AsyncResponse end |