Module: Respect::Rails::RequestHelper

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) action_schema

Returns the value of attribute action_schema



54
55
56
# File 'lib/respect/rails/request_helper.rb', line 54

def action_schema
  @action_schema
end

Instance Method Details

- (Boolean) has_schema?

Returns:

  • (Boolean)


58
59
60
# File 'lib/respect/rails/request_helper.rb', line 58

def has_schema?
  !!action_schema
end

- (Object) last_validation_error



62
63
64
# File 'lib/respect/rails/request_helper.rb', line 62

def last_validation_error
  request_schema.last_error
end

- (Object) request_schema



50
51
52
# File 'lib/respect/rails/request_helper.rb', line 50

def request_schema
  action_schema.request if action_schema
end

- (Object) response_schema(http_status)



44
45
46
47
48
# File 'lib/respect/rails/request_helper.rb', line 44

def response_schema(http_status)
  unless action_schema.nil? || action_schema.responses.nil?
    action_schema.responses[http_status]
  end
end

- (Object) sane_params

Returns the sanitized parameters if the schema validation has succeed.



67
68
69
# File 'lib/respect/rails/request_helper.rb', line 67

def sane_params
  request_schema.sanitized_params if request_schema
end

- (Object) sanitize_params!

Sanitize all the request's parameters (path, query and body) in-place. if the schema validation has succeed. Validate it first if it has not been yet.



86
87
88
89
90
91
92
93
94
95
# File 'lib/respect/rails/request_helper.rb', line 86

def sanitize_params!
  if request_schema
    if validated.nil?
      validate_schema
    end
    if validated?
      request_schema.sanitize!(self)
    end
  end
end

- (Object) validate_schema

Raise a Respect::Rails::RequestValidationError exception if this request does not validate the schema.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/respect/rails/request_helper.rb', line 18

def validate_schema
  log_msg = "  Request validation: "
  @validated = nil
  measure = Benchmark.realtime do
    @validated = request_schema.validate?(self) unless request_schema.nil?
  end
  if @validated.nil?
    log_msg += "none"
  else
    if @validated == true
      log_msg += "success"
    else
      log_msg += "failure"
    end
  end
  log_msg += " (%.1fms)" % [ measure * 1000 ]
  ::Rails.logger.info log_msg
  if @validated == false
    last_validation_error.context.each do |msg|
      ::Rails.logger.info "    #{msg}"
    end
    raise last_validation_error
  end
  true
end

- (Boolean) validate_schema?

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

Returns:

  • (Boolean)


8
9
10
11
12
13
14
# File 'lib/respect/rails/request_helper.rb', line 8

def validate_schema?
  begin
    validate_schema
  rescue Respect::Rails::RequestValidationError
    false
  end
end

- (Object) validated

Returns nil if never validated, true if validated successfully and false if validation failed.



73
74
75
# File 'lib/respect/rails/request_helper.rb', line 73

def validated
  @validated
end

- (Boolean) validated?

Returns true if the validation has been done and succeed and false otherwise.

Returns:

  • (Boolean)


79
80
81
# File 'lib/respect/rails/request_helper.rb', line 79

def validated?
  !!validated
end