Module: HCA::Validations

Defined in:
lib/hca/validations.rb

Constant Summary collapse

INVALID_SSN_REGEXES =
[
  /^\d{3}-?\d{2}-?0{4}$/,
  /1{9}|2{9}|3{9}|4{9}|5{9}|6{9}|7{9}|8{9}|9{9}/,
  /^0{3}-?\d{2}-?\d{4}$/,
  /^\d{3}-?0{2}-?\d{4}$/
].freeze
FUTURE_DISCHARGE_CUTOFF =
(Settings.hca.future_discharge_testing ? 730 : 180).days

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.date_of_birth(input_dob) ⇒ Object



43
44
45
46
47
48
# File 'lib/hca/validations.rb', line 43

def date_of_birth(input_dob)
  parsed_dob = parse_date(input_dob)
  return '' if parsed_dob.blank? || parsed_dob.future?

  parsed_dob.strftime('%m/%d/%Y')
end

.discharge_date(date_string) ⇒ Object



30
31
32
33
34
35
# File 'lib/hca/validations.rb', line 30

def discharge_date(date_string)
  parsed_date = parse_date(date_string)
  return '' if parsed_date.blank?

  parsed_date.strftime('%m/%d/%Y')
end

.parse_date(date_string) ⇒ Object



16
17
18
19
20
# File 'lib/hca/validations.rb', line 16

def parse_date(date_string)
  return nil if !date_string.is_a?(String) || date_string.blank?

  Date.parse(date_string)
end

.parse_short_date(date_string) ⇒ Object



37
38
39
40
41
# File 'lib/hca/validations.rb', line 37

def parse_short_date(date_string)
  return '' if date_string.blank?

  date_string.split('-').excluding('XX').reverse.join('/')
end

.valid_discharge_date?(date_string) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/hca/validations.rb', line 22

def valid_discharge_date?(date_string)
  date = parse_date(date_string)
  return false if date.nil?

  cutoff = Time.zone.today + FUTURE_DISCHARGE_CUTOFF
  date <= cutoff
end

.validate_nameObject



63
64
65
66
67
68
# File 'lib/hca/validations.rb', line 63

def validate_name(*, **)
  formatted_name = validate_string(*, **)
  return '' if formatted_name.blank?

  formatted_name.upcase
end

.validate_ssn(input_ssn) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hca/validations.rb', line 70

def validate_ssn(input_ssn)
  return '' unless input_ssn.is_a?(String)

  validated_ssn = input_ssn.gsub(/\D/, '')

  return '' if validated_ssn.size != 9

  INVALID_SSN_REGEXES.each do |invalid_ssn_regex|
    return '' if invalid_ssn_regex.match?(validated_ssn)
  end

  validated_ssn
end

.validate_string(data:, count: nil, nullable: false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hca/validations.rb', line 50

def validate_string(data:, count: nil, nullable: false)
  blank_data = data.blank?

  return if nullable && blank_data
  return '' if blank_data || !data.is_a?(String)

  validated_string = data.dup
  validated_string[0] = validated_string[0].capitalize
  validated_string = validated_string[0, count] unless count.nil?

  validated_string
end

Instance Method Details

#date_of_birth(input_dob) ⇒ Object (private)



43
44
45
46
47
48
# File 'lib/hca/validations.rb', line 43

def date_of_birth(input_dob)
  parsed_dob = parse_date(input_dob)
  return '' if parsed_dob.blank? || parsed_dob.future?

  parsed_dob.strftime('%m/%d/%Y')
end

#discharge_date(date_string) ⇒ Object (private)



30
31
32
33
34
35
# File 'lib/hca/validations.rb', line 30

def discharge_date(date_string)
  parsed_date = parse_date(date_string)
  return '' if parsed_date.blank?

  parsed_date.strftime('%m/%d/%Y')
end

#parse_date(date_string) ⇒ Object (private)



16
17
18
19
20
# File 'lib/hca/validations.rb', line 16

def parse_date(date_string)
  return nil if !date_string.is_a?(String) || date_string.blank?

  Date.parse(date_string)
end

#parse_short_date(date_string) ⇒ Object (private)



37
38
39
40
41
# File 'lib/hca/validations.rb', line 37

def parse_short_date(date_string)
  return '' if date_string.blank?

  date_string.split('-').excluding('XX').reverse.join('/')
end

#valid_discharge_date?(date_string) ⇒ Object (private)



22
23
24
25
26
27
28
# File 'lib/hca/validations.rb', line 22

def valid_discharge_date?(date_string)
  date = parse_date(date_string)
  return false if date.nil?

  cutoff = Time.zone.today + FUTURE_DISCHARGE_CUTOFF
  date <= cutoff
end

#validate_nameObject (private)



63
64
65
66
67
68
# File 'lib/hca/validations.rb', line 63

def validate_name(*, **)
  formatted_name = validate_string(*, **)
  return '' if formatted_name.blank?

  formatted_name.upcase
end

#validate_ssn(input_ssn) ⇒ Object (private)



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hca/validations.rb', line 70

def validate_ssn(input_ssn)
  return '' unless input_ssn.is_a?(String)

  validated_ssn = input_ssn.gsub(/\D/, '')

  return '' if validated_ssn.size != 9

  INVALID_SSN_REGEXES.each do |invalid_ssn_regex|
    return '' if invalid_ssn_regex.match?(validated_ssn)
  end

  validated_ssn
end

#validate_string(data:, count: nil, nullable: false) ⇒ Object (private)



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hca/validations.rb', line 50

def validate_string(data:, count: nil, nullable: false)
  blank_data = data.blank?

  return if nullable && blank_data
  return '' if blank_data || !data.is_a?(String)

  validated_string = data.dup
  validated_string[0] = validated_string[0].capitalize
  validated_string = validated_string[0, count] unless count.nil?

  validated_string
end