-
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 = {})
-
123
name = name.to_s
-
-
123
return unless params.member?(name) or options[:default] or options[:required]
-
-
92
begin
-
92
params[name] = coerce(params[name], type, options)
-
87
params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if params[name].nil? and options[:default]
-
87
params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform]
-
87
validate!(params[name], options)
-
22
rescue InvalidParameterError => exception
-
22
if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
-
1
exception.param, exception.options = name, options
-
1
raise exception
-
end
-
-
21
error = "Invalid Parameter: #{name}"
-
21
if content_type and content_type.match(mime_type(:json))
-
21
error = {message: error, errors: {name => exception.message}}.to_json
-
end
-
-
21
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
-
-
13
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 = "Parameters #{names.join(', ')} are mutually exclusive"
-
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
private
-
-
1
def coerce(param, type, options = {})
-
92
begin
-
92
return nil if param.nil?
-
85
return param if (param.is_a?(type) rescue false)
-
46
return Integer(param) if type == Integer
-
35
return Float(param) if type == Float
-
33
return String(param) if type == String
-
32
return Date.parse(param) if type == Date
-
30
return Time.parse(param) if type == Time
-
28
return DateTime.parse(param) if type == DateTime
-
26
return Array(param.split(options[:delimiter] || ",")) if type == Array
-
25
return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
-
21
return (/(false|f|no|n|0)$/i === param.to_s ? false : (/(true|t|yes|y|1)$/i === param.to_s ? true : nil)) if type == TrueClass || type == FalseClass || type == Boolean
-
return nil
-
5
rescue ArgumentError
-
5
raise InvalidParameterError, "'#{param}' is not a valid #{type}"
-
end
-
end
-
-
1
def validate!(param, options)
-
87
options.each do |key, value|
-
45
case key
-
when :required
-
5
raise InvalidParameterError, "Parameter is required" if value && param.nil?
-
when :blank
-
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?
-
5
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
-
raise InvalidParameterError, "Parameter must be within #{value}" unless param.nil? || case value
-
when Range
-
4
value.include?(param)
-
else
-
2
Array(value).include?(param)
-
6
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
-
3
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, "Parameters #{names.join(', ')} are mutually exclusive" if names.count{|name| present?(params[name])} > 1
-
end
-
-
# ActiveSupport #present? and #blank? without patching Object
-
1
def present?(object)
-
34
!blank?(object)
-
end
-
-
1
def blank?(object)
-
34
object.respond_to?(:empty?) ? object.empty? : !object
-
end
-
end
-
-
1
helpers Param
-
end