Class: Isono::Rack::Proc
- Inherits:
-
Object
- Object
- Isono::Rack::Proc
- Includes:
- Logger
- Defined in:
- lib/isono/rack/proc.rb
Defined Under Namespace
Modules: InjectMethods
Constant Summary collapse
- THREAD_LOCAL_KEY =
self.to_s
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
Instance Method Summary collapse
- #call(req, res) ⇒ Object
-
#initialize(context = nil, opts = {}, &blk) ⇒ Proc
constructor
A new instance of Proc.
Methods included from Logger
Constructor Details
#initialize(context = nil, opts = {}, &blk) ⇒ Proc
Returns a new instance of Proc.
12 13 14 15 |
# File 'lib/isono/rack/proc.rb', line 12 def initialize(context=nil, opts={}, &blk) @context = context || Object.new @blk = blk end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
10 11 12 |
# File 'lib/isono/rack/proc.rb', line 10 def context @context end |
Instance Method Details
#call(req, res) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/isono/rack/proc.rb', line 17 def call(req, res) Thread.current["#{THREAD_LOCAL_KEY}/request"] = req Thread.current["#{THREAD_LOCAL_KEY}/response"] = res begin # create per-request context object from original. c = @context.dup c.extend InjectMethods begin c.instance_eval(&@blk) # send empty message back to client if the response is not handled in block. res.response(nil) unless res.responded? rescue ::Exception => e res.response(e) unless res.responded? raise e end ensure Thread.current["#{THREAD_LOCAL_KEY}/request"] = nil Thread.current["#{THREAD_LOCAL_KEY}/response"] = nil end end |