Class: Zipcoder::Cacher::Base
- Inherits:
-
Object
- Object
- Zipcoder::Cacher::Base
- Defined in:
- lib/zipcoder/cacher/base.rb
Overview
The cacher base class places all of the objects in memory. The abstraction will later allow us to override for MemCacher and Redis implementations
Constant Summary collapse
- KEY_BASE =
"zipcoder"- KEY_ZIP =
"#{KEY_BASE}:zip"- KEY_CITY =
"#{KEY_BASE}:city"- KEY_STATE =
"#{KEY_BASE}:state"- 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_cache(state) ⇒ Object
- #read_states ⇒ Object
- #read_zip_cache(zip) ⇒ Object
Constructor Details
#initialize(**kwargs) ⇒ Base
endregion
38 39 40 |
# File 'lib/zipcoder/cacher/base.rb', line 38 def initialize(**kwargs) self._init_cache **kwargs end |
Instance Method Details
#_empty_cache ⇒ Object
20 21 22 |
# File 'lib/zipcoder/cacher/base.rb', line 20 def _empty_cache # Override ME end |
#_init_cache(**kwargs) ⇒ Object
region Override These
16 17 18 |
# File 'lib/zipcoder/cacher/base.rb', line 16 def _init_cache(**kwargs) # Override ME end |
#_iterate_keys(**kwargs, &block) ⇒ Object
32 33 34 |
# File 'lib/zipcoder/cacher/base.rb', line 32 def _iterate_keys(**kwargs, &block) # Override ME end |
#_read_cache(key) ⇒ Object
28 29 30 |
# File 'lib/zipcoder/cacher/base.rb', line 28 def _read_cache(key) # Override ME end |
#_write_cache(key, value) ⇒ Object
24 25 26 |
# File 'lib/zipcoder/cacher/base.rb', line 24 def _write_cache(key, value) # Override ME end |
#iterate_cities(&block) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/zipcoder/cacher/base.rb', line 118 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
110 111 112 113 114 115 116 |
# File 'lib/zipcoder/cacher/base.rb', line 110 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
42 43 44 45 46 47 48 49 50 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 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/zipcoder/cacher/base.rb', line 42 def load 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_lookup = {} # Add the zip codes to the cache zip_codes.each do |zip, info| city = _capitalize_all(info[:city]) info[:city] = city state = info[:state] # Iterate through the zip codes and add them to the zip cache _write_cache _zip_cache(zip), info # Create the city lookups city_state = "#{city.upcase},#{state.upcase}" infos = city_states[city_state] || [] infos << info city_states[city_state] = infos 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 Cache cities = state_lookup[state] || [] cities << city state_lookup[state] = cities end # Set the cities cache state_lookup.each do |state, cities| _write_cache _state_cache(state), cities.sort end # Set the states cache self._write_cache _states, state_lookup.keys.sort end |
#read_city_cache(city_state) ⇒ Object
98 99 100 |
# File 'lib/zipcoder/cacher/base.rb', line 98 def read_city_cache(city_state) _read_cache _city_cache(city_state) end |
#read_state_cache(state) ⇒ Object
102 103 104 |
# File 'lib/zipcoder/cacher/base.rb', line 102 def read_state_cache(state) _read_cache _state_cache(state) end |
#read_states ⇒ Object
106 107 108 |
# File 'lib/zipcoder/cacher/base.rb', line 106 def read_states _read_cache _states end |
#read_zip_cache(zip) ⇒ Object
94 95 96 |
# File 'lib/zipcoder/cacher/base.rb', line 94 def read_zip_cache(zip) _read_cache _zip_cache(zip) end |