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.0'

Instance Method Summary collapse

Instance Method Details

#all_or_none_of(*args) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sinatra/param.rb', line 110

def all_or_none_of(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  names = args.collect(&:to_s)

 begin
    validate_all_or_none_of!(params, names, options)
  rescue InvalidParameterError => exception
    if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
      exception.param, exception.options = names, options
      raise exception
    end

    error = "Invalid parameters [#{names.join(', ')}]"
    if content_type and content_type.match(mime_type(:json))
      error = {message: error, errors: {names => exception.message}}.to_json
    end

    halt 400, error
  end
end

#any_of(*args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sinatra/param.rb', line 85

def any_of(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  names = args.collect(&:to_s)
  applicable_params = @applicable_params || params

  return unless names.length >= 2

  begin
    validate_any_of!(applicable_params, names, options)
  rescue InvalidParameterError => exception
    if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
      exception.param, exception.options = names, options
      raise exception
    end

    formatted_params = formatted_params(@parent_key_name, names)
    error = "Invalid parameters #{formatted_params}"
    if content_type and content_type.match(mime_type(:json))
      error = {message: error, errors: {formatted_params => exception.message}}.to_json
    end

    halt 400, error
  end
end

#one_of(*args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sinatra/param.rb', line 61

def one_of(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  names = args.collect(&:to_s)
  applicable_params = @applicable_params || params

  return unless names.length >= 2

  begin
    validate_one_of!(applicable_params, names, options)
  rescue InvalidParameterError => exception
    if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
      exception.param, exception.options = names, options
      raise exception
    end

    error = "Invalid parameters #{formatted_params(@parent_key_name, names)}"
    if content_type and content_type.match(mime_type(:json))
      error = {message: error, errors: {names => exception.message}}.to_json
    end

    halt 400, error
  end
end

#param(name, type, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sinatra/param.rb', line 14

def param(name, type, options = {})
  name = name.to_s
  applicable_params = @applicable_params || params

  return unless applicable_params.member?(name) or options.has_key?(:default) or options[:required]

  begin
    applicable_params[name] = coerce(applicable_params[name], type, options)
    applicable_params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if applicable_params[name].nil? and options.has_key?(:default)
    applicable_params[name] = options[:transform].to_proc.call(applicable_params[name]) if applicable_params[name] and options[:transform]
    validate!(applicable_params[name], options)

    if block_given?
      if type != Hash
        raise Sinatra::Param::InvalidParameterError.new(
          'Only the Hash parameter validation can use sub hash validation method')
      end
      original_applicable_params = @applicable_params
      original_parent_key_name = @parent_key_name
      @applicable_params = applicable_params[name]
      @parent_key_name = formatted_params(@parent_key_name, name)

      yield

      @applicable_params = original_applicable_params
      @parent_key_name = original_parent_key_name
    end

    applicable_params[name]
  rescue InvalidParameterError => exception
    exception_name = formatted_params(@parent_key_name, name)
    if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
      exception.param = exception_name
      exception.options = options
      raise exception
    end

    error = options[:message] || exception.to_s

    if content_type and content_type.match(mime_type(:json))
      error = {message: error, errors: {exception_name => exception.message}}.to_json
    end

    halt 400, error
  end
end