zipcoder 0.8.4 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +50 -6
- data/Rakefile +47 -17
- data/lib/data/county.csv +63212 -0
- data/lib/data/zip_data.yml +45121 -3043
- data/lib/zipcoder/cacher/base.rb +99 -16
- data/lib/zipcoder/ext/string.rb +4 -0
- data/lib/zipcoder/version.rb +1 -1
- data/lib/zipcoder.rb +10 -2
- data/spec/zipcoder_spec.rb +41 -0
- data/zipcoder.gemspec +2 -3
- metadata +23 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 382bf99325cd32e9e8289b9638333b4635f6d99b
|
4
|
+
data.tar.gz: 3c1261c069f7f08a92a233bfef54bd7aaa1a09a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36d427b2293cbd5bfb143937e836403a49a44b1da70d65930f579256e95d94816844fcdc0223b74ca36d1ed9eb7fd353cdcf0b932f0ab3b19f76019867cde9f8
|
7
|
+
data.tar.gz: d510c2d4fbca592ccbcc9b4dd4bc7a1c5fb7714d107197c010365b71e9163946bedd81e68fa05bc943074dfef6de0b9cd9f9be8d6ab4617586ee7d01a0ecf157
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -7,6 +7,14 @@ Gem for performing zip code lookup operations
|
|
7
7
|
|
8
8
|
## Revision History
|
9
9
|
|
10
|
+
- v0.9.0:
|
11
|
+
- added counties - note that there is a slight inaccuracy. When a city is in
|
12
|
+
multiple counties (for example Austin, TX), ALL of the zip codes for Austin
|
13
|
+
will list ALL of the counties that Austin is in, even though the individual
|
14
|
+
zip codes aren't necessarily in all of the counties. This is an artifact
|
15
|
+
of the data being from multiple sources
|
16
|
+
- added printing out the time it took to load the data for performance reasons
|
17
|
+
- updated copyright
|
10
18
|
- v0.8.4:
|
11
19
|
- fixed data corruption issue due to not cloning city before modifying
|
12
20
|
- v0.8.3:
|
@@ -54,6 +62,12 @@ Gem for performing zip code lookup operations
|
|
54
62
|
- v0.1.0:
|
55
63
|
- Initial Revision
|
56
64
|
|
65
|
+
## Data
|
66
|
+
The data for this library is taken from several different open source areas and combined together
|
67
|
+
|
68
|
+
- zip codes: [Federal Goverment Zip Codes](http://federalgovernmentzipcodes.us/free-zipcode-database.csv)
|
69
|
+
- counties: [grammakov Github](https://raw.githubusercontent.com/grammakov/USA-cities-and-states/master/us_cities_states_counties.csv)
|
70
|
+
|
57
71
|
## Installation
|
58
72
|
|
59
73
|
Add this line to your application's Gemfile:
|
@@ -113,7 +127,7 @@ cacher = Zipcoder::Cacher::Redis.new(**args)
|
|
113
127
|
Zipcoder.load_cache(cacher)
|
114
128
|
```
|
115
129
|
|
116
|
-
Please
|
130
|
+
Please visit [Redis Github](https://github.com/redis/redis-rb) for the different options
|
117
131
|
to use when instantiating the "Redis" client.
|
118
132
|
|
119
133
|
### Methods
|
@@ -154,11 +168,11 @@ require 'zipcoder'
|
|
154
168
|
|
155
169
|
# Looks up a zip code by string
|
156
170
|
puts "78748".zip_info
|
157
|
-
# > {:zip=>"78748", :city=>"
|
171
|
+
# > {:zip=>"78748", :city=>"Austin", :county=>"Travis,Williamson,Hays", :state=>"TX", :lat=>30.26, :long=>-97.74}
|
158
172
|
|
159
173
|
# Looks up a zip code by integer
|
160
174
|
puts 78748.zip_info
|
161
|
-
# > {:zip=>"78748", :city=>"Austin", :state=>"TX", :lat=>30.26, :long=>-97.74}
|
175
|
+
# > {:zip=>"78748", :city=>"Austin", :county=>"Travis,Williamson,Hays", :state=>"TX", :lat=>30.26, :long=>-97.74}
|
162
176
|
|
163
177
|
```
|
164
178
|
|
@@ -198,7 +212,7 @@ require 'zipcoder'
|
|
198
212
|
|
199
213
|
# Returns the cities for a zip_code
|
200
214
|
puts "78701-78750,78613".zip_cities
|
201
|
-
# > [{:zip=>"78701-78705,78710,78712,78717,...", :specified_zip => "78701-78750", :city=>"Austin", :state=>"TX", :lat=>30.26, :long=>-97.74}, ...
|
215
|
+
# > [{:zip=>"78701-78705,78710,78712,78717,...", :specified_zip => "78701-78750", :city=>"Austin", :county=>"Travis,...", :state=>"TX", :lat=>30.26, :long=>-97.74}, ...
|
202
216
|
|
203
217
|
# Returns just the name of the cities
|
204
218
|
puts "78701-78750,78613".zip_cities names_only: true
|
@@ -249,13 +263,13 @@ Zipcoder.city_info("Atlanta, GA", **args)
|
|
249
263
|
require 'zipcoder'
|
250
264
|
|
251
265
|
puts "Austin, TX".city_info
|
252
|
-
# > {:zip=>"78701-78705,78710,78712,78717,...", :city=>"
|
266
|
+
# > {:zip=>"78701-78705,78710,78712,78717,...", :city=>"Austin", :county=>"Travis,...", :state=>"TX", :lat=>30.26, :long=>-97.74}
|
253
267
|
|
254
268
|
puts "Austin, TX".city_info(zips_only: true)
|
255
269
|
# > ["78701", "78702", "78703", ...]
|
256
270
|
|
257
271
|
puts "Austin, TX".city_info(filter: "78701-78704,78748")
|
258
|
-
# > {:zip=>"78701-78705,78710,78712,78717,...", :specified_zip=> "78701-78704,78748", :city=>"
|
272
|
+
# > {:zip=>"78701-78705,78710,78712,78717,...", :specified_zip=> "78701-78704,78748", :city=>"Austin", :county=>"Travis,...", :state=>"TX", :lat=>30.26, :long=>-97.74}
|
259
273
|
```
|
260
274
|
|
261
275
|
#### Method: Zipcoder.state_cities(state, **args)
|
@@ -294,6 +308,36 @@ puts "TX".state_cities names_only: true
|
|
294
308
|
# > ["Abbott", ...
|
295
309
|
```
|
296
310
|
|
311
|
+
#### Method: Zipcoder.state_counties(state, **args)
|
312
|
+
|
313
|
+
This will return the cities in a state
|
314
|
+
|
315
|
+
**variations:**
|
316
|
+
|
317
|
+
``` ruby
|
318
|
+
Zipcoder.state_counties("GA", **args)
|
319
|
+
"GA".state_counties(**args)
|
320
|
+
```
|
321
|
+
|
322
|
+
**parameters:**
|
323
|
+
|
324
|
+
- state [String] - the 2 letter state abbreviation
|
325
|
+
- **return** [Array] - list of county names
|
326
|
+
|
327
|
+
**arguments:**
|
328
|
+
|
329
|
+
- none
|
330
|
+
|
331
|
+
**examples:**
|
332
|
+
|
333
|
+
``` ruby
|
334
|
+
require 'zipcoder'
|
335
|
+
|
336
|
+
# Returns List
|
337
|
+
puts "TX".state_counties
|
338
|
+
# > ["Anderson", ...
|
339
|
+
```
|
340
|
+
|
297
341
|
#### Method: Zipcoder.states
|
298
342
|
|
299
343
|
This will return the states in the US
|
data/Rakefile
CHANGED
@@ -13,30 +13,56 @@ namespace :zipcoder do
|
|
13
13
|
desc "Pulls the latest zip code data base file"
|
14
14
|
task :update do
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
16
|
+
def download(url, output)
|
17
|
+
puts "Downloading data from '#{url}'"
|
18
|
+
puts " writing to the file '#{output}'"
|
19
|
+
|
20
|
+
# Download the file
|
21
|
+
uri = URI(url)
|
22
|
+
doc = Net::HTTP.get(uri)
|
23
|
+
|
24
|
+
# Write the file to the file system
|
25
|
+
File.open(output, 'w') { |file| file.write(doc.to_s) }
|
26
|
+
end
|
27
|
+
|
28
|
+
# Fetch the latest zip code file
|
29
|
+
download "http://federalgovernmentzipcodes.us/free-zipcode-database.csv", "lib/data/zipcode.csv"
|
30
|
+
|
31
|
+
# Fetch the latest county file
|
32
|
+
download "https://raw.githubusercontent.com/grammakov/USA-cities-and-states/master/us_cities_states_counties.csv", "lib/data/county.csv"
|
25
33
|
end
|
26
34
|
|
27
35
|
desc "Converts the database file into the formats expected for the library"
|
28
36
|
task :convert do
|
29
37
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
38
|
+
def open_file(filename)
|
39
|
+
puts "Importing data from '#{filename}'"
|
40
|
+
File.read(filename)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Open the county files
|
44
|
+
county_csv_text = open_file "lib/data/county.csv"
|
45
|
+
city_county = {}
|
46
|
+
county_csv_text.split("\n").each do |line|
|
47
|
+
components = line.split("|")
|
48
|
+
next if components.length < 5
|
49
|
+
state = components[1]
|
50
|
+
county = components[3]
|
51
|
+
city = components[4]
|
52
|
+
|
53
|
+
key = "#{city.upcase},#{state}"
|
54
|
+
counties = city_county[key] || []
|
55
|
+
counties << county.capitalize
|
56
|
+
city_county[key] = counties.uniq
|
57
|
+
end
|
58
|
+
|
59
|
+
# Open the zip file
|
60
|
+
zip_csv_text = open_file "lib/data/zipcode.csv"
|
34
61
|
|
35
62
|
# Import the CSV file and build the data structure
|
36
63
|
zip_lookup_data = {}
|
37
|
-
|
38
|
-
|
39
|
-
csv.each do |row|
|
64
|
+
zip_csv = CSV.parse(zip_csv_text, :headers => true)
|
65
|
+
zip_csv.each do |row|
|
40
66
|
next if row["ZipCodeType"] != "STANDARD" or not (["PRIMARY", "ACCEPTABLE"].include? row["LocationType"])
|
41
67
|
|
42
68
|
zip_code = row["Zipcode"]
|
@@ -46,9 +72,13 @@ namespace :zipcoder do
|
|
46
72
|
lat = row["Lat"].to_f
|
47
73
|
long = row["Long"].to_f
|
48
74
|
|
75
|
+
# Pull the county
|
76
|
+
key = "#{city.upcase},#{state}"
|
77
|
+
county = (city_county[key] || []).join(",")
|
78
|
+
|
49
79
|
# Write the zip_lookup_data
|
50
80
|
areas = zip_lookup_data[zip_code] || []
|
51
|
-
areas << { zip: zip_code, city: city, state: state, lat: lat, long: long, primary: primary }
|
81
|
+
areas << { zip: zip_code, city: city, county: county, state: state, lat: lat, long: long, primary: primary }
|
52
82
|
zip_lookup_data[zip_code] = areas
|
53
83
|
end
|
54
84
|
|