Class: Wampproto::Validate
- Inherits:
-
Object
- Object
- Wampproto::Validate
- Defined in:
- lib/wampproto/validate.rb
Overview
validation
Class Method Summary collapse
- .array!(name, value) ⇒ Object
- .greater_than_equal!(array, expected_length) ⇒ Object
- .hash!(name, value) ⇒ Object
- .int!(name, value) ⇒ Object
- .length!(array, expected_length) ⇒ Object
- .options!(options, valid_keys) ⇒ Object
- .string!(name, value) ⇒ Object
Class Method Details
.array!(name, value) ⇒ Object
25 26 27 28 29 |
# File 'lib/wampproto/validate.rb', line 25 def array!(name, value) return value if value.is_a?(Array) raise ArgumentError, "The #{name} argument should be a list." end |
.greater_than_equal!(array, expected_length) ⇒ Object
37 38 39 40 41 |
# File 'lib/wampproto/validate.rb', line 37 def greater_than_equal!(array, expected_length) return array if array.length >= expected_length raise ArgumentError, "The response message length is #{array.length} but it should be #{expected_length} " end |
.hash!(name, value) ⇒ Object
19 20 21 22 23 |
# File 'lib/wampproto/validate.rb', line 19 def hash!(name, value) return value.transform_keys(&:to_sym) if value.is_a?(Hash) raise ArgumentError, "The #{name} argument should be a dictionary." end |
.int!(name, value) ⇒ Object
7 8 9 10 11 |
# File 'lib/wampproto/validate.rb', line 7 def int!(name, value) return value if value.is_a?(Integer) raise ArgumentError, "The #{name} argument should be an integer." end |
.length!(array, expected_length) ⇒ Object
31 32 33 34 35 |
# File 'lib/wampproto/validate.rb', line 31 def length!(array, expected_length) return array if array.length == expected_length raise ArgumentError, "The response message length should be #{expected_length} but got #{array.length} " end |
.options!(options, valid_keys) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/wampproto/validate.rb', line 43 def (, valid_keys) .each_key do |key| raise ArgumentError, "Unrecognized option: #{key.inspect}" unless valid_keys.include?(key) end end |
.string!(name, value) ⇒ Object
13 14 15 16 17 |
# File 'lib/wampproto/validate.rb', line 13 def string!(name, value) return value if value.is_a?(String) raise ArgumentError, "The #{name} argument should be a string." end |