Class: Identified::SSN

Inherits:
Object
  • Object
show all
Defined in:
lib/identified/ssn/ssn.rb

Overview

Represents a Social Security Number and provides validation functionality.

Constant Summary collapse

RANDOMIZATION_DATE =
Date.parse('2011-06-25').freeze
RETIRED_SSNS =
%w(078-05-1120 219-09-9999)
SSN_REGEX =
/\A\d{3}-\d{2}-\d{4}\Z/
SSN_REGEX_WITHOUT_DASHES =
/\A(?<area>\d{3})(?<group>\d{2})(?<serial>\d{4})\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssn_string, options = {}) ⇒ SSN

Date is optional but should be provided to improve validation quality.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/identified/ssn/ssn.rb', line 12

def initialize(ssn_string, options = {})
  area_num, group_num, serial_num = extract_ssn_values(ssn_string)

  @area = AreaNumber.new(area_num)
  @group = GroupNumber.new(group_num)
  @serial = SerialNumber.new(serial_num)

  # Emulating keyword arguments to provide ruby 1.9.3 support.
  if options.instance_of?(Hash)
    @date_issued = options.delete(:date_issued)
  else
    fail ArgumentError, 'Unexpected argument. The second argument must be an options hash.'
  end
  fail ArgumentError, "Unrecgonized option(s): #{options}" if options.any?
end

Instance Attribute Details

#areaObject (readonly)

Returns the value of attribute area.



9
10
11
# File 'lib/identified/ssn/ssn.rb', line 9

def area
  @area
end

#date_issuedObject (readonly)

Returns the value of attribute date_issued.



9
10
11
# File 'lib/identified/ssn/ssn.rb', line 9

def date_issued
  @date_issued
end

#groupObject (readonly)

Returns the value of attribute group.



9
10
11
# File 'lib/identified/ssn/ssn.rb', line 9

def group
  @group
end

#serialObject (readonly)

Returns the value of attribute serial.



9
10
11
# File 'lib/identified/ssn/ssn.rb', line 9

def serial
  @serial
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'lib/identified/ssn/ssn.rb', line 40

def ==(other)
  area == other.area && group == other.group && serial == other.serial
end

#issuing_statesObject

Provides an array of potential states or protectorates the ssn was issued in. This information cannot be known unless an issuance date is known and it before SSN randomizaiton. If no information is avaliable, issuing_states will return [].



36
37
38
# File 'lib/identified/ssn/ssn.rb', line 36

def issuing_states
  IssuingStateData.issuing_states(area, date_issued)
end

#to_sObject

Uses ‘123-45-6789’ format.



45
46
47
# File 'lib/identified/ssn/ssn.rb', line 45

def to_s
  format('%.03d-%.02d-%.04d', area, group, serial)
end

#valid?Boolean

Returns whether the ssn COULD be a valid ssn.

Returns:

  • (Boolean)


29
30
31
# File 'lib/identified/ssn/ssn.rb', line 29

def valid?
  area.valid? && group.valid?(area, date_issued) && serial.valid? && !retired?
end