Class: Pistachio::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/pistachio/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
6
7
# File 'lib/pistachio/middleware.rb', line 3

def initialize(app)
  # Matches messages that should be processed by Pistachio
  @path_regex = /^#{Pistachio.path}\/(\w+)$/
  @timeout = Pistachio.timeout
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pistachio/middleware.rb', line 9

def call(env)
  request = Rack::Request.new(env)
  result = request.path.match(@path_regex) 
  return unless result

  endpoint = result[1]
  begin
    message = Stash::List[endpoint].bshift @timeout
    [200, {'Content-Type' => 'text/plain'}, message]
  rescue Stash::TimeoutError
    [504, {'Content-Type' => 'text/plain'}, "Timeout waiting for response"]
  end
end