Class: 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

Instance Method Summary collapse

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_cacheObject



12
13
14
# File 'lib/zipcoder/cacher/base.rb', line 12

def _empty_cache
  @cache.clear
end

#_init_cache(**kwargs) ⇒ Object

region Override These



8
9
10
# File 'lib/zipcoder/cacher/base.rb', line 8

def _init_cache(**kwargs)
  @cache = {}
end

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



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zipcoder/cacher/base.rb', line 24

def _iterate_keys(**kwargs, &block)
  return if block == nil

  start_with = kwargs[:start_with]

  @cache.keys.each do |key|
    if start_with == nil or key.start_with?(start_with)
      block.call(key)
    end
  end
end

#_read_cache(key) ⇒ Object



20
21
22
# File 'lib/zipcoder/cacher/base.rb', line 20

def _read_cache(key)
  @cache[key]
end

#_write_cache(key, value) ⇒ Object



16
17
18
# File 'lib/zipcoder/cacher/base.rb', line 16

def _write_cache(key, value)
  @cache[key] = value
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

#loadObject



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.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 = info[:city].capitalize_all
    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_statesObject



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