despecable 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8bd312e2dbfeb02c7d6f895e360225eee7b6f9ab
4
- data.tar.gz: 3d2d7fa1ef7c0690ae947dd03afbe874f1818718
3
+ metadata.gz: 844167e9ba0dad9d3420d9da0055e5f03429a173
4
+ data.tar.gz: c6e592b5c7b15ff362e6544435313f55a842a595
5
5
  SHA512:
6
- metadata.gz: fd6525433243bf037bd0fbced8ee13f01832a016c0942a741dfbf27dbe47a914e3f56aff1dfa0406b13051a2c5d41e97914586ebde487871c7958929d02eebee
7
- data.tar.gz: f3abcf704a9d4226e20ec91d7abc924c4cabd552d0d9341c25e8f56a137a3627ffcd5b98d71c2ac39949275335404ec3c269d5132b98d288abfa4dbedfb84703
6
+ metadata.gz: b3e960887d49d22504ac2d2085a0719dec32ea74dcb0750f780b855399635c0f1e54acbf92d0f266cd76831071e23ab8b81fc511a413ac4f61608db294464042
7
+ data.tar.gz: 789378ab03e1ff0e829c36255c8d6b76c0826124db5737012e60bf7fc1749cf643d46882f064513c1ff2b7e884b1aac454ad489c1b111a4487db2133900cbf68
@@ -10,44 +10,77 @@ module Despecable
10
10
  end
11
11
 
12
12
  def read(name, value, type, options)
13
- value = public_send(type, name, value) unless value.nil?
13
+ value = public_send(type, name, value, options) unless value.nil?
14
14
  validate_param(name, value, options)
15
15
  return value
16
16
  end
17
17
 
18
18
  def validate_param(name, value, options)
19
19
  validate_param_presence(name, value) if options[:required]
20
- validate_param_value(name, value, options[:in]) if options.key?(:in) && !value.nil?
20
+ validate_param_value(name, value, options) if options.key?(:in) && !value.nil?
21
21
  end
22
22
 
23
23
  def validate_param_presence(name, value)
24
24
  raise Despecable::MissingParameterError.new("Missing required parameter: '#{name}'", parameters: name) if value.nil?
25
25
  end
26
26
 
27
- def validate_param_value(name, value, allowed_values)
28
- raise Despecable::IncorrectParameterError.new("Unacceptable value for parameter: '#{name}'", parameters: name) if !allowed_values.include?(value)
27
+ def validate_param_value(name, value, options)
28
+ allowed_values = options[:in]
29
+ allowed_values = allowed_values.map{|x| x.is_a?(String) ? x.downcase : x} if options[:case] == false
30
+ value = value.downcase if options[:case] == false && value.is_a?(String)
31
+ if !allowed_values.include?(value)
32
+ msg = "Unacceptable value for parameter: '#{name}'"
33
+ allowed_values_message = expected_values_message(allowed_values)
34
+ msg += "; acceptable values are" + allowed_values_message if !allowed_values_message.nil?
35
+ raise Despecable::IncorrectParameterError.new(msg, parameters: name)
36
+ end
37
+ end
38
+
39
+ def validate_string_length(name, value, options)
40
+ allowed_lengths = options[:length]
41
+ allowed_lengths = [*allowed_lengths] unless allowed_lengths.is_a?(Range)
42
+ if !allowed_lengths.include?(value.length)
43
+ msg = "Unacceptable length for parameter: '#{name}'"
44
+ allowed_values_message = expected_values_message(allowed_lengths)
45
+ msg += "; acceptable lengths are" + allowed_values_message if !allowed_values_message.nil?
46
+ raise Despecable::IncorrectParameterError.new(msg, parameters: name)
47
+ end
29
48
  end
30
49
 
31
- def integer(name, value)
50
+ def expected_values_message(allowed_values)
51
+ case allowed_values
52
+ when Array
53
+ msg = ": " + allowed_values.slice(0..99).join(", ")
54
+ msg += ", ... [truncated]" if allowed_values.length > 100
55
+ return msg
56
+ when Range
57
+ " between #{allowed_values.first} and #{allowed_values.last}"
58
+ else
59
+ nil
60
+ end
61
+ end
62
+
63
+ def integer(name, value, options)
32
64
  Integer(value)
33
65
  rescue ArgumentError
34
66
  raise unless /^invalid value for Integer/ =~ $!.message
35
67
  raise Despecable::InvalidParameterError.new("Integer type required for parameter: '#{name}'", parameters: name)
36
68
  end
37
69
 
38
- def float(name, value)
70
+ def float(name, value, options)
39
71
  Float(value)
40
72
  rescue ArgumentError
41
73
  raise unless /^invalid value for Float/ =~ $!.message
42
74
  raise Despecable::InvalidParameterError.new("Float type required for parameter: '#{name}'", parameters: name)
43
75
  end
44
76
 
45
- def string(name, value)
46
- # This is really a no-op.
47
- value.to_s
77
+ def string(name, value, options)
78
+ value = value.to_s #no-op
79
+ validate_string_length(name, value, options) if options.key?(:length)
80
+ return value
48
81
  end
49
82
 
50
- def boolean(name, value)
83
+ def boolean(name, value, options)
51
84
  case value.to_s
52
85
  when "true", "1" then true
53
86
  when "false", "0", nil then false
@@ -55,26 +88,26 @@ module Despecable
55
88
  end
56
89
  end
57
90
 
58
- def date(name, value)
91
+ def date(name, value, options)
59
92
  Date.rfc3339(value + "T00:00:00+00:00")
60
93
  rescue ArgumentError
61
94
  raise unless $!.message == "invalid date"
62
95
  raise Despecable::InvalidParameterError.new("Date type (e.g. '2012-12-31') required for parameter: '#{name}'", parameters: name)
63
96
  end
64
97
 
65
- def datetime(name, value)
98
+ def datetime(name, value, options)
66
99
  DateTime.rfc3339(value)
67
100
  rescue ArgumentError
68
101
  raise unless $!.message == "invalid date"
69
102
  raise Despecable::InvalidParameterError.new("Rfc3339 datetime type (e.g. '2009-06-19T00:00:00-04:00') required for parameter: '#{name}'", parameters: name)
70
103
  end
71
104
 
72
- def file(name, value)
73
- raise Despecable::InvalidParameterError.new("File upload type required for parameter: '#{name}'" , parameters: name) if !(value.respond_to?(:original_filename) && value.original_filename.present?)
105
+ def file(name, value, options)
106
+ raise Despecable::InvalidParameterError.new("File upload type required for parameter: '#{name}'" , parameters: name) if !(value.respond_to?(:original_filename) && value.original_filename.to_s.length > 0)
74
107
  return value
75
108
  end
76
109
 
77
- def any(name, value)
110
+ def any(name, value, options)
78
111
  value
79
112
  end
80
113
  end
@@ -44,7 +44,7 @@ module Despecable
44
44
 
45
45
  def _spec(name, type, options = {})
46
46
  @specd << (name)
47
- if !params.key?(name) && options[:default]
47
+ if !params.key?(name) && options.key?(:default)
48
48
  params[name] = options[:default]
49
49
  elsif options[:array]
50
50
  values = @spectacles.arrayify(params[name])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: despecable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Schwartz