Class: Sinatra::Swagger::ParamValidator::ValidityParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/swagger/param_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, params, captures, spec, definitions) ⇒ ValidityParser

Returns a new instance of ValidityParser



60
61
62
63
64
65
66
# File 'lib/sinatra/swagger/param_validator.rb', line 60

def initialize(request, params, captures, spec, definitions)
  @request = request
  @params = params
  @captures = captures
  @spec = spec
  @definitions = definitions
end

Instance Attribute Details

#capturesObject (readonly)

Returns the value of attribute captures



58
59
60
# File 'lib/sinatra/swagger/param_validator.rb', line 58

def captures
  @captures
end

#definitionsObject (readonly)

Returns the value of attribute definitions



58
59
60
# File 'lib/sinatra/swagger/param_validator.rb', line 58

def definitions
  @definitions
end

#paramsObject (readonly)

Returns the value of attribute params



58
59
60
# File 'lib/sinatra/swagger/param_validator.rb', line 58

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request



58
59
60
# File 'lib/sinatra/swagger/param_validator.rb', line 58

def request
  @request
end

#specObject (readonly)

Returns the value of attribute spec



58
59
60
# File 'lib/sinatra/swagger/param_validator.rb', line 58

def spec
  @spec
end

Instance Method Details

#invaliditiesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sinatra/swagger/param_validator.rb', line 72

def invalidities
  return @invalidities unless @invalidities.nil?
  unknown_params = params.keys.select { |k| k.is_a?(String) }

  @invalidities = Hash[(spec['parameters'] || []).map { |details|
    param_name = details['name']
    unknown_params.delete(param_name)

    case details['in']
    when "body"
      body = params[:body]
      next check_json_body(body, param_name, details)

    when "query", "formData"
      parameter = params[param_name]

    when "header"
      normalised_header_name = param_name.tr("-", "_").upcase
      parameter = request.env["HTTP_#{normalised_header_name}"]

    when "path"
      parameter = captures[param_name]

    else
      # other param types aren't dealt with at the moment
      next
    end

    next check_parameter(parameter, param_name, details)
  }.compact]

  unknown_params.each { |p| @invalidities[p] = :unexpected_key } if @invalidities.any?

  @invalidities
end

#invalidities?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/sinatra/swagger/param_validator.rb', line 68

def invalidities?
  invalidities.any?
end