Module: Sinatra::Param
- Defined in:
- lib/sinatra/param.rb,
lib/sinatra/param/version.rb
Defined Under Namespace
Classes: InvalidParameterError
Constant Summary
collapse
- Boolean =
:boolean
- VERSION =
'1.0.1'
Instance Method Summary
collapse
Instance Method Details
#one_of(*names) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/sinatra/param.rb', line 27
def one_of(*names)
count = 0
names.each do |name|
if params[name] and present?(params[name])
count += 1
next unless count > 1
error = "Parameters #{names.join(', ')} are mutually exclusive"
if content_type and content_type.match(mime_type(:json))
error = {message: error}.to_json
end
halt 400, error
end
end
end
|
#param(name, type, options = {}) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/sinatra/param.rb', line 12
def param(name, type, options = {})
begin
params[name] = coerce(params[name], type, options) || options[:default]
params[name] = options[:transform].to_proc.call(params[name]) if options[:transform]
validate!(params[name], options)
rescue
error = "Invalid parameter, #{name}"
if content_type and content_type.match(mime_type(:json))
error = {message: error}.to_json
end
halt 400, error
end
end
|