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

def initialize(app, options={})
  super
  @cache = {}
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  request = Rack::Request.new(env)
  link_schema, type_schema = @router.routes_request?(request)
  if type_schema
    str = cache(link_schema["method"], link_schema["href"]) do
      data = Committee::ResponseGenerator.new(@schema, type_schema, link_schema).call
      MultiJson.encode(data, pretty: true)
    end
    [200, { "Content-Type" => "application/json" }, [str]]
  else
    @app.call(env)
  end
end