Class: Committee::Middleware::Base

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

Direct Known Subclasses

RequestValidation, ResponseValidation, Stub

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/committee/middleware/base.rb', line 6

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

  @error_class = options.fetch(:error_class, Committee::ValidationError)
  @error_handler = options[:error_handler]
  @ignore_error = options.fetch(:ignore_error, false)

  @raise = options[:raise]
  @schema = self.class.get_schema(options)

  @router = @schema.build_router(options)
  @accept_request_filter = options[:accept_request_filter] || -> (_) { true }
end

Class Method Details

.get_schema(options) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/committee/middleware/base.rb', line 31

def get_schema(options)
  schema = options[:schema]
  if !schema && options[:schema_path]
    # In the future, we could have `parser_options` as an exposed config?
    parser_options = options.key?(:strict_reference_validation) ? { strict_reference_validation: options[:strict_reference_validation] } : {}
    schema = Committee::Drivers::load_from_file(options[:schema_path], parser_options: parser_options)
  end
  raise(ArgumentError, "Committee: need option `schema` or `schema_path`") unless schema

  # Expect the type we want by now. If we don't have it, the user passed
  # something else non-standard in.
  if !schema.is_a?(Committee::Drivers::Schema)
    raise ArgumentError, "Committee: schema expected to be an instance of Committee::Drivers::Schema."
  end

  return schema
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/committee/middleware/base.rb', line 20

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

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