Class: PLCommerce::Pesel

Inherits:
Object
  • Object
show all
Defined in:
lib/pl-commerce/pesel.rb

Constant Summary collapse

SEX_POSITION =
9
CONTROL_CODE_POSITION =
10
WAGES =
[1,3,7,9,1,3,7,9,1,3]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pesel, opts = {:lenient => false}) ⇒ Pesel

Constructor It takes pesel as first argument first argument can be either number or string

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
# File 'lib/pl-commerce/pesel.rb', line 20

def initialize(pesel, opts = {:lenient => false})
  raise ArgumentError, 'pesel argument must be either of type String or Bignum' unless pesel.instance_of? String or pesel.instance_of? Bignum

  @lenient = opts[:lenient];
  @pesel = strip_PESEL(pesel);
  if not @lenient
    pre_validate_PESEL(@pesel);
  end
end

Class Method Details

.sex?(pesel, lenient = false) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/pl-commerce/pesel.rb', line 139

def self.sex?(pesel, lenient = false)
  return Pesel.new(pesel, lenient).sex?
end

.valid?(pesel, lenient = false) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/pl-commerce/pesel.rb', line 135

def self.valid?(pesel, lenient = false)
  return Pesel.new(pesel, lenient).valid?
end

Instance Method Details

#female?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/pl-commerce/pesel.rb', line 50

def female?
  return sex == 'F'
end

#get_date_of_birthObject Also known as: dob, date_of_birth



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
127
128
129
130
131
132
133
# File 'lib/pl-commerce/pesel.rb', line 82

def get_date_of_birth
  begin
    pre_validate_PESEL(@pesel, true, false, false)
    #we need at least first 6 numbers
    if @pesel.length < 6
      return '?';
    end
    
  rescue Exception => e
    if @lenient
      return '?'
    end

    raise e
  end
  
  sYearLow = '19';
  sYearHigh = @pesel[0, 2];
  iMonth = @pesel[2, 2].to_i;
  iDay = @pesel[4, 2].to_i;
  
  iMileniumCode = @pesel[2, 1].to_i;
  case iMileniumCode
    #(1900 - 1999) 20 century
    when 0..1
      sYearLow = '19';
    #(2000 - 2099) 21st century
    when 2..3
      sYearLow = '20';
      iMonth = iMonth - 20;
    #(2100 - 2199) 22nd century
    when 4..5
      begin
        sYearLow = '21';
        iMonth = iMonth - 40;
      end
    #(2200 - 2299) 23rd century
   when 6..7
      sYearLow = '22';
      iMonth = iMonth - 60;
      
   #(1800 - 1899) 19th century
   when 8..9
      sYearLow = '18';
      iMonth = iMonth - 80;
  end
  
  sYear = sYearLow + sYearHigh;
  iYear = sYear.to_i;
  
  return Date.new(iYear, iMonth, iDay)
end

#male?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/pl-commerce/pesel.rb', line 46

def male?
  return sex == 'M'
end

#sexObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pl-commerce/pesel.rb', line 30

def sex
  begin
    #to get sex we don't need all characters to be numeric but we need the same length
    pre_validate_PESEL(@pesel, true, true, false)
  rescue Exception => e
    return '?'
  end
  
  iSexValue = @pesel[SEX_POSITION, 1].to_i
  if (iSexValue % 2) == 0
    return 'F'
  end
  
  return 'M'
end

#to_sObject



143
144
145
# File 'lib/pl-commerce/pesel.rb', line 143

def to_s
  return @pesel;
end

#valid?Boolean

Returns:

  • (Boolean)


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/pl-commerce/pesel.rb', line 54

def valid?
  #if lenient, it means we will not be throwing exceptions if pesel is incorrect
  # we will simply return false in that case
  if @lenient
    begin
      pre_validate_PESEL(@pesel)
    rescue Exception => e
      return false
    end
  end

  sum = 0
  for i in 0..(@pesel.length-2)
    sum += @pesel[i, 1].to_i * WAGES[i].to_i
  end

  iControlCode = sum % 10;
  
  iControlCode = 10 - iControlCode;
  if iControlCode == 10
    iControlCode = 0;
  end
  
  iControlPESELValue = @pesel[CONTROL_CODE_POSITION, 1].to_i;
  
  return (iControlPESELValue == iControlCode);
end