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 Attribute Summary collapse
-
#loaded ⇒ Object
Returns the value of attribute loaded.
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(data: nil) ⇒ 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
94 95 96 97 |
# File 'lib/zipcoder/cacher/base.rb', line 94 def initialize(**kwargs) self._init_cache **kwargs self.loaded = false end |
Instance Attribute Details
#loaded ⇒ Object
Returns the value of attribute loaded.
61 62 63 |
# File 'lib/zipcoder/cacher/base.rb', line 61 def loaded @loaded end |
Instance Method Details
#_empty_cache ⇒ Object
76 77 78 |
# File 'lib/zipcoder/cacher/base.rb', line 76 def _empty_cache # Override ME end |
#_init_cache(**kwargs) ⇒ Object
region Override These
72 73 74 |
# File 'lib/zipcoder/cacher/base.rb', line 72 def _init_cache(**kwargs) # Override ME end |
#_iterate_keys(**kwargs, &block) ⇒ Object
88 89 90 |
# File 'lib/zipcoder/cacher/base.rb', line 88 def _iterate_keys(**kwargs, &block) # Override ME end |
#_read_cache(key) ⇒ Object
84 85 86 |
# File 'lib/zipcoder/cacher/base.rb', line 84 def _read_cache(key) # Override ME end |
#_write_cache(key, value) ⇒ Object
80 81 82 |
# File 'lib/zipcoder/cacher/base.rb', line 80 def _write_cache(key, value) # Override ME end |
#iterate_cities(&block) ⇒ Object
205 206 207 208 209 210 211 |
# File 'lib/zipcoder/cacher/base.rb', line 205 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
197 198 199 200 201 202 203 |
# File 'lib/zipcoder/cacher/base.rb', line 197 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(data: nil) ⇒ Object
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 168 169 170 171 172 173 174 175 |
# File 'lib/zipcoder/cacher/base.rb', line 99 def load(data: nil) return if self.loaded start_time = Time.now # Load zip cache from file if data != nil zip_data = File.open(data) else this_dir = File.(File.dirname(__FILE__)) zip_data = File.join(this_dir, '..', '..', 'data', 'data.yml' ) end 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 = info[: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}" self.loaded = true end |
#read_city_cache(city_state) ⇒ Object
181 182 183 |
# File 'lib/zipcoder/cacher/base.rb', line 181 def read_city_cache(city_state) _read_cache _city_cache(city_state) end |
#read_state_cities_cache(state) ⇒ Object
185 186 187 |
# File 'lib/zipcoder/cacher/base.rb', line 185 def read_state_cities_cache(state) _read_cache _state_cities_cache(state) end |
#read_state_counties_cache(state) ⇒ Object
189 190 191 |
# File 'lib/zipcoder/cacher/base.rb', line 189 def read_state_counties_cache(state) _read_cache _state_counties_cache(state) end |
#read_states ⇒ Object
193 194 195 |
# File 'lib/zipcoder/cacher/base.rb', line 193 def read_states _read_cache _states end |
#read_zip_cache(zip) ⇒ Object
177 178 179 |
# File 'lib/zipcoder/cacher/base.rb', line 177 def read_zip_cache(zip) _read_cache _zip_cache(zip) end |