Class: Committee::Middleware::ResponseValidation

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#call, get_schema

Constructor Details

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

Returns a new instance of ResponseValidation.



8
9
10
11
12
# File 'lib/committee/middleware/response_validation.rb', line 8

def initialize(app, options = {})
  super
  @strict = options[:strict]
  @validate_success_only = @schema.validator_option.validate_success_only
end

Instance Attribute Details

#validate_success_onlyObject (readonly)

Returns the value of attribute validate_success_only.



6
7
8
# File 'lib/committee/middleware/response_validation.rb', line 6

def validate_success_only
  @validate_success_only
end

Class Method Details

.validate?(status, validate_success_only) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/committee/middleware/response_validation.rb', line 37

def validate?(status, validate_success_only)
  case status
  when 204
    false
  when 200..299
    true
  when 304
    false
  else
    !validate_success_only
  end
end

Instance Method Details

#handle(request) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/committee/middleware/response_validation.rb', line 14

def handle(request)
  status, headers, response = @app.call(request.env)

  begin
    v = build_schema_validator(request)
    v.response_validate(status, headers, response, @strict) if v.link_exist? && self.class.validate?(status, validate_success_only)

  rescue Committee::InvalidResponse
    handle_exception($!, request.env)

    raise if @raise
    return @error_class.new(500, :invalid_response, $!.message).render unless @ignore_error
  rescue JSON::ParserError
    handle_exception($!, request.env)

    raise Committee::InvalidResponse if @raise
    return @error_class.new(500, :invalid_response, "Response wasn't valid JSON.").render unless @ignore_error
  end

  [status, headers, response]
end