Class: Committee::Middleware::Stub

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Stub.



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

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

Instance Method Details

#call(env) ⇒ Object



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

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

      # a committee.suppress signal initiates a direct pass through
      if 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(env)
  end
end