Class: UncleKryon::Countries

Inherits:
BaseIsos show all
Defined in:
lib/unclekryon/iso/country.rb

Constant Summary collapse

DEFAULT_FILEPATH =
"#{DEFAULT_DIR}/countries.yaml"

Constants inherited from BaseIsos

BaseIsos::DEFAULT_DIR

Instance Attribute Summary

Attributes inherited from BaseIsos

#id, #values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseIsos

#[], #[]=, #find, #find_by_code, #find_by_name, get_class_name, #key?, #load_file, #save_to_file, #sort_keys!, #to_s

Methods included from Logging

#init_log, #log

Constructor Details

#initializeCountries

Returns a new instance of Countries.



66
67
68
# File 'lib/unclekryon/iso/country.rb', line 66

def initialize
  super()
end

Class Method Details

.load_file(filepath = DEFAULT_FILEPATH) ⇒ Object



70
71
72
# File 'lib/unclekryon/iso/country.rb', line 70

def self.load_file(filepath=DEFAULT_FILEPATH)
  return Countries.new.load_file(filepath)
end

.parse_and_save_to_file(parse_filepath, save_filepath = DEFAULT_FILEPATH) ⇒ Object

Parameters:

  • parse_filepath (String)

    use web browser’s developer tools to copy & paste table HTML into local file

  • save_filepath (String) (defaults to: DEFAULT_FILEPATH)

    local file to save YAML to

See Also:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/unclekryon/iso/country.rb', line 78

def self.parse_and_save_to_file(parse_filepath,save_filepath=DEFAULT_FILEPATH)
  doc = Nokogiri::HTML(URI(parse_filepath).open,nil,'utf-8')
  tds = doc.css('td')

  countries = Countries.new
  i = 0
  tr = []

  tds.each do |td|
    c = td.content
    c.gsub!(/[[:space:]]+/,' ')
    c.strip!
    tr.push(c)

    if (i += 1) >= 5
      #puts tr.inspect()
      country = Country.new(tr)
      raise "Country already exists: #{country.inspect}" if countries.key?(country.code)

      countries.values.each_value do |v|
        puts "Duplicate country names: #{v.name}" if v.name == country.name
      end

      countries[country.code] = country
      tr.clear
      i = 0
    end
  end

  countries.sort_keys!
  countries.save_to_file(save_filepath)
end