Class: Committee::Middleware::Base

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

Direct Known Subclasses

RequestValidation, ResponseValidation, Stub

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/committee/middleware/base.rb', line 3

def initialize(app, options={})
  @app = app

  @error_class = options.fetch(:error_class, Committee::ValidationError)
  @params_key = options[:params_key] || "committee.params"
  @raise = options[:raise]

  schema = options[:schema] || raise("need option `schema`")
  if schema.is_a?(String)
    warn_string_deprecated
    schema = JSON.parse(schema)
  end
  if schema.is_a?(Hash)
    schema = JsonSchema.parse!(schema)
    schema.expand_references!
  end
  @schema = schema

  @router = Committee::Router.new(@schema, options)
end

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/committee/middleware/base.rb', line 24

def call(env)
  request = Rack::Request.new(env)

  if @router.includes_request?(request)
    handle(request)
  else
    @app.call(request.env)
  end
end