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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b5565ddf47367e796da5ff33a7efc1eee7f33b3
4
- data.tar.gz: 69a24212531937015c179a8ac9587217915ea667
3
+ metadata.gz: 269604b0ffb17ef578acfa582c7842445bed4c4c
4
+ data.tar.gz: 7a9f3d1fbee596ef848848b77faaf5cf535b1bc2
5
5
  SHA512:
6
- metadata.gz: 60375f5a8a3ae600481b0efdc6d71473f11acd0b9ebd00b3799f72445c4c630f97e86d9c02ab3cf647b4b1824f9fc9e8b1179e5af1a4824562743ad7b0d8cd68
7
- data.tar.gz: bca4f3e6cd1b0b165ed10feb910fe299f0c4bc5225d60661297caa41e155afa5c49d3a7b9d604f0b7d06f15a699a9e768a1eb8131138beff2ca1fef48b539862
6
+ metadata.gz: e207aaec581f73bd98f52c01e9e2906cd1a784b86dcf682b51f67923d1df59f7ae6a16a9426684b8c1e9696c9d37f2a6bb09410d02afb82905059d15df26ef9c
7
+ data.tar.gz: d2b3115499c5418afa3a13589fa1f3a049fbc64c2aca21c830774c2f5897791c98c048842fbe07def6322922e7580efa5fdd6850a9dada7fb7d027d448a45e45
@@ -1 +1,15 @@
1
- Despecable::DespecableError = Class.new(StandardError)
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 +1,5 @@
1
- Despecable::IncorrectParameterError = Class.new(Despecable::DespecableError)
1
+ class Despecable::IncorrectParameterError < Despecable::DespecableError
2
+ def introduction(name)
3
+ "Incorrect value for paramter '#{name}'."
4
+ end
5
+ end
@@ -1 +1,5 @@
1
- Despecable::InvalidParameterError = Class.new(Despecable::DespecableError)
1
+ class Despecable::InvalidParameterError < Despecable::DespecableError
2
+ def introduction(name)
3
+ "Invalid value for parameter '#{name}'."
4
+ end
5
+ end
@@ -1 +1,5 @@
1
- Despecable::MissingParameterError = Class.new(Despecable::DespecableError)
1
+ class Despecable::MissingParameterError < Despecable::DespecableError
2
+ def intro_message(name)
3
+ "Missing required parameter '#{name}'."
4
+ end
5
+ end
@@ -1,55 +1,65 @@
1
1
  module Despecable
2
2
  class Spectacles
3
- attr_reader :params
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 validate_param(name, options)
10
- validate_param_presence(name) if options[:required]
11
- validate_param_value(name, options[:in]) if options.key?(:in) && params.key?(name)
7
+ def arrayify(value)
8
+ return value if value.is_a?(Array)
9
+ value.to_s.split(",")
12
10
  end
13
11
 
14
- def validate_param_presence(name)
15
- raise Despecable::MissingParameterError, "Missing required param: '#{name}'" if !params.key?(name)
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 validate_param_value(name, allowed_values)
19
- raise Despecable::IncorrectParameterError, "Incorrect value for param: '#{name}'" if !allowed_values.include?(params[name])
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 integer(name)
23
- int = params[name].to_i
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 string(name)
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
- params[name].to_s
40
+ value.to_s
31
41
  end
32
42
 
33
- def boolean(name)
34
- case params[name].to_s
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, "Invalid value for param: '#{name}'. Require type: boolean (1/0 or true/false)"
47
+ else raise Despecable::InvalidParameterError, "Require type: boolean (1/0 or true/false)"
38
48
  end
39
49
  end
40
50
 
41
- def date(name)
42
- Date.rfc3339(params[name] + "T00:00:00+00:00")
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, "Invalid value for param: '#{name}'. Required type: date (e.g. '2012-12-31')."
55
+ raise Despecable::InvalidParameterError, "Required type: date (e.g. '2012-12-31')."
46
56
  end
47
57
 
48
- def datetime(name)
49
- DateTime.rfc3339(params[name])
58
+ def datetime(value)
59
+ DateTime.rfc3339(value)
50
60
  rescue ArgumentError
51
61
  raise unless $!.message == "invalid date"
52
- raise Despecable::InvalidParameterError, "Invalid value for param: '#{name}'. Required type: rfc3339 datetime (e.g. '2009-06-19T00:00:00-04:00')."
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
@@ -4,7 +4,7 @@ module Despecable
4
4
 
5
5
  def initialize(params)
6
6
  @params = params
7
- @spectacles = ::Despecable::Spectacles.new(params)
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
- @spectacles.validate_param(name, options)
39
- return params[name]
42
+ rescue ::Despecable::DespecableError
43
+ ::Kernel.raise $!.insert_name_here(name)
40
44
  end
41
-
42
45
  end
43
46
  end
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.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Schwartz