Module: Respect::Rails::ResponseHelper

Extended by:
ActiveSupport::Concern
Defined in:
lib/respect/rails/response_helper.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) schema

Returns the value of attribute schema



6
7
8
# File 'lib/respect/rails/response_helper.rb', line 6

def schema
  @schema
end

Instance Method Details

- (Boolean) has_schema?

Returns:

  • (Boolean)


10
11
12
# File 'lib/respect/rails/response_helper.rb', line 10

def has_schema?
  !!@schema
end

- (Object) last_validation_error



59
60
61
# File 'lib/respect/rails/response_helper.rb', line 59

def last_validation_error
  schema.last_error
end

- (Object) validate_schema

Raise a Respect::Rails::ResponseValidationError exception if this response does not validate the schema.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/respect/rails/response_helper.rb', line 26

def validate_schema
  log_msg = "  Response validation: "
  valid = nil
  measure = Benchmark.realtime do
    if schema && content_type == Mime::JSON
      valid = schema.validate?(self)
    end
  end
  if valid.nil?
    log_msg += "none"
  else
    if valid
      log_msg += "success"
    else
      log_msg += "failure"
    end
  end
  log_msg += " (%.1fms)" % [ measure * 1000 ]
  ::Rails.logger.info log_msg
  if valid == false
    last_validation_error.context.each do |msg|
      ::Rails.logger.info "    #{msg}"
    end
    if Respect::Rails::Engine.catch_response_validation_error
      self.body = last_validation_error.to_json
      self.status = :internal_server_error
    else
      raise last_validation_error
    end
  end
  true
end

- (Boolean) validate_schema?

Return whether this response validates the schema. You can get the validation error via #last_validation_error.

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'lib/respect/rails/response_helper.rb', line 16

def validate_schema?
  begin
    validate_schema
  rescue Respect::Rails::ResponseValidationError => e
    false
  end
end