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
19
# File 'lib/committee/middleware/base.rb', line 6

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

  @error_class = @options.error_class
  @error_handler = @options.error_handler
  @ignore_error = @options.ignore_error

  @raise = @options.raise_error
  @schema = self.class.get_schema(options)

  @router = @schema.build_router(options)
  @accept_request_filter = @options.accept_request_filter
end

Class Method Details

.get_schema(options) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

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