despecable 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/despecable/errors/despecable_error.rb +15 -1
- data/lib/despecable/errors/incorrect_parameter_error.rb +5 -1
- data/lib/despecable/errors/invalid_parameter_error.rb +5 -1
- data/lib/despecable/errors/missing_parameter_error.rb +5 -1
- data/lib/despecable/spectacles.rb +36 -26
- data/lib/despecable/spectator.rb +10 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 269604b0ffb17ef578acfa582c7842445bed4c4c
|
4
|
+
data.tar.gz: 7a9f3d1fbee596ef848848b77faaf5cf535b1bc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e207aaec581f73bd98f52c01e9e2906cd1a784b86dcf682b51f67923d1df59f7ae6a16a9426684b8c1e9696c9d37f2a6bb09410d02afb82905059d15df26ef9c
|
7
|
+
data.tar.gz: d2b3115499c5418afa3a13589fa1f3a049fbc64c2aca21c830774c2f5897791c98c048842fbe07def6322922e7580efa5fdd6850a9dada7fb7d027d448a45e45
|
@@ -1 +1,15 @@
|
|
1
|
-
Despecable::DespecableError
|
1
|
+
class Despecable::DespecableError < StandardError
|
2
|
+
def despecable_message
|
3
|
+
message == self.class.to_s ? nil : message
|
4
|
+
end
|
5
|
+
|
6
|
+
def insert_name_here(name)
|
7
|
+
msg = introduction(name)
|
8
|
+
msg += " " + despecable_message unless despecable_message.nil?
|
9
|
+
return exception(msg)
|
10
|
+
end
|
11
|
+
|
12
|
+
def introduction(name)
|
13
|
+
"Invalid value for paramter #{name}."
|
14
|
+
end
|
15
|
+
end
|
@@ -1,55 +1,65 @@
|
|
1
1
|
module Despecable
|
2
2
|
class Spectacles
|
3
|
-
|
4
|
-
|
5
|
-
def initialize(params)
|
6
|
-
@params = params
|
3
|
+
def arrayable?(value)
|
4
|
+
value.is_a?(::Array) || /,/ =~ value.to_s
|
7
5
|
end
|
8
6
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
7
|
+
def arrayify(value)
|
8
|
+
return value if value.is_a?(Array)
|
9
|
+
value.to_s.split(",")
|
12
10
|
end
|
13
11
|
|
14
|
-
def
|
15
|
-
|
12
|
+
def read(value, type, options)
|
13
|
+
value = public_send(type, value) unless value.nil?
|
14
|
+
validate_param(value, options)
|
15
|
+
return value
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
19
|
-
|
18
|
+
def validate_param(value, options)
|
19
|
+
validate_param_presence(value) if options[:required]
|
20
|
+
validate_param_value(value, options[:in]) if options.key?(:in) && !value.nil?
|
20
21
|
end
|
21
22
|
|
22
|
-
def
|
23
|
-
|
24
|
-
raise Despecable::InvalidParameterError, "Invalid value for param: '#{name}'. Required type: integer." if int.to_s != params[name].sub(/^0+/, "")
|
25
|
-
return int
|
23
|
+
def validate_param_presence(value)
|
24
|
+
raise Despecable::MissingParameterError if value.nil?
|
26
25
|
end
|
27
26
|
|
28
|
-
def
|
27
|
+
def validate_param_value(value, allowed_values)
|
28
|
+
raise Despecable::IncorrectParameterError, "Value received: #{value}." if !allowed_values.include?(value)
|
29
|
+
end
|
30
|
+
|
31
|
+
def integer(value)
|
32
|
+
Integer(value)
|
33
|
+
rescue ArgumentError
|
34
|
+
raise unless /^invalid value for Integer/ =~ $!.message
|
35
|
+
raise Despecable::InvalidParameterError, "Required type: integer."
|
36
|
+
end
|
37
|
+
|
38
|
+
def string(value)
|
29
39
|
# This is really a no-op.
|
30
|
-
|
40
|
+
value.to_s
|
31
41
|
end
|
32
42
|
|
33
|
-
def boolean(
|
34
|
-
case
|
43
|
+
def boolean(value)
|
44
|
+
case value.to_s
|
35
45
|
when "true", "1" then true
|
36
46
|
when "false", "0", nil then false
|
37
|
-
else raise Despecable::InvalidParameterError, "
|
47
|
+
else raise Despecable::InvalidParameterError, "Require type: boolean (1/0 or true/false)"
|
38
48
|
end
|
39
49
|
end
|
40
50
|
|
41
|
-
def date(
|
42
|
-
Date.rfc3339(
|
51
|
+
def date(value)
|
52
|
+
Date.rfc3339(value + "T00:00:00+00:00")
|
43
53
|
rescue ArgumentError
|
44
54
|
raise unless $!.message == "invalid date"
|
45
|
-
raise Despecable::InvalidParameterError, "
|
55
|
+
raise Despecable::InvalidParameterError, "Required type: date (e.g. '2012-12-31')."
|
46
56
|
end
|
47
57
|
|
48
|
-
def datetime(
|
49
|
-
DateTime.rfc3339(
|
58
|
+
def datetime(value)
|
59
|
+
DateTime.rfc3339(value)
|
50
60
|
rescue ArgumentError
|
51
61
|
raise unless $!.message == "invalid date"
|
52
|
-
raise Despecable::InvalidParameterError, "
|
62
|
+
raise Despecable::InvalidParameterError, "Required type: rfc3339 datetime (e.g. '2009-06-19T00:00:00-04:00')."
|
53
63
|
end
|
54
64
|
end
|
55
65
|
end
|
data/lib/despecable/spectator.rb
CHANGED
@@ -4,7 +4,7 @@ module Despecable
|
|
4
4
|
|
5
5
|
def initialize(params)
|
6
6
|
@params = params
|
7
|
-
@spectacles = ::Despecable::Spectacles.new
|
7
|
+
@spectacles = ::Despecable::Spectacles.new
|
8
8
|
end
|
9
9
|
|
10
10
|
def integer(name, options = {})
|
@@ -30,14 +30,17 @@ module Despecable
|
|
30
30
|
private
|
31
31
|
|
32
32
|
def _spec(name, type, options = {})
|
33
|
-
if params.key?(name)
|
34
|
-
params[name] = @spectacles.public_send(type, name)
|
35
|
-
elsif options.key?(:default)
|
33
|
+
if !params.key?(name) && options[:default]
|
36
34
|
params[name] = options[:default]
|
35
|
+
elsif options[:arrayable] && @spectacles.arrayable?(params[name])
|
36
|
+
values = @spectacles.arrayify(params[name])
|
37
|
+
params[name] = values.map{|val| @spectacles.read(val, type, options)}
|
38
|
+
else
|
39
|
+
value = @spectacles.read(params[name], type, options)
|
40
|
+
params[name] = value if params.key?(name)
|
37
41
|
end
|
38
|
-
|
39
|
-
|
42
|
+
rescue ::Despecable::DespecableError
|
43
|
+
::Kernel.raise $!.insert_name_here(name)
|
40
44
|
end
|
41
|
-
|
42
45
|
end
|
43
46
|
end
|