Class: Isono::Rack::Proc

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Logger

included, initialize

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

#contextObject

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