Class: CountryConvert

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

Instance Method Summary collapse

Constructor Details

#initialize(input, countryfield) ⇒ CountryConvert

Returns a new instance of CountryConvert.



4
5
6
7
8
# File 'lib/countryconvert.rb', line 4

def initialize(input, countryfield)
  @input = JSON.parse(input)
  @countryfield = countryfield
  @isocodes = JSON.parse(File.read("isocodes.json"))
end

Instance Method Details

#convertObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/countryconvert.rb', line 10

def convert
  finaldata = Array.new
  @input.each do |f|
    if @isocodes[f[@countryfield].upcase]
      threeletter = @isocodes[f[@countryfield].upcase]
      f[@countryfield] = threeletter
      finaldata.push(f)
    end
  end
  return JSON.pretty_generate(finaldata)
end