Class: Zipcoder::Cacher::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/zipcoder/cacher/base.rb

Direct Known Subclasses

Memory, Redis

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

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_cacheObject



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



199
200
201
202
203
204
205
# File 'lib/zipcoder/cacher/base.rb', line 199

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



191
192
193
194
195
196
197
# File 'lib/zipcoder/cacher/base.rb', line 191

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



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
168
169
# File 'lib/zipcoder/cacher/base.rb', line 97

def load(data: nil)
  start_time = Time.now

  # Load zip cache from file
  if data != nil
    zip_data = File.open(data)
  else
    this_dir = File.expand_path(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}"
end

#read_city_cache(city_state) ⇒ Object



175
176
177
# File 'lib/zipcoder/cacher/base.rb', line 175

def read_city_cache(city_state)
  _read_cache _city_cache(city_state)
end

#read_state_cities_cache(state) ⇒ Object



179
180
181
# File 'lib/zipcoder/cacher/base.rb', line 179

def read_state_cities_cache(state)
  _read_cache _state_cities_cache(state)
end

#read_state_counties_cache(state) ⇒ Object



183
184
185
# File 'lib/zipcoder/cacher/base.rb', line 183

def read_state_counties_cache(state)
  _read_cache _state_counties_cache(state)
end

#read_statesObject



187
188
189
# File 'lib/zipcoder/cacher/base.rb', line 187

def read_states
  _read_cache _states
end

#read_zip_cache(zip) ⇒ Object



171
172
173
# File 'lib/zipcoder/cacher/base.rb', line 171

def read_zip_cache(zip)
  _read_cache _zip_cache(zip)
end