Class: IGE_ISB_API::Validation
- Inherits:
-
Object
- Object
- IGE_ISB_API::Validation
- Defined in:
- lib/ige_isb_api/validation.rb
Class Method Summary collapse
- .is_valid_currency?(an_amount) ⇒ Boolean
- .is_valid_datestring?(a_datestring) ⇒ Boolean
- .is_valid_email?(an_email) ⇒ Boolean
- .is_valid_ip4?(an_ip) ⇒ Boolean
- .is_valid_name?(a_name) ⇒ Boolean
- .is_valid_password?(a_password) ⇒ Boolean
- .is_valid_phonenumber?(a_phonenumber) ⇒ Boolean
- .is_valid_username?(a_username) ⇒ Boolean
- .test(a_fieldname, a_value, a_type, opts = {}) ⇒ Object
Class Method Details
.is_valid_currency?(an_amount) ⇒ Boolean
69 70 71 72 |
# File 'lib/ige_isb_api/validation.rb', line 69 def self.is_valid_currency?(an_amount) vc = an_amount.match /^[1-9][0-9]*\.[0-9][0-9]$/ return !vc.nil? end |
.is_valid_datestring?(a_datestring) ⇒ Boolean
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ige_isb_api/validation.rb', line 50 def self.is_valid_datestring?(a_datestring) dm = a_datestring.match /^((19|20)[0-9][0-9])-([0-9]{2})-([0-9]{2})\.([0-9]{2}):([0-9]{2}):([0-9]{2})$/ if dm.nil? dm = a_datestring.match /^((19|20)[0-9][0-9])-([0-9]{2})-([0-9]{2})$/ return false if dm.nil? else return false unless $5.to_i.between?(0,23) return false unless $6.to_i.between?(0,59) return false unless $7.to_i.between?(0,59) end return false unless $1.to_i.between?(1900,2099) return false unless $3.to_i.between?(1,12) return false unless $4.to_i.between?(1,31) return false if [9, 4, 6, 11].include?($3.to_i) && !$4.to_i.between?(1,30) return false if $3.to_i == 2 && $1.to_i % 4 == 0 && !$4.to_i.between?(1,29) # leap year return false if $3.to_i == 2 && !$4.to_i.between?(1,28) # non leap year return true end |
.is_valid_email?(an_email) ⇒ Boolean
23 24 25 26 |
# File 'lib/ige_isb_api/validation.rb', line 23 def self.is_valid_email?(an_email) return false if an_email.empty? return !an_email.match(RFC822::EMAIL).nil? end |
.is_valid_ip4?(an_ip) ⇒ Boolean
42 43 44 45 46 47 48 |
# File 'lib/ige_isb_api/validation.rb', line 42 def self.is_valid_ip4?(an_ip) # valid IP4 address vip = an_ip.match /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/ return false if vip.nil? return false unless $1.to_i.between?(0,255) && $2.to_i.between?(0,255) && $3.to_i.between?(0,255) return true end |
.is_valid_name?(a_name) ⇒ Boolean
28 29 30 31 32 33 |
# File 'lib/ige_isb_api/validation.rb', line 28 def self.is_valid_name?(a_name) "0123456789!@#$%^&*()[]\\/+=?<>~;:{}".split('').each do |ch| return false if a_name.include?(ch) end return true end |
.is_valid_password?(a_password) ⇒ Boolean
17 18 19 20 21 |
# File 'lib/ige_isb_api/validation.rb', line 17 def self.is_valid_password?(a_password) # Password must be over 6 and less than 20 characters long; Password must be alphanumeric return false unless a_password.size.between?(6,20) return !a_password.match(/^[[:alpha:]|[0-9]]+$/).nil? end |
.is_valid_phonenumber?(a_phonenumber) ⇒ Boolean
35 36 37 38 39 40 |
# File 'lib/ige_isb_api/validation.rb', line 35 def self.is_valid_phonenumber?(a_phonenumber) # phone can start with + # TODO: Awaiting feedback from ISB numb_regex = /^\+?[0-9\(\s\)-]{5,21}$/ return !a_phonenumber.match(numb_regex).nil? end |
.is_valid_username?(a_username) ⇒ Boolean
11 12 13 14 15 |
# File 'lib/ige_isb_api/validation.rb', line 11 def self.is_valid_username?(a_username) # Username must be alphanumeric return false if a_username.size > 32 return !a_username.match(/^[[:alpha:]|[0-9]|\-_]+$/).nil? end |
.test(a_fieldname, a_value, a_type, opts = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ige_isb_api/validation.rb', line 74 def self.test(a_fieldname, a_value, a_type, opts = {}) correct_type = true begin case a_type when 'username' correct_type = a_value.is_a?(String) && is_valid_username?(a_value) when 'password' correct_type = a_value.is_a?(String) && is_valid_password?(a_value) when 'boolstring' correct_type = a_value.is_a?(String) && ['True', 'False'].include?(a_value) when 'country' correct_type = a_value.is_a?(String) && !IGE_ISB_API::Constants::ALLOWED_COUNTRY_CODES[a_value.to_sym].nil? when 'limit' correct_type = a_value.is_a?(String) && IGE_ISB_API::Constants::ALLOWED_LIMITATIONS.include?(a_value) when 'timespan' correct_type = a_value.is_a?(String) && IGE_ISB_API::Constants::ALLOWED_FREQUENCIES.include?(a_value) when 'string' correct_type = a_value.is_a? String raise ValidationException, sprintf("Value '%s' in field '%s' is too long.", a_value, a_fieldname) if opts[:length] && a_value.size > opts[:length] raise ValidationException, "#{a_value} is invalid for #{a_fieldname}" if opts[:values] && !opts[:values].include?(a_value) when 'currency_code' correct_type = a_value.is_a?(String) && IGE_ISB_API::Constants::ALLOWED_CURRENCIES.include?(a_value) when 'currency' correct_type = is_valid_currency?(a_value) when 'percent' correct_type = a_value.is_a?(Float) || a_value.is_a?(Integer) raise ValidationException, "#{a_fieldname} must be positive" if a_value < 0 when 'int' correct_type = a_value.is_a? Integer raise ValidationException, "#{a_fieldname} only allows positive integers" if a_value < 0 raise ValidationException, "#{a_value} is invalid for #{a_fieldname}" if opts[:values] && !opts[:values].include?(a_value) when 'boolint' correct_type = a_value.is_a?(Integer) && [0, 1].include?(a_value) when 'datestring' correct_type = a_value.is_a? String raise ValidationException, "#{a_value} is not a valid date string." unless is_valid_datestring?(a_value) when 'timestamp' correct_type = a_value.is_a? Integer raise ValidationException, "#{a_value} is not a valid timestamp." unless a_value > 0 when 'phone' correct_type = a_value.is_a?(String) && is_valid_phonenumber?(a_value) when 'ip4' correct_type = a_value.is_a?(String) && is_valid_ip4?(a_value) when 'email' correct_type = a_value.is_a?(String) && is_valid_email?(a_value) end rescue ArgumentError => ae raise ValidationException, sprintf("Value '%s' in field '%s' is not of expected type '%s' Error: %s", a_value, a_fieldname, a_type, ae.) end raise ValidationException, sprintf("Value '%s' in field '%s' is not of expected type '%s'", a_value, a_fieldname, a_type) unless correct_type end |