Class: Committee::Middleware::Stub

Inherits:
Base
  • Object
show all
Defined in:
lib/committee/middleware/stub.rb

Instance Method Summary collapse

Methods inherited from Base

#call

Constructor Details

#initialize(app, options = {}) ⇒ Stub

Returns a new instance of Stub.



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

def initialize(app, options={})
  super
  @cache = {}
  @call  = options[:call]
end

Instance Method Details

#handle(request) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/committee/middleware/stub.rb', line 9

def handle(request)
  if link = @router.find_request_link(request)
    headers = { "Content-Type" => "application/json" }
    data = cache(link.method, link.href) do
      Committee::ResponseGenerator.new.call(link)
    end
    if @call
      request.env["committee.response"] = data
      call_status, call_headers, call_body = @app.call(request.env)

      # a committee.suppress signal initiates a direct pass through
      if request.env["committee.suppress"] == true
        return call_status, call_headers, call_body
      end

      # otherwise keep the headers and whatever data manipulations were
      # made, and stub normally
      headers.merge!(call_headers)
    end
    status = link.rel == "create" ? 201 : 200
    [status, headers, [MultiJson.encode(data, pretty: true)]]
  else
    @app.call(request.env)
  end
end