Class: UncleKryon::Countries
- Defined in:
- lib/unclekryon/iso/country.rb
Constant Summary collapse
- DEFAULT_FILEPATH =
"#{DEFAULT_DIR}/countries.yaml"
Constants inherited from BaseIsos
Instance Attribute Summary
Attributes inherited from BaseIsos
Class Method Summary collapse
- .load_file(filepath = DEFAULT_FILEPATH) ⇒ Object
- .parse_and_save_to_file(parse_filepath, save_filepath = DEFAULT_FILEPATH) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Countries
constructor
A new instance of Countries.
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
Constructor Details
#initialize ⇒ Countries
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
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 |