Class: Kokki::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/kokki/converter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Converter

Returns a new instance of Converter.



9
10
11
12
13
# File 'lib/kokki/converter.rb', line 9

def initialize(input)
  @input = input
  @upcase = input.upcase
  @dict = Dictionary.new
end

Instance Attribute Details

#dictObject (readonly)

Returns the value of attribute dict.



7
8
9
# File 'lib/kokki/converter.rb', line 7

def dict
  @dict
end

#inputObject (readonly)

Returns the value of attribute input.



7
8
9
# File 'lib/kokki/converter.rb', line 7

def input
  @input
end

#upcaseObject (readonly)

Returns the value of attribute upcase.



7
8
9
# File 'lib/kokki/converter.rb', line 7

def upcase
  @upcase
end

Class Method Details

.convert(input) ⇒ Object



28
29
30
# File 'lib/kokki/converter.rb', line 28

def self.convert(input)
  new(input).convert
end

Instance Method Details

#convertObject

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kokki/converter.rb', line 15

def convert
  flag = nil

  flag = dict.lookup_by_alpha_2_code(upcase) if input.length == 2
  flag = dict.lookup_by_alpha_3_code(upcase) if input.length == 3
  flag ||= dict.lookup_by_name(upcase)
  flag ||= convert_as_ip_address(input)

  raise InvalidInputError, "invalid input: #{input}" unless flag

  flag
end