Class: Wampproto::Validate

Inherits:
Object
  • Object
show all
Defined in:
lib/wampproto/validate.rb

Overview

validation

Class Method Summary collapse

Class Method Details

.array!(name, value) ⇒ Object

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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 options!(options, valid_keys)
  options.each_key do |key|
    raise ArgumentError, "Unrecognized option: #{key.inspect}" unless valid_keys.include?(key)
  end

  options
end

.string!(name, value) ⇒ Object

Raises:

  • (ArgumentError)


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