Module: Chook::Validators

Defined in:
lib/chook/subject/validators.rb

Overview

A namespace to hold validation methods for use with Test Events and Test Subjects. Each method taks a value, and returns a boolean indicating the validity of the value.

Class Method Summary collapse

Class Method Details

.boolean(true_or_false) ⇒ Boolean

Validate Boolean

Parameters:

  • Make (Boolean)

    sure input is valid Boolean, because Ruby doesn’t have a Boolean Class

Returns:

  • (Boolean)


92
93
94
# File 'lib/chook/subject/validators.rb', line 92

def self.boolean(true_or_false)
  [true, false].include? true_or_false
end

.email(email_address) ⇒ Boolean

Validate E-mail Address

Parameters:

Returns:

  • (Boolean)


26
27
28
# File 'lib/chook/subject/validators.rb', line 26

def self.email(email_address)
  email_address =~ /^[a-zA-Z]([\w -]*[a-zA-Z])\@\w*\.\w*$/ ? true : false
end

.iccid(iccid) ⇒ Boolean

Validate ICCID

Parameters:

  • ICCID (String)

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/chook/subject/validators.rb', line 82

def self.iccid(iccid)
  iccid_length = iccid.delete(' ').size
  return true if iccid_length < 23
end

.imei(imei) ⇒ Boolean

Validate IMEI

Parameters:

  • A (String)

    17-15 digit sequence of numbers

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/chook/subject/validators.rb', line 72

def self.imei(imei)
  imei_length = imei.delete(' ').size
  (15...17).cover? imei_length
end

.mac_address(mac) ⇒ Boolean

Validate MAC Address

Parameters:

  • MAC (String)

    address formatted String

Returns:

  • (Boolean)


17
18
19
# File 'lib/chook/subject/validators.rb', line 17

def self.mac_address(mac)
  mac =~ /^([a-f\d]{2}:){5}[a-f\d]{2}$/i ? true : false
end

.patch(patch_name) ⇒ Boolean

Validate Patch

Parameters:

  • Name (String)

    of a Patch Reporting Software Title

Returns:

  • (Boolean)


101
102
103
# File 'lib/chook/subject/validators.rb', line 101

def self.patch(patch_name)
  Chook::Randomizers::PATCH_SOFTWARE_TITLES.include? patch_name
end

.push(push) ⇒ Boolean

Validate Push

Parameters:

  • Push (String)

    Type

Returns:

  • (Boolean)

Raises:

  • (TypeError)


60
61
62
63
64
65
# File 'lib/chook/subject/validators.rb', line 60

def self.push(push)
  raise TypeError unless push.is_a? String
  return false if push.empty?
  return false unless Chook::Randomizers::PUSH_COMMANDS.include? push
  true
end

.serial_number(serial) ⇒ Boolean

Validate Serial Number

Parameters:

  • Serial (String)

    Number Formatted String

Returns:

  • (Boolean)

Raises:

  • (TypeError)


45
46
47
48
49
50
51
52
53
# File 'lib/chook/subject/validators.rb', line 45

def self.serial_number(serial)
  raise TypeError unless serial.is_a? String
  serial_array = serial.scan(/\w/)
  return false if serial.empty?
  serial_array.each_with_index do |character, index|
    return false unless (Chook::Randomizers::MOBILE_SERIAL_CHARACTER_SETS[index].include? character) || (Chook::Randomizers::COMPUTER_SERIAL_CHARACTER_SETS[index].include? character)
  end
  true
end

.url(url) ⇒ Boolean

Validate URL

Parameters:

  • URL (String)

    formatted String

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/chook/subject/validators.rb', line 35

def self.url(url)
  uri = URI.parse(url)
  uri.is_a?(URI::HTTP) && !uri.host.nil? ? true : false
end

.valid_test_subject(event_class, subject) ⇒ Boolean

Validate Test Subject

Parameters:

  • Name (String)

    of an Event class

  • An (Chook::TestSubjects)

    instance of a Chook::TestSubjects subclass

Returns:

  • (Boolean)


111
112
113
# File 'lib/chook/subject/validators.rb', line 111

def self.valid_test_subject(event_class, subject)
  Chook::Event::EVENTS[event_class].include? subject.class.to_s.split('::')[-1]
end