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
# File 'lib/committee/middleware/stub.rb', line 10

def call(env)
  request = Rack::Request.new(env)
  link_schema, type_schema = @router.routes_request?(request, prefix: @prefix)
  if type_schema
    headers = { "Content-Type" => "application/json" }
    data = cache(link_schema["method"], link_schema["href"]) do
      Committee::ResponseGenerator.new(@schema, type_schema, link_schema).call
    end
    if @call
      env["committee.response"] = data
      _, call_headers, _ = @app.call(env)
      headers.merge!(call_headers)
    end
    status = link_schema["rel"] == "create" ? 201 : 200
    [status, headers, [MultiJson.encode(data, pretty: true)]]
  else
    @app.call(env)
  end
end