Class: Zipcoder::Cacher::Base

Inherits:
Object
  • Object
show all
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

Direct Known Subclasses

Memory, Redis

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

Constructor Details

#initialize(**kwargs) ⇒ Base

endregion



36
37
38
# File 'lib/zipcoder/cacher/base.rb', line 36

def initialize(**kwargs)
  self._init_cache **kwargs
end

Instance Method Details

#_empty_cacheObject



18
19
20
# File 'lib/zipcoder/cacher/base.rb', line 18

def _empty_cache
  # Override ME
end

#_init_cache(**kwargs) ⇒ Object

region Override These



14
15
16
# File 'lib/zipcoder/cacher/base.rb', line 14

def _init_cache(**kwargs)
  # Override ME
end

#_iterate_keys(**kwargs, &block) ⇒ Object



30
31
32
# File 'lib/zipcoder/cacher/base.rb', line 30

def _iterate_keys(**kwargs, &block)
  # Override ME
end

#_read_cache(key) ⇒ Object



26
27
28
# File 'lib/zipcoder/cacher/base.rb', line 26

def _read_cache(key)
  # Override ME
end

#_write_cache(key, value) ⇒ Object



22
23
24
# File 'lib/zipcoder/cacher/base.rb', line 22

def _write_cache(key, value)
  # Override ME
end

#iterate_cities(&block) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/zipcoder/cacher/base.rb', line 116

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



108
109
110
111
112
113
114
# File 'lib/zipcoder/cacher/base.rb', line 108

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

#loadObject



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

def load
  this_dir = File.expand_path(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



96
97
98
# File 'lib/zipcoder/cacher/base.rb', line 96

def read_city_cache(city_state)
  _read_cache _city_cache(city_state)
end

#read_state_cache(state) ⇒ Object



100
101
102
# File 'lib/zipcoder/cacher/base.rb', line 100

def read_state_cache(state)
  _read_cache _state_cache(state)
end

#read_statesObject



104
105
106
# File 'lib/zipcoder/cacher/base.rb', line 104

def read_states
  _read_cache _states
end

#read_zip_cache(zip) ⇒ Object



92
93
94
# File 'lib/zipcoder/cacher/base.rb', line 92

def read_zip_cache(zip)
  _read_cache _zip_cache(zip)
end