zipcoder 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +132 -0
- data/Rakefile +77 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/circle.yml +3 -0
- data/lib/data/city_lookup.yml +306283 -0
- data/lib/data/zip_lookup.yml +255133 -0
- data/lib/data/zipcode.csv +42523 -0
- data/lib/ext/integer.rb +8 -0
- data/lib/ext/string.rb +34 -0
- data/lib/zipcoder/version.rb +3 -0
- data/lib/zipcoder.rb +32 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/zipcoder_spec.rb +68 -0
- data/zipcoder.gemspec +28 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 33e7e2b9eb15e35a4b1248e3a16772d6763880e7
|
4
|
+
data.tar.gz: db823f8dccbfb1292b85be3bd2111e96f237f748
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: db14e8d94211f815d508eaaf375ffbe2dc5472a31c525597d3d1de9d3519f0a3b0e0502a44bd8467fa86603b6cabeab49dbea7bb1f647eed653dc62b6c584a59
|
7
|
+
data.tar.gz: 672abf7c9f868cc4fe0083472e66b1ad3989155f1829bd04181db34985bdc3c7fa0b83f4c138a5bb8ef3160a8f52872f3f1d31968be0363c743cd1aaa2ba5ce0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Eric Chapman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Zipcoder
|
2
|
+
[](https://badge.fury.io/rb/zipcoder)
|
3
|
+
[](https://circleci.com/gh/ericchapman/zipcoder/tree/master)
|
4
|
+
[](https://codecov.io/github/ericchapman/zipcoder)
|
5
|
+
|
6
|
+
Gem for performing zip code lookup operations
|
7
|
+
|
8
|
+
## Revision History
|
9
|
+
|
10
|
+
- v0.1.0:
|
11
|
+
- Initial Revision
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'zipcoder'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install zipcoder
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
### Setup
|
32
|
+
|
33
|
+
By default, the library will lazy-load the data structures when you first try
|
34
|
+
to use it. If you would like to have the data structures already loaded
|
35
|
+
when application is loaded, you can do the following RAILS example
|
36
|
+
|
37
|
+
**config/initializers/zipcoder.rb:**
|
38
|
+
|
39
|
+
``` ruby
|
40
|
+
require 'zipcoder'
|
41
|
+
Zipcoder.load_data
|
42
|
+
```
|
43
|
+
|
44
|
+
This will immediately load the data structures
|
45
|
+
|
46
|
+
### Methods
|
47
|
+
|
48
|
+
The library overrides the String (and in some instances Integer) class to
|
49
|
+
provide the following methods
|
50
|
+
|
51
|
+
#### Lookup Zip Code
|
52
|
+
|
53
|
+
This will return information about the zip code
|
54
|
+
|
55
|
+
``` ruby
|
56
|
+
require 'zipcoder'
|
57
|
+
|
58
|
+
puts "78748".zip_info
|
59
|
+
# > {:zip=>"78748", :city=>"AUSTIN", :state=>"TX", :lat=>30.26, :long=>-97.74}
|
60
|
+
```
|
61
|
+
|
62
|
+
Note that this also works with Integer zip codes, for example
|
63
|
+
|
64
|
+
``` ruby
|
65
|
+
require 'zipcoder'
|
66
|
+
|
67
|
+
puts 78748.zip_info
|
68
|
+
# > {:zip=>"78748", :city=>"AUSTIN", :state=>"TX", :lat=>30.26, :long=>-97.74}
|
69
|
+
```
|
70
|
+
|
71
|
+
You can filter the keys that are returned by including the "keys" argument
|
72
|
+
as shown below
|
73
|
+
|
74
|
+
``` ruby
|
75
|
+
require 'zipcoder'
|
76
|
+
|
77
|
+
puts "78748".zip_info(keys: [:city, :state])
|
78
|
+
# > {:city=>"AUSTIN", :state=>"TX"}
|
79
|
+
```
|
80
|
+
|
81
|
+
#### Lookup City
|
82
|
+
|
83
|
+
This will return info about a city
|
84
|
+
|
85
|
+
``` ruby
|
86
|
+
require 'zipcoder'
|
87
|
+
|
88
|
+
puts "Austin, TX".city_info
|
89
|
+
# > {:zips=>["73301", ...], :city=>"AUSTIN", :state=>"TX", :lats=>[30.26, ...], :longs=>[-97.74, ...]}
|
90
|
+
```
|
91
|
+
|
92
|
+
The "zips", "lats", and "longs" are all arrays where each value is representing
|
93
|
+
the info for a specific zip code.
|
94
|
+
|
95
|
+
Note that the library will normalize the key by removing all of the whitespace
|
96
|
+
and capitalizing the letters. So for example, "Austin, TX" becomes "AUSTIN,TX".
|
97
|
+
|
98
|
+
You can filter the keys that are returned by including the "keys" argument
|
99
|
+
as shown below
|
100
|
+
|
101
|
+
``` ruby
|
102
|
+
require 'zipcoder'
|
103
|
+
|
104
|
+
puts "Austin, TX".city_info(keys: [:zips])
|
105
|
+
# > {:zips=>["73301", ...]}
|
106
|
+
```
|
107
|
+
|
108
|
+
### Updating Data
|
109
|
+
|
110
|
+
The library is using the free public zip code data base located [here](http://federalgovernmentzipcodes.us/).
|
111
|
+
To update the database, run the following at command line from the top directory
|
112
|
+
|
113
|
+
```
|
114
|
+
%> rake zipcoder:update # Pulls the latest CSV file from the website
|
115
|
+
%> rake zipcoder:convert # Updates the zipcoder data structures
|
116
|
+
```
|
117
|
+
|
118
|
+
## Development
|
119
|
+
|
120
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
121
|
+
|
122
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
123
|
+
|
124
|
+
## Contributing
|
125
|
+
|
126
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ericchapman/zipcoder.
|
127
|
+
|
128
|
+
|
129
|
+
## License
|
130
|
+
|
131
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
132
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require 'net/http'
|
4
|
+
require 'csv'
|
5
|
+
require 'yaml'
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
task :default => :spec
|
10
|
+
|
11
|
+
namespace :zipcoder do
|
12
|
+
|
13
|
+
desc "Pulls the latest zip code data base file"
|
14
|
+
task :update do
|
15
|
+
|
16
|
+
# Fetch the latest database file
|
17
|
+
uri = URI("http://federalgovernmentzipcodes.us/free-zipcode-database-Primary.csv")
|
18
|
+
puts "Downloading newest zip codes from '#{uri.to_s}'"
|
19
|
+
doc = Net::HTTP.get(uri)
|
20
|
+
|
21
|
+
# Write the file to the file system
|
22
|
+
filename = "lib/data/zipcode.csv"
|
23
|
+
puts "Writing to the file '#{filename}'"
|
24
|
+
File.open(filename, 'w') { |file| file.write(doc.to_s) }
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Converts the database file into the formats expected for the library"
|
28
|
+
task :convert do
|
29
|
+
|
30
|
+
# Open the file
|
31
|
+
filename = "lib/data/zipcode.csv"
|
32
|
+
puts "Opening the file '#{filename}'"
|
33
|
+
csv_text = File.read(filename)
|
34
|
+
|
35
|
+
# Import the CSV file and build the data structure
|
36
|
+
zip_lookup_data = {}
|
37
|
+
city_lookup_data = {}
|
38
|
+
csv = CSV.parse(csv_text, :headers => true)
|
39
|
+
puts "Importing data from '#{filename}'"
|
40
|
+
csv.each do |row|
|
41
|
+
zip_code = row["Zipcode"]
|
42
|
+
city = row["City"]
|
43
|
+
state = row["State"]
|
44
|
+
lat = row["Lat"].to_f
|
45
|
+
long = row["Long"].to_f
|
46
|
+
|
47
|
+
# Write the zip_lookup_data
|
48
|
+
zip_lookup_data[zip_code] = { zip: zip_code, city: city, state: state, lat: lat, long: long }
|
49
|
+
|
50
|
+
# Write the city_lookup_data
|
51
|
+
city_key = "#{city},#{state}"
|
52
|
+
city_data = city_lookup_data[city_key] || {}
|
53
|
+
|
54
|
+
zips = city_data[:zips] || []
|
55
|
+
zips << zip_code
|
56
|
+
|
57
|
+
lats = city_data[:lats] || []
|
58
|
+
lats << lat
|
59
|
+
|
60
|
+
longs = city_data[:longs] || []
|
61
|
+
longs << long
|
62
|
+
|
63
|
+
city_lookup_data[city_key] = { zips: zips, city: city, state: state, lats: lats, longs: longs }
|
64
|
+
end
|
65
|
+
|
66
|
+
# Write the data to the yaml file
|
67
|
+
zip_lookup = "lib/data/zip_lookup.yml"
|
68
|
+
puts "Writing data to '#{zip_lookup}'"
|
69
|
+
File.open(zip_lookup, 'w') {|file| file.write zip_lookup_data.to_yaml }
|
70
|
+
|
71
|
+
# Write the city lookup data
|
72
|
+
city_lookup = "lib/data/city_lookup.yml"
|
73
|
+
puts "Writing data to '#{city_lookup}'"
|
74
|
+
File.open(city_lookup, 'w') {|file| file.write city_lookup_data.to_yaml }
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zipcoder"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/circle.yml
ADDED