class HealthcarePhony::Helper

Public Class Methods

double_alternate_digits(npi) click to toggle source
# File lib/healthcare_phony/helper.rb, line 48
def double_alternate_digits(npi)
  a_total = 0
  counter = 1
  npi.to_s.split('').each do |n|
    a_total += n.to_i if counter.even?
    counter += 1
  end
  a_total
end
get_array(input_argument) click to toggle source
# File lib/healthcare_phony/helper.rb, line 11
def get_array(input_argument)
  return_array = []

  if !input_argument.nil? && input_argument.instance_of?(Array)
    return_array = input_argument
  elsif !input_argument.nil? && input_argument.instance_of?(String)
    return_array = input_argument.split(',')
  end

  return_array
end
get_npi_check_digit(npi) click to toggle source
# File lib/healthcare_phony/helper.rb, line 58
def get_npi_check_digit(npi)
  step_one = npi_step_one(npi)

  # Add totals from above two steps + 24
  # c_total = 24 + a_total + b_total
  step_two = npi_step_two(npi, step_one)

  next_high = next_number_end_with_zero(step_two)

  # Step 3:  Subtract from next higher number ending in zero.
  (next_high - step_two)
end
next_number_end_with_zero(input) click to toggle source
# File lib/healthcare_phony/helper.rb, line 43
def next_number_end_with_zero(input)
  input += 1 while input.to_s[-1] != '0'
  input
end
npi_step_one(input) click to toggle source
# File lib/healthcare_phony/helper.rb, line 23
def npi_step_one(input)
  # Step 1: Double the value of alternate digits, beginning with the rightmost digit.
  npi = []
  input.to_s.split('').reverse.each_with_index do |n, i|
    npi.push((n.to_i * 2).to_s) if (i + 1).odd?
  end
  npi.reverse!
  total = 0
  npi.join('').split('').each do |x|
    total += x.to_i
  end
  total
end
npi_step_two(input, step_one_total) click to toggle source
# File lib/healthcare_phony/helper.rb, line 37
def npi_step_two(input, step_one_total)
  # Step 2:  Add constant 24, to account for the 80840 prefix that would be present on a card issuer identifier,
  # plus the individual digits of products of doubling, plus unaffected digits.
  (24 + step_one_total + double_alternate_digits(input))
end
random_with_blank(non_blank_value, blank_percentage) click to toggle source
# File lib/healthcare_phony/helper.rb, line 6
def random_with_blank(non_blank_value, blank_percentage)
  b_array = [[non_blank_value, (100 - blank_percentage)], ['', blank_percentage]]
  b_array.max_by { |_, weight| rand**100.fdiv(weight) }[0]
end