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



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

def get_schema(options)
  schema = options[:schema]
  unless schema
    schema = Committee::Drivers::load_from_file(options[:schema_path]) if options[:schema_path]

    raise(ArgumentError, "Committee: need option `schema` or `schema_path`") unless schema
  end

  # 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