Module: ChupakabraTools::Validations

Defined in:
lib/chupakabra_tools/validations.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.russian_letters_templateObject



4
5
6
# File 'lib/chupakabra_tools/validations.rb', line 4

def self.russian_letters_template
    /^[а-яА-Я\ ]+$/
end

.validate_email(data, params = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/chupakabra_tools/validations.rb', line 82

def self.validate_email(data, params = {})
    return false if data.nil?
    return false unless data.is_a?(String)
    data.strip!
    return false if data.blank?
    (data =~ /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,10}\z/) != nil
end

.validate_phone(phone, params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chupakabra_tools/validations.rb', line 13

def self.validate_phone(phone, params={})

    params ||= {}


    if phone.nil?
        return params[:allow_nil] && params[:allow_nil] == true
    end

    unless phone.is_a?(String)
        phone = phone.to_s
    end

    phone.strip!

    if phone.blank?
        return params[:allow_blank] && params[:allow_blank] == true
    end

    phone = phone.gsub(/[^0-9]/, "")

    # checking phone lengths for each country
    if phone.starts_with?("7")
        # for Russia, Kazahstan, Abhazia and so on
        phone = phone.length == 11 ? phone : nil
        #phone += " Russia" if phone
    elsif phone.starts_with?("38")
        # Ukrine
        phone = phone.length == 12 ? phone : nil
        #phone += " Ukraine" if phone
    elsif phone.starts_with?("375")
        # Belarus
        phone = phone.length == 12 ? phone : nil
        #phone += " Belarus" if phone
    elsif phone.starts_with?("370")
        # Litua
        phone = phone.length == 11 ? phone : nil
        #phone += " Litva" if phone
    elsif phone.starts_with?("371")
        # Latvia
        phone = phone.length == 11 ? phone : nil
        #phone += " Latvia"  if phone
    elsif phone.starts_with?("372")
        # Estonia
        phone = phone.length == 11 || phone.length == 10 ? phone : nil
        #phone += " Estonia" if phone
    elsif phone.starts_with?("995") && phone.length != 12
        # Geogia
        phone = phone.length == 11 ? phone : nil
        #phone += " Georgia" if phone
    elsif phone.starts_with?("374")
        #Armenia
        phone = phone.length == 11 ? phone : nil
        #phone += " Armenia" if phone
    elsif phone.starts_with?("994")
        # Azerbadzhan
        phone = phone.length == 12 ? phone : nil
        #phone += " Azerbaidzhan" if phone
    elsif phone.starts_with?("373")
        # Moldova
        phone = phone.length == 11 ? phone : nil
        #phone += " Moldova" if phone
    else
        phone = nil
    end

    phone ? true : false
end

Instance Method Details

#russian_letters_and_digitsObject



8
9
10
# File 'lib/chupakabra_tools/validations.rb', line 8

def russian_letters_and_digits
    /^[а-яА-Я0-9\ ,;.\-]+$/
end