class TurkishId

Constants

VERSION

Attributes

checksum[R]
elder_relative[R]
id_number[R]
younger_relative[R]

Public Class Methods

new(id_number) click to toggle source
# File lib/turkish_id.rb, line 8
def initialize(id_number)
  @id_number = get_id_array(id_number)
  @checksum = calculate_checksum(@id_number)
  @elder_relative = generate_relatives(@id_number, :up)
  @younger_relative = generate_relatives(@id_number, :down)
end

Public Instance Methods

valid?() click to toggle source
# File lib/turkish_id.rb, line 15
def valid?
  return false if @id_number.length != 11 || @id_number.first.zero?
  return false if @id_number.values_at(9, 10) != @checksum ||= calculate_checksum(@id_number)

  true
end

Private Instance Methods

append_checksum(id) click to toggle source
# File lib/turkish_id.rb, line 39
def append_checksum(id)
  id_core = split_num(id)
  join_num(id_core + calculate_checksum(id_core))
end
calculate_checksum(id_array) click to toggle source
# File lib/turkish_id.rb, line 24
def calculate_checksum(id_array)
  # Calculate the sums of odd and even digits
  odds = id_array.values_at(0, 2, 4, 6, 8).reduce(:+)
  evens = id_array.values_at(1, 3, 5, 7).reduce(:+)

  # Generate checksum digits
  d10 = ((odds * 7) - evens) % 10
  d11 = (id_array.take(9).reduce(:+) + d10) % 10

  # Return checksum
  [d10, d11]
rescue StandardError
  []
end
generate_relatives(id_array, direction) click to toggle source
# File lib/turkish_id.rb, line 49
def generate_relatives(id_array, direction)
  Enumerator.new do |y|
    id = join_num(id_array)
    loop do
      id = next_relative(id, direction)
      break unless id.between?(100_000_001, 999_999_999)

      y << append_checksum(id)
    end
  end
end
get_core(id_array) click to toggle source
# File lib/turkish_id.rb, line 76
def get_core(id_array)
  join_num(split_num(id_array).take(9))
end
get_id_array(id) click to toggle source
# File lib/turkish_id.rb, line 61
def get_id_array(id)
  split_num(Integer(id))
rescue StandardError
  []
end
join_num(id_array) click to toggle source
# File lib/turkish_id.rb, line 72
def join_num(id_array)
  id_array.inject { |a, i| a * 10 + i }
end
next_relative(id_array, direction) click to toggle source
# File lib/turkish_id.rb, line 44
def next_relative(id_array, direction)
  tree = { up: 1, down: -1 }
  get_core(id_array) + 29_999 * tree[direction]
end
split_num(num) click to toggle source
# File lib/turkish_id.rb, line 67
def split_num(num)
  n = Math.log10(num).floor
  (0..n).map { |i| (num / 10**(n - i)) % 10 }
end