despecable 0.1.0 → 0.1.1

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: 9a990382ac0e881252fdba00bb3da79c83efd94b
4
- data.tar.gz: 968824fbdd35b569e35480c1ff8e44e56e8d9f37
3
+ metadata.gz: 290a992d2845a837ff7ce19c2fab3dccb6777fcd
4
+ data.tar.gz: 4e6bda047e215fc133e20ad56c02ebc417719798
5
5
  SHA512:
6
- metadata.gz: ec7b8ba68446f1e2b6ecc3b1695fae9794044ea19e166ca690bfa632b982f20558db7e6037068d76127c8181a3bf2a2866a6f86e8b8cd0580676c3dbd827896c
7
- data.tar.gz: a6267c9f73c3545f0967954bc36d80bf7ca2aa020b77f2f0f90363a560832db8123fbdb69b6cd7a6cd233f6fddca2c300f1c3d4cd68313713117737e47747931
6
+ metadata.gz: c59f54e988d690ce171d1cfada645fa3790d4e3435cab086e16d3d8988aee03ca315446b87b82d02cd4763e1b3702e6ecd6db134de645d35963134d2352957fc
7
+ data.tar.gz: cc348e49a9cea4d797f788e0d9e1b5abbeb7ff3c778e552622a3cc9508b6a93862d3d3fe23805d8845d2e6ab9913aabc5c90871b37e5aa902d07a388b1d485b4
@@ -1,15 +1,15 @@
1
1
  class Despecable::DespecableError < StandardError
2
- def despecable_message
3
- message == self.class.to_s ? nil : message
4
- end
2
+ attr_accessor :parameters
3
+ protected :parameters=
5
4
 
6
- def insert_name_here(name)
7
- msg = introduction(name)
8
- msg += " " + despecable_message unless despecable_message.nil?
9
- return exception(msg)
5
+ def initialize(*args, parameters: [])
6
+ @parameters = [*parameters].map(&:to_s)
7
+ super(*args)
10
8
  end
11
9
 
12
- def introduction(name)
13
- "Invalid value for parameter #{name}."
10
+ def exception(*args)
11
+ ex = super
12
+ ex.parameters = @parameters
13
+ return ex
14
14
  end
15
15
  end
@@ -1,5 +1,5 @@
1
1
  class Despecable::UnrecognizedParameterError < Despecable::DespecableError
2
- def intro_message(name)
3
- "Unrecognized parameter name: '#{name}'"
2
+ def introduction(name)
3
+ "Unrecognized parameter '#{name}'"
4
4
  end
5
5
  end
@@ -7,7 +7,7 @@ module Despecable
7
7
  # pass in request.query_parameters (GET) and/or request.request_parameters (POST)
8
8
  # for supplied_params to correctly get only the user-supplied parameters.
9
9
  def initialize(params, supplied_params = nil)
10
- @supplied_params = (supplied_params || params).deep_dup
10
+ @supplied_params = (supplied_params || params).dup
11
11
  @params = params
12
12
  @specd = []
13
13
  end
@@ -27,7 +27,8 @@ module Despecable
27
27
  def despecably_strict
28
28
  if !unspecd.empty?
29
29
  list = unspecd.map{|x| "'#{x}'"}.join(", ")
30
- raise Despecable::UnrecognizedParameterError, "Unrecognized parameters: #{list}"
30
+ error = Despecable::UnrecognizedParameterError.new("Unrecognized parameters supplied: #{list}", parameters: unspecd)
31
+ raise error
31
32
  end
32
33
  end
33
34
  end
@@ -9,72 +9,72 @@ module Despecable
9
9
  value.to_s.split(",")
10
10
  end
11
11
 
12
- def read(value, type, options)
13
- value = public_send(type, value) unless value.nil?
14
- validate_param(value, options)
12
+ def read(name, value, type, options)
13
+ value = public_send(type, name, value) unless value.nil?
14
+ validate_param(name, value, options)
15
15
  return value
16
16
  end
17
17
 
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?
18
+ def validate_param(name, value, options)
19
+ validate_param_presence(name, value) if options[:required]
20
+ validate_param_value(name, value, options[:in]) if options.key?(:in) && !value.nil?
21
21
  end
22
22
 
23
- def validate_param_presence(value)
24
- raise Despecable::MissingParameterError if value.nil?
23
+ def validate_param_presence(name, value)
24
+ raise Despecable::MissingParameterError.new("Missing required parameter: #{name}", parameters: name) if value.nil?
25
25
  end
26
26
 
27
- def validate_param_value(value, allowed_values)
28
- raise Despecable::IncorrectParameterError, "Value received: #{value}." if !allowed_values.include?(value)
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)
29
29
  end
30
30
 
31
- def integer(value)
31
+ def integer(name, value)
32
32
  Integer(value)
33
33
  rescue ArgumentError
34
34
  raise unless /^invalid value for Integer/ =~ $!.message
35
- raise Despecable::InvalidParameterError, "Required type: integer"
35
+ raise Despecable::InvalidParameterError.new("Integer type required for parameter: #{name}", parameters: name)
36
36
  end
37
37
 
38
- def float(value)
38
+ def float(name, value)
39
39
  Float(value)
40
40
  rescue ArgumentError
41
41
  raise unless /^invalid value for Float/ =~ $!.message
42
- raise Despecable::InvalidParameterError, "Required type: float"
42
+ raise Despecable::InvalidParameterError.new("Float type required for parameter: #{name}", parameters: name)
43
43
  end
44
44
 
45
- def string(value)
45
+ def string(name, value)
46
46
  # This is really a no-op.
47
47
  value.to_s
48
48
  end
49
49
 
50
- def boolean(value)
50
+ def boolean(name, value)
51
51
  case value.to_s
52
52
  when "true", "1" then true
53
53
  when "false", "0", nil then false
54
- else raise Despecable::InvalidParameterError, "Required type: boolean (1/0 or true/false)"
54
+ else raise Despecable::InvalidParameterError.new("Boolean type (1/0 or true/false) required for parameter: #{name}", parameters: name)
55
55
  end
56
56
  end
57
57
 
58
- def date(value)
58
+ def date(name, value)
59
59
  Date.rfc3339(value + "T00:00:00+00:00")
60
60
  rescue ArgumentError
61
61
  raise unless $!.message == "invalid date"
62
- raise Despecable::InvalidParameterError, "Required type: date (e.g. '2012-12-31')."
62
+ raise Despecable::InvalidParameterError.new("Date type (e.g. '2012-12-31') required for parameter: #{name}", parameters: name)
63
63
  end
64
64
 
65
- def datetime(value)
65
+ def datetime(name, value)
66
66
  DateTime.rfc3339(value)
67
67
  rescue ArgumentError
68
68
  raise unless $!.message == "invalid date"
69
- raise Despecable::InvalidParameterError, "Required type: rfc3339 datetime (e.g. '2009-06-19T00:00:00-04:00')."
69
+ raise Despecable::InvalidParameterError.new("Rfc3339 datetime type (e.g. '2009-06-19T00:00:00-04:00') required for parameter: #{name}", parameters: name)
70
70
  end
71
71
 
72
- def file(value)
73
- raise Despecable::InvalidParameterError, "Required type: file upload" if !(value.respond_to?(:original_filename) && value.original_filename.present?)
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?)
74
74
  return value
75
75
  end
76
76
 
77
- def any(value)
77
+ def any(name, value)
78
78
  value
79
79
  end
80
80
  end
@@ -48,17 +48,15 @@ module Despecable
48
48
  params[name] = options[:default]
49
49
  elsif options[:array]
50
50
  values = @spectacles.arrayify(params[name])
51
- params[name] = values.map{|val| @spectacles.read(val, type, options)}
51
+ params[name] = values.map{|val| @spectacles.read(name, val, type, options)}
52
52
  elsif options[:arrayable] && @spectacles.arrayable?(params[name])
53
53
  # TODO: deprecate arrayable in favor of array
54
54
  values = @spectacles.arrayify(params[name])
55
- params[name] = values.map{|val| @spectacles.read(val, type, options)}
55
+ params[name] = values.map{|val| @spectacles.read(name, val, type, options)}
56
56
  else
57
- value = @spectacles.read(params[name], type, options)
57
+ value = @spectacles.read(name, params[name], type, options)
58
58
  params[name] = value if params.key?(name)
59
59
  end
60
- rescue ::Despecable::DespecableError
61
- ::Kernel.raise $!.insert_name_here(name)
62
60
  end
63
61
  end
64
62
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: despecable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Schwartz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-14 00:00:00.000000000 Z
11
+ date: 2018-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec