Class: UncleKryon::UsaStates
- Defined in:
- lib/unclekryon/iso/usa_state.rb
Constant Summary collapse
- DEFAULT_FILEPATH =
"#{DEFAULT_DIR}/usa_states.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 ⇒ UsaStates
constructor
A new instance of UsaStates.
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 ⇒ UsaStates
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
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 |