Class: Despecable::Spectacles

Inherits:
Object
  • Object
show all
Defined in:
lib/despecable/spectacles.rb

Instance Method Summary collapse

Instance Method Details

#any(name, value, options) ⇒ Object



112
113
114
# File 'lib/despecable/spectacles.rb', line 112

def any(name, value, options)
  value
end

#arrayable?(value) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/despecable/spectacles.rb', line 14

def arrayable?(value)
  value.is_a?(::Array) || /,/ =~ value.to_s
end

#arrayify(value) ⇒ Object



18
19
20
21
# File 'lib/despecable/spectacles.rb', line 18

def arrayify(value)
  return value if value.is_a?(Array)
  value.to_s.split(",")
end

#boolean(name, value, options) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/despecable/spectacles.rb', line 85

def boolean(name, value, options)
  case value.to_s
  when "true", "1" then true
  when "false", "0", nil then false
  else raise Despecable::InvalidParameterError.new("Boolean type (1/0 or true/false) required for parameter: '#{name}'", parameters: name)
  end
end

#custom(name, value, options) ⇒ Object



116
117
118
# File 'lib/despecable/spectacles.rb', line 116

def custom(name, value, options)
  return yield(name, value, options)
end

#date(name, value, options) ⇒ Object



93
94
95
96
97
98
# File 'lib/despecable/spectacles.rb', line 93

def date(name, value, options)
  Date.rfc3339(value + "T00:00:00+00:00")
rescue ArgumentError
  raise unless $!.message == "invalid date"
  raise Despecable::InvalidParameterError.new("Date type (e.g. '2012-12-31') required for parameter: '#{name}'", parameters: name)
end

#datetime(name, value, options) ⇒ Object



100
101
102
103
104
105
# File 'lib/despecable/spectacles.rb', line 100

def datetime(name, value, options)
  DateTime.rfc3339(value)
rescue ArgumentError
  raise unless $!.message == "invalid date"
  raise Despecable::InvalidParameterError.new("Rfc3339 datetime type (e.g. '2009-06-19T00:00:00-04:00') required for parameter: '#{name}'", parameters: name)
end

#expected_values_message(allowed_values) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/despecable/spectacles.rb', line 52

def expected_values_message(allowed_values)
  case allowed_values
  when Array
    msg = ": " + allowed_values.slice(0..99).join(", ")
    msg += ", ... [truncated]" if allowed_values.length > 100
    return msg
  when Range
    " between #{allowed_values.first} and #{allowed_values.last}"
  else
    nil
  end
end

#file(name, value, options) ⇒ Object



107
108
109
110
# File 'lib/despecable/spectacles.rb', line 107

def file(name, value, options)
  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)
  return value
end

#float(name, value, options) ⇒ Object



72
73
74
75
76
77
# File 'lib/despecable/spectacles.rb', line 72

def float(name, value, options)
  Float(value)
rescue ArgumentError
  raise unless /^invalid value for Float/ =~ $!.message
  raise Despecable::InvalidParameterError.new("Float type required for parameter: '#{name}'", parameters: name)
end

#integer(name, value, options) ⇒ Object



65
66
67
68
69
70
# File 'lib/despecable/spectacles.rb', line 65

def integer(name, value, options)
  Integer(value)
rescue ArgumentError
  raise unless /^invalid value for Integer/ =~ $!.message
  raise Despecable::InvalidParameterError.new("Integer type required for parameter: '#{name}'", parameters: name)
end

#parse(name, value, type, options, &blk) ⇒ Object



23
24
25
26
27
# File 'lib/despecable/spectacles.rb', line 23

def parse(name, value, type, options, &blk)
  value = public_send(type, name, value, options, &blk)
  validate_param_value(name, value, options) if options.key?(:in) && !value.nil?
  return value
end

#read(name, value, type, options, &blk) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/despecable/spectacles.rb', line 3

def read(name, value, type, options, &blk)
  if options[:array]
    arrayify(value).map{|val| parse(name, val, type, options, &blk)}
  elsif options[:arrayable] && arrayable?(value)
    # TODO: :arrayable is deprecated in favor of :array
    arrayify(value).map{|val| parse(name, val, type, options, &blk)}
  else
    parse(name, value, type, options, &blk)
  end
end

#string(name, value, options) ⇒ Object



79
80
81
82
83
# File 'lib/despecable/spectacles.rb', line 79

def string(name, value, options)
  value = value.to_s #no-op
  validate_string_length(name, value, options) if options.key?(:length)
  return value
end

#validate_param_value(name, value, options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/despecable/spectacles.rb', line 29

def validate_param_value(name, value, options)
  allowed_values = options[:in]
  allowed_values = allowed_values.map{|x| x.is_a?(String) ? x.downcase : x} if options[:case] == false
  value = value.downcase if options[:case] == false && value.is_a?(String)
  if !allowed_values.include?(value)
    msg = "Unacceptable value for parameter: '#{name}'"
    allowed_values_message = expected_values_message(allowed_values)
    msg += "; acceptable values are" + allowed_values_message if !allowed_values_message.nil?
    raise Despecable::IncorrectParameterError.new(msg, parameters: name)
  end
end

#validate_string_length(name, value, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/despecable/spectacles.rb', line 41

def validate_string_length(name, value, options)
  allowed_lengths = options[:length]
  allowed_lengths = [*allowed_lengths] unless allowed_lengths.is_a?(Range)
  if !allowed_lengths.include?(value.length)
    msg = "Unacceptable length for parameter: '#{name}'"
    allowed_values_message = expected_values_message(allowed_lengths)
    msg += "; acceptable lengths are" + allowed_values_message if !allowed_values_message.nil?
    raise Despecable::IncorrectParameterError.new(msg, parameters: name)
  end
end