class Kokki::Converter

Attributes

dict[R]
input[R]
upcase[R]

Public Class Methods

convert(input) click to toggle source
# File lib/kokki/converter.rb, line 33
def self.convert(input)
  new(input).convert
end
new(input) click to toggle source
# File lib/kokki/converter.rb, line 9
def initialize(input)
  @input = input
  @upcase = input.upcase
  @dict = Dictionary.new
end

Public Instance Methods

convert() click to toggle source
# File lib/kokki/converter.rb, line 15
def convert
  flag = nil

  case input.length
  when 2
    flag = dict.lookup_by_alpha_2_code(upcase)
  when 3
    flag = dict.lookup_by_alpha_3_code(upcase)
  end

  flag ||= dict.lookup_by_name(upcase)
  flag ||= convert_as_ip_address(input)

  raise InvalidInputError, "Invalid input given: #{input}" unless flag

  flag
end

Private Instance Methods

convert_as_ip_address(ip_address) click to toggle source
# File lib/kokki/converter.rb, line 43
def convert_as_ip_address(ip_address)
  ip = IPAddress.new(refang(ip_address))
  dict.lookup_by_alpha_2_code ip.country_code
end
refang(input) click to toggle source
# File lib/kokki/converter.rb, line 39
def refang(input)
  input.gsub("[.]", ".").gsub("(.)", ".")
end