All Files
(100.0%
covered at
18.4
hits/line)
1 files in total.
64 relevant lines.
64 lines covered and
0 lines missed
File |
% covered |
Lines |
Relevant Lines |
Lines covered |
Lines missed |
Avg. Hits / Line |
lib/sinatra/param.rb |
100.0 % |
112 |
64 |
64 |
0 |
18.4 |
-
1
require 'sinatra/base'
-
1
require 'sinatra/param/version'
-
1
require 'time'
-
1
require 'date'
-
-
1
module Sinatra
-
1
module Param
-
1
Boolean = :boolean
-
-
1
class InvalidParameterError < StandardError; end
-
-
1
def param(name, type, options = {})
-
76
name = name.to_s
-
-
76
return unless params.member?(name) or present?(options[:default]) or options[:required]
-
-
67
begin
-
67
params[name] = coerce(params[name], type, options)
-
67
params[name] = options[:default] if params[name].nil? and options[:default]
-
67
params[name] = options[:transform].to_proc.call(params[name]) if options[:transform]
-
67
validate!(params[name], options)
-
rescue
-
13
error = "Invalid parameter, #{name}"
-
13
if content_type and content_type.match(mime_type(:json))
-
13
error = {message: error}.to_json
-
end
-
-
13
halt 400, error
-
end
-
end
-
-
1
def one_of(*names)
-
6
count = 0
-
6
names.each do |name|
-
16
if params[name] and present?(params[name])
-
9
count += 1
-
9
next unless count > 1
-
-
3
error = "Parameters #{names.join(', ')} are mutually exclusive"
-
3
if content_type and content_type.match(mime_type(:json))
-
3
error = {message: error}.to_json
-
end
-
-
3
halt 400, error
-
end
-
end
-
end
-
-
1
private
-
-
1
def coerce(param, type, options = {})
-
67
return nil if param.nil?
-
64
return param if (param.is_a?(type) rescue false)
-
39
return Integer(param) if type == Integer
-
30
return Float(param) if type == Float
-
29
return String(param) if type == String
-
28
return Time.parse(param) if type == Time
-
27
return Date.parse(param) if type == Date
-
26
return DateTime.parse(param) if type == DateTime
-
25
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
-
end
-
-
1
def validate!(param, options)
-
67
options.each do |key, value|
-
38
case key
-
when :required
-
3
raise InvalidParameterError if value && param.nil?
-
when :blank
-
raise InvalidParameterError if !value && case param
-
when String
-
1
!(/\S/ === param)
-
when Array, Hash
-
2
param.empty?
-
else
-
1
param.nil?
-
5
end
-
when :is
-
2
raise InvalidParameterError unless value === param
-
when :in, :within, :range
-
raise InvalidParameterError unless param.nil? || case value
-
when Range
-
4
value.include?(param)
-
else
-
2
Array(value).include?(param)
-
6
end
-
when :min
-
2
raise InvalidParameterError unless param.nil? || value <= param
-
when :max
-
2
raise InvalidParameterError unless param.nil? || value >= param
-
when :min_length
-
1
raise InvalidParameterError unless param.nil? || value <= param.length
-
when :max_length
-
3
raise InvalidParameterError unless param.nil? || value >= param.length
-
end
-
end
-
end
-
-
# ActiveSupport #present? and #blank? without patching Object
-
1
def present?(object)
-
21
!blank?(object)
-
end
-
-
1
def blank?(object)
-
21
object.respond_to?(:empty?) ? object.empty? : !object
-
end
-
end
-
-
1
helpers Param
-
end