Module: Zipcoder
- Defined in:
- lib/zipcoder.rb,
lib/zipcoder/version.rb,
lib/zipcoder/cacher/base.rb,
lib/zipcoder/cacher/redis.rb,
lib/zipcoder/cacher/memory.rb
Defined Under Namespace
Modules: Cacher Classes: ZipcoderError
Constant Summary collapse
- VERSION =
"0.5.0"- @@cacher =
nil
Class Method Summary collapse
-
._cache_key(city_state) ⇒ Object
Returns a cache key.
-
._check_zip(zip) ⇒ Object
Check the zip codes.
-
._filter_hash_args(hash, keys) ⇒ Object
Filters arguments in return hash.
-
._parse_zip_string(zip_string) ⇒ Object
Parses a zip code string and returns all of the zip codes as an array.
- .cacher ⇒ Object
-
.city_info(city_state, **kwargs) ⇒ Object
Looks up city information.
-
.load_cache(cacher = nil) ⇒ Object
Loads the data into memory.
-
.state_cities(state, **kwargs) ⇒ Object
Returns the cities in a state.
-
.states ⇒ Object
Returns the states.
-
.zip_cities(zip_string, **kwargs) ⇒ Object
Returns the cities that contain the zip codes.
-
.zip_info(zip = nil, **kwargs) ⇒ Object
Looks up zip code information.
Class Method Details
._cache_key(city_state) ⇒ Object
Returns a cache key
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/zipcoder.rb', line 134 def self._cache_key(city_state) unless city_state.include? ',' raise ZipcoderError, "city/state must include ','" end components = city_state.split(',') city = components[0].strip.upcase state = components[1].strip.upcase "#{city},#{state}" end |
._check_zip(zip) ⇒ Object
Check the zip codes
166 167 168 169 170 171 |
# File 'lib/zipcoder.rb', line 166 def self._check_zip(zip) if zip.length != 5 raise ZipcoderError, "zip code #{zip} is not 5 characters" end zip end |
._filter_hash_args(hash, keys) ⇒ Object
Filters arguments in return hash
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/zipcoder.rb', line 122 def self._filter_hash_args(hash, keys) return nil if hash == nil if keys != nil new_hash = {} keys.each { |k| new_hash[k] = hash[k] } hash = new_hash end hash end |
._parse_zip_string(zip_string) ⇒ Object
Parses a zip code string and returns all of the zip codes as an array
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/zipcoder.rb', line 148 def self._parse_zip_string(zip_string) zips = [] zip_string.split(",").each do |zip_component| if zip_component.include? "-" z = zip_component.split("-") (z[0].strip.to_i..z[1].strip.to_i).each do |zip| zips << self._check_zip(zip.to_zip) end else zips << self._check_zip(zip_component.strip) end end zips.sort.uniq end |
.cacher ⇒ Object
13 14 15 16 17 18 |
# File 'lib/zipcoder.rb', line 13 def self.cacher if @@cacher == nil self.load_cache end @@cacher end |
.city_info(city_state, **kwargs) ⇒ Object
Looks up city information
86 87 88 89 90 91 92 93 |
# File 'lib/zipcoder.rb', line 86 def self.city_info(city_state, **kwargs) # Get the city from the cache cache_key = self._cache_key(city_state) cached_value = self.cacher.read_city_cache(cache_key) # Return it self._filter_hash_args cached_value, kwargs[:keys] end |
.load_cache(cacher = nil) ⇒ Object
Loads the data into memory
21 22 23 24 |
# File 'lib/zipcoder.rb', line 21 def self.load_cache(cacher=nil) @@cacher = cacher || Cacher::Memory.new self.cacher.load end |
.state_cities(state, **kwargs) ⇒ Object
Returns the cities in a state
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/zipcoder.rb', line 96 def self.state_cities(state, **kwargs) state = state.strip.upcase names_only = kwargs[:names_only] keys = kwargs[:keys] # Filter the returned cities cities = self.cacher.read_state_cache(state) if names_only cities else infos = [] self.cacher.read_state_cache(state).each { |city| infos << self.city_info("#{city}, #{state}", keys: keys) } infos end end |
.states ⇒ Object
Returns the states
117 118 119 |
# File 'lib/zipcoder.rb', line 117 def self.states self.cacher.read_states end |
.zip_cities(zip_string, **kwargs) ⇒ Object
Returns the cities that contain the zip codes
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 |
# File 'lib/zipcoder.rb', line 55 def self.zip_cities(zip_string, **kwargs) max = kwargs[:max] cities = {} self._parse_zip_string(zip_string).each do |zip| info = zip.zip_info if info == nil next end cities["#{info[:city]}, #{info[:state]}"] = true if max != nil and cities.keys.count >= max break end end cities = cities.keys.uniq.sort if kwargs[:names_only] zips = cities.map{ |x| x.split(",")[0].strip } else zips = [] cities.each do |key| zips << key.city_info(keys: kwargs[:keys]) end end zips end |
.zip_info(zip = nil, **kwargs) ⇒ Object
Looks up zip code information
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/zipcoder.rb', line 27 def self.zip_info(zip=nil, **kwargs) # If zip is not nil, then we are returning a single value if zip != nil # Get the info info = self.cacher.read_zip_cache(zip.to_zip) # Filter to the included keys self._filter_hash_args info, kwargs[:keys] else # If zip is nil, then we are returning an array of values city_filter = kwargs[:city] != nil ? kwargs[:city].upcase : nil state_filter = kwargs[:state] != nil ? kwargs[:state].upcase : nil # Iterate through and only add the ones that match the filters infos = [] self.cacher.iterate_zips do |info| if (city_filter == nil or info[:city].upcase == city_filter) and (state_filter == nil or info[:state].upcase == state_filter) infos << self._filter_hash_args(info, kwargs[:keys]) end end infos end end |