-
1
require 'sinatra/base'
-
1
require 'sinatra/param/version'
-
1
require 'date'
-
1
require 'time'
-
-
1
module Sinatra
-
1
module Param
-
1
Boolean = :boolean
-
-
1
class InvalidParameterError < StandardError
-
1
attr_accessor :param, :options
-
end
-
-
1
def param(name, type, options = {})
-
154
name = name.to_s
-
-
154
return unless params.member?(name) or options.has_key?(:default) or options[:required]
-
-
begin
-
109
params[name] = coerce(params[name], type, options)
-
102
params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if params[name].nil? and options.has_key?(:default)
-
102
params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform]
-
102
validate!(params[name], options)
-
82
params[name]
-
27
rescue InvalidParameterError => exception
-
27
if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
-
1
exception.param, exception.options = name, options
-
1
raise exception
-
end
-
-
26
error = options[:message] || exception.to_s
-
-
26
if content_type and content_type.match(mime_type(:json))
-
25
error = {message: error, errors: {name => exception.message}}.to_json
-
else
-
1
content_type 'text/plain'
-
end
-
-
26
halt 400, error
-
end
-
end
-
-
1
def one_of(*args)
-
17
options = args.last.is_a?(Hash) ? args.pop : {}
-
17
names = args.collect(&:to_s)
-
-
17
return unless names.length >= 2
-
-
begin
-
13
validate_one_of!(params, names, options)
-
5
rescue InvalidParameterError => exception
-
5
if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
-
1
exception.param, exception.options = names, options
-
1
raise exception
-
end
-
-
4
error = "Invalid parameters [#{names.join(', ')}]"
-
4
if content_type and content_type.match(mime_type(:json))
-
4
error = {message: error, errors: {names => exception.message}}.to_json
-
end
-
-
4
halt 400, error
-
end
-
end
-
-
1
def any_of(*args)
-
8
options = args.last.is_a?(Hash) ? args.pop : {}
-
8
names = args.collect(&:to_s)
-
-
8
return unless names.length >= 2
-
-
begin
-
8
validate_any_of!(params, names, options)
-
2
rescue InvalidParameterError => exception
-
2
if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
-
1
exception.param, exception.options = names, options
-
1
raise exception
-
end
-
-
1
error = "Invalid parameters [#{names.join(', ')}]"
-
1
if content_type and content_type.match(mime_type(:json))
-
1
error = {message: error, errors: {names => exception.message}}.to_json
-
end
-
-
1
halt 400, error
-
end
-
end
-
-
1
private
-
-
1
def coerce(param, type, options = {})
-
begin
-
109
return nil if param.nil?
-
98
return param if (param.is_a?(type) rescue false)
-
48
return Integer(param, 10) if type == Integer
-
37
return Float(param) if type == Float
-
35
return String(param) if type == String
-
34
return Date.parse(param) if type == Date
-
32
return Time.parse(param) if type == Time
-
30
return DateTime.parse(param) if type == DateTime
-
28
return Array(param.split(options[:delimiter] || ",")) if type == Array
-
27
return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
-
23
if [TrueClass, FalseClass, Boolean].include? type
-
22
coerced = /^(false|f|no|n|0)$/i === param.to_s ? false : /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
-
22
raise ArgumentError if coerced.nil?
-
20
return coerced
-
end
-
return nil
-
7
rescue ArgumentError
-
7
raise InvalidParameterError, "'#{param}' is not a valid #{type}"
-
end
-
end
-
-
1
def validate!(param, options)
-
102
options.each do |key, value|
-
52
case key
-
when :required
-
7
raise InvalidParameterError, "Parameter is required" if value && param.nil?
-
when :blank
-
5
raise InvalidParameterError, "Parameter cannot be blank" if !value && case param
-
when String
-
1
!(/\S/ === param)
-
when Array, Hash
-
2
param.empty?
-
else
-
1
param.nil?
-
end
-
when :format
-
3
raise InvalidParameterError, "Parameter must be a string if using the format validation" unless param.kind_of?(String)
-
2
raise InvalidParameterError, "Parameter must match format #{value}" unless param =~ value
-
when :is
-
2
raise InvalidParameterError, "Parameter must be #{value}" unless param === value
-
when :in, :within, :range
-
8
raise InvalidParameterError, "Parameter must be within #{value}" unless param.nil? || case value
-
when Range
-
4
value.include?(param)
-
else
-
2
Array(value).include?(param)
-
end
-
when :min
-
2
raise InvalidParameterError, "Parameter cannot be less than #{value}" unless param.nil? || value <= param
-
when :max
-
2
raise InvalidParameterError, "Parameter cannot be greater than #{value}" unless param.nil? || value >= param
-
when :min_length
-
1
raise InvalidParameterError, "Parameter cannot have length less than #{value}" unless param.nil? || value <= param.length
-
when :max_length
-
4
raise InvalidParameterError, "Parameter cannot have length greater than #{value}" unless param.nil? || value >= param.length
-
end
-
end
-
end
-
-
1
def validate_one_of!(params, names, options)
-
47
raise InvalidParameterError, "Only one of [#{names.join(', ')}] is allowed" if names.count{|name| present?(params[name])} > 1
-
end
-
-
1
def validate_any_of!(params, names, options)
-
32
raise InvalidParameterError, "One of parameters [#{names.join(', ')}] is required" if names.count{|name| present?(params[name])} < 1
-
end
-
-
# ActiveSupport #present? and #blank? without patching Object
-
1
def present?(object)
-
58
!blank?(object)
-
end
-
-
1
def blank?(object)
-
58
object.respond_to?(:empty?) ? object.empty? : !object
-
end
-
end
-
-
1
helpers Param
-
end