Class: UncleKryon::UsaStates

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

Constant Summary collapse

DEFAULT_FILEPATH =
"#{DEFAULT_DIR}/usa_states.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

#initializeUsaStates

Returns a new instance of UsaStates.



37
38
39
40
41
# File 'lib/unclekryon/iso/usa_state.rb', line 37

def initialize
  super()

  @id = 'USA States'
end

Class Method Details

.load_file(filepath = DEFAULT_FILEPATH) ⇒ Object



43
44
45
# File 'lib/unclekryon/iso/usa_state.rb', line 43

def self.load_file(filepath=DEFAULT_FILEPATH)
  return UsaStates.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:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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