Class: Zipcoder::Cacher::Base
- Inherits:
-
Object
- Object
- Zipcoder::Cacher::Base
- Defined in:
- lib/zipcoder/cacher/base.rb
Constant Summary collapse
- KEY_BASE =
"zipcoder"
- KEY_ZIP =
"#{KEY_BASE}:zip"
- KEY_CITY =
"#{KEY_BASE}:city"
- KEY_COUNTY =
"#{KEY_BASE}:county"
- KEY_STATE_CITIES =
"#{KEY_BASE}:state:cities"
- KEY_STATE_COUNTIES =
"#{KEY_BASE}:state:counties"
- KEY_STATES =
"#{KEY_BASE}:states"
Instance Method Summary collapse
- #_empty_cache ⇒ Object
-
#_init_cache(**kwargs) ⇒ Object
region Override These.
- #_iterate_keys(**kwargs, &block) ⇒ Object
- #_read_cache(key) ⇒ Object
- #_write_cache(key, value) ⇒ Object
-
#initialize(**kwargs) ⇒ Base
constructor
endregion.
- #iterate_cities(&block) ⇒ Object
- #iterate_zips(&block) ⇒ Object
- #load ⇒ Object
- #read_city_cache(city_state) ⇒ Object
- #read_state_cities_cache(state) ⇒ Object
- #read_state_counties_cache(state) ⇒ Object
- #read_states ⇒ Object
- #read_zip_cache(zip) ⇒ Object
Constructor Details
#initialize(**kwargs) ⇒ Base
endregion
93 94 95 |
# File 'lib/zipcoder/cacher/base.rb', line 93 def initialize(**kwargs) self._init_cache **kwargs end |
Instance Method Details
#_empty_cache ⇒ Object
75 76 77 |
# File 'lib/zipcoder/cacher/base.rb', line 75 def _empty_cache # Override ME end |
#_init_cache(**kwargs) ⇒ Object
region Override These
71 72 73 |
# File 'lib/zipcoder/cacher/base.rb', line 71 def _init_cache(**kwargs) # Override ME end |
#_iterate_keys(**kwargs, &block) ⇒ Object
87 88 89 |
# File 'lib/zipcoder/cacher/base.rb', line 87 def _iterate_keys(**kwargs, &block) # Override ME end |
#_read_cache(key) ⇒ Object
83 84 85 |
# File 'lib/zipcoder/cacher/base.rb', line 83 def _read_cache(key) # Override ME end |
#_write_cache(key, value) ⇒ Object
79 80 81 |
# File 'lib/zipcoder/cacher/base.rb', line 79 def _write_cache(key, value) # Override ME end |
#iterate_cities(&block) ⇒ Object
197 198 199 200 201 202 203 |
# File 'lib/zipcoder/cacher/base.rb', line 197 def iterate_cities(&block) return if block == nil _iterate_keys(start_with: "zipcoder:city") do |key| info = _read_cache(key) block.call(info) if block != nil end end |
#iterate_zips(&block) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/zipcoder/cacher/base.rb', line 189 def iterate_zips(&block) return if block == nil _iterate_keys(start_with: "zipcoder:zip") do |key| info = _read_cache(key) block.call(info) if block != nil end end |
#load ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/zipcoder/cacher/base.rb', line 97 def load start_time = Time.now this_dir = File.(File.dirname(__FILE__)) # Load zip cache from file zip_data = File.join(this_dir, '..', '..', 'data', 'zip_data.yml') zip_codes = YAML.load(File.open(zip_data)) # Initialize _empty_cache city_states = {} state_cities_lookup = {} state_counties_lookup = {} # Add the zip codes to the cache zip_codes.each do |zip, cities| cities.each do |info| city = _capitalize_all(info[:city]) info[:city] = city state = info[:state] # For the zip codes, only store the primary if info[:primary] _write_cache _zip_cache(zip), info end # Create the city lookups city_state = "#{city.upcase},#{state.upcase}" infos = city_states[city_state] || [] infos << info city_states[city_state] = infos end end # Normalize each city and populate the state cache city_states.each do |city_state, infos| city = infos[0][:city] state = infos[0][:state] # Populate the City Cache normalized = _normalize_city(infos) _write_cache _city_cache(city_state), normalized # Populate the State City Cache cities = state_cities_lookup[state] || [] cities << city state_cities_lookup[state] = cities # Populate the State Counties Cache counties = state_counties_lookup[state] || [] counties += normalized[:county].split(",") state_counties_lookup[state] = counties end # Set the cities cache state_cities_lookup.each do |state, cities| _write_cache _state_cities_cache(state), cities.sort end # Set the cities cache state_counties_lookup.each do |state, counties| _write_cache _state_counties_cache(state), counties.sort.uniq end # Set the states cache self._write_cache _states, state_cities_lookup.keys.sort # Print the alpsed time puts "ZipCoder initialization time: #{Time.now-start_time}" end |
#read_city_cache(city_state) ⇒ Object
173 174 175 |
# File 'lib/zipcoder/cacher/base.rb', line 173 def read_city_cache(city_state) _read_cache _city_cache(city_state) end |
#read_state_cities_cache(state) ⇒ Object
177 178 179 |
# File 'lib/zipcoder/cacher/base.rb', line 177 def read_state_cities_cache(state) _read_cache _state_cities_cache(state) end |
#read_state_counties_cache(state) ⇒ Object
181 182 183 |
# File 'lib/zipcoder/cacher/base.rb', line 181 def read_state_counties_cache(state) _read_cache _state_counties_cache(state) end |
#read_states ⇒ Object
185 186 187 |
# File 'lib/zipcoder/cacher/base.rb', line 185 def read_states _read_cache _states end |
#read_zip_cache(zip) ⇒ Object
169 170 171 |
# File 'lib/zipcoder/cacher/base.rb', line 169 def read_zip_cache(zip) _read_cache _zip_cache(zip) end |