class UncleKryon::UsaStates

Constants

DEFAULT_FILEPATH

Public Class Methods

load_file(filepath=DEFAULT_FILEPATH) click to toggle source
# File lib/unclekryon/iso/usa_state.rb, line 43
def self.load_file(filepath=DEFAULT_FILEPATH)
  return UsaStates.new.load_file(filepath)
end
new() click to toggle source
Calls superclass method UncleKryon::BaseIsos::new
# File lib/unclekryon/iso/usa_state.rb, line 37
def initialize
  super()

  @id = 'USA States'
end
parse_and_save_to_file(parse_filepath,save_filepath=DEFAULT_FILEPATH) click to toggle source

@param parse_filepath [String] use web browser's developer tools to copy & paste table HTML

into local file

@param save_filepath [String] local file to save YAML to @see www.iso.org/obp/ui/#iso:code:3166:US

# File lib/unclekryon/iso/usa_state.rb, line 51
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')

  states = UsaStates.new
  i = 0
  tr = []

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

    if (i += 1) >= 7
      #puts tr.inspect()
      state = UsaState.new(tr)
      raise "USA state already exists: #{state.inspect}" if states.key?(state.code)

      states.values.each_value do |v|
        puts "Duplicate USA state names: #{v.name}" if v.name == state.name
      end

      states[state.code] = state
      tr.clear
      i = 0
    end
  end

  states.sort_keys!
  states.save_to_file(save_filepath)
end