Class: SsnValidator::Ssn

Inherits:
Object
  • Object
show all
Defined in:
app/models/ssn_validator/ssn.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssn, date = nil) ⇒ Ssn

Instantiate the object passing in a social security number and optional validation date. Validation date allows you to ask if the ssn was valid (or deceased) according to the SSA as of some date specified. The ssn can be a string or integer, with or without the ‘-’s. Date is any ruby valid date, defaults to today.



11
12
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
# File 'app/models/ssn_validator/ssn.rb', line 11

def initialize(ssn, date=nil)
  @errors = []
  ssn = ssn.to_s
  @validation_date = Date.parse(date.to_s) rescue Date.today
  if ssn =~ /-/ && ssn !~ /\d\d\d-\d\d-\d\d\d\d/
    @errors << 'Hyphen misplaced.'
  end
  ssn.gsub!('-', '')
  if ssn.to_s.size != 9
    @errors << 'SSN not 9 digits long.'
  end
  if ssn =~ /\D/
    @errors << 'Non-digit found.'
  end
  #known dummy numbers
  if %w(078051120 111111111 123456789 219099999 999999999).include? ssn || (ssn >= '987654320' and ssn <= '987654329')
    @errors << 'Known dummy SSN.'
  end
  #known invalid area, group and serial numbers
  if ssn =~ /\d{3}00\d{4}|0000\Z/
    @errors << 'Invalid group or serial number.'
  end
  @ssn = ssn
  @area = ssn.first(3)
  @group = ssn[3, 2]
  @serial_number = ssn.last(4)
  if @errors.empty?
    if ssn_high_group_code = SsnHighGroupCode.where(area: @area).where('as_of <= ?', @validation_date).order('as_of DESC').first
      @as_of = ssn_high_group_code.as_of
      define_group_ranks
      if @group_ranks[@group] > @group_ranks[ssn_high_group_code.group]
        @errors << "Group '#{@group}' has not been assigned yet for area '#{@area}'"
      end
    else
      @errors << "Area '#{@area}' has not been assigned."
    end
  end
end

Instance Attribute Details

#areaObject (readonly)

Returns the value of attribute area.



4
5
6
# File 'app/models/ssn_validator/ssn.rb', line 4

def area
  @area
end

#as_ofObject (readonly)

Returns the value of attribute as_of.



4
5
6
# File 'app/models/ssn_validator/ssn.rb', line 4

def as_of
  @as_of
end

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'app/models/ssn_validator/ssn.rb', line 5

def errors
  @errors
end

#groupObject (readonly)

Returns the value of attribute group.



4
5
6
# File 'app/models/ssn_validator/ssn.rb', line 4

def group
  @group
end

#serial_numberObject (readonly)

Returns the value of attribute serial_number.



4
5
6
# File 'app/models/ssn_validator/ssn.rb', line 4

def serial_number
  @serial_number
end

#ssnObject (readonly)

Returns the value of attribute ssn.



4
5
6
# File 'app/models/ssn_validator/ssn.rb', line 4

def ssn
  @ssn
end

#validation_dateObject (readonly)

Returns the value of attribute validation_date.



4
5
6
# File 'app/models/ssn_validator/ssn.rb', line 4

def validation_date
  @validation_date
end

Instance Method Details

#death_master_file_hit?Boolean

Determines if the passed in ssn belongs to the deceased.

Returns:

  • (Boolean)


62
63
64
# File 'app/models/ssn_validator/ssn.rb', line 62

def death_master_file_hit?
  !death_master_file_record.nil?
end

#death_master_file_recordObject

returns the death master record if there is one.



57
58
59
# File 'app/models/ssn_validator/ssn.rb', line 57

def death_master_file_record
  DeathMasterFile.where(social_security_number: @ssn).where('as_of <= ?', @validation_date).order('as_of DESC').first
end

#valid?Boolean

Determines whether or not the passed in ssn passed all validations.

Returns:

  • (Boolean)


52
53
54
# File 'app/models/ssn_validator/ssn.rb', line 52

def valid?
  @errors.empty?
end