state-of-zip 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,24 @@
1
+ = StateOfZip
2
+
3
+ StateOfZip will return a state for a given zip code. It uses Sectional Center Facility locations to determine this. And then scrapes the data from http://en.wikipedia.org/wiki/ZIP_code_prefixes
4
+
5
+ Note some zip codes serve multiple states. This list may not be entirely accurate.
6
+
7
+ == Compatibility
8
+ This is a Rails Engine and is only tested against Rails 3.2 and Ruby 1.9.3
9
+
10
+ == Install
11
+
12
+ Add the following to your Gemfile
13
+ gem 'state-of-zip'
14
+
15
+ Then run
16
+ rake state_of_zip:install:migrations
17
+
18
+ Then run
19
+ rake db:migrate
20
+
21
+
22
+ == Credit
23
+
24
+ The scraping script was taken from https://github.com/monicao/zip-code-info
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ require 'rspec/core/rake_task'
8
+ begin
9
+ require 'rdoc/task'
10
+ rescue LoadError
11
+ require 'rdoc/rdoc'
12
+ require 'rake/rdoctask'
13
+ RDoc::Task = Rake::RDocTask
14
+ end
15
+
16
+ RDoc::Task.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'StateOfZip'
19
+ rdoc.options << '--line-numbers'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
25
+ load 'rails/tasks/engine.rake'
26
+
27
+ Bundler::GemHelper.install_tasks
28
+
29
+ require 'rake/testtask'
30
+
31
+ Rake::TestTask.new(:test) do |t|
32
+ t.libs << 'lib'
33
+ t.libs << 'test'
34
+ t.pattern = 'test/**/*_test.rb'
35
+ t.verbose = false
36
+ end
37
+
38
+
39
+ task :default => :test
40
+
41
+ desc "Update data.yml file with the zip code state mapping. (This should only be ran in development)"
42
+ task :data_scrape do
43
+ load 'lib/state-of-zip/data.rb'
44
+ StateOfZip::DataScrape.scrape
45
+ end
@@ -0,0 +1,11 @@
1
+ module StateOfZip
2
+ class ZipCode < ActiveRecord::Base
3
+ def self.lookup(zip_code)
4
+ if zip_code = where(zip_code_prefix: zip_code[0..2]).first
5
+ zip_code.state_code
6
+ else
7
+ false
8
+ end
9
+ end
10
+ end
11
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ StateOfZip::Engine.routes.draw do
2
+ resources :zip_codes
3
+
4
+ end
@@ -0,0 +1,12 @@
1
+ class CreateStateOfZipZipCodes < ActiveRecord::Migration
2
+ def change
3
+ create_table :state_of_zip_zip_codes do |t|
4
+ t.string :zip_code_prefix, :limit => 3
5
+ t.string :state_code, :limit => 2
6
+ end
7
+
8
+ add_index :state_of_zip_zip_codes, :zip_code_prefix
9
+
10
+ StateOfZip::DataLoad.load
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ require "state-of-zip/engine"
2
+ require "state-of-zip/data_load"
3
+
4
+ module StateOfZip
5
+ end
@@ -0,0 +1,35 @@
1
+ module StateOfZip
2
+ class DataScrape
3
+ def self.scrape
4
+ require "nokogiri"
5
+ require "open-uri"
6
+ zip_to_state = {}
7
+
8
+ doc = Nokogiri::HTML(open("http://en.wikipedia.org/wiki/ZIP_code_prefixes"))
9
+
10
+ puts doc
11
+
12
+ doc.css('#bodyContent table td b').each do |code|
13
+ puts "zip prefix: #{code.content}\n"
14
+ if code.content.match /^[0-9]{3}/
15
+ # first three digits of the zip and the state. ex. 384 NJ
16
+ a = code.content.split
17
+ if a.length == 2
18
+ @current_zip = a[0].to_s
19
+ zip_to_state[@current_zip] = a[1][0..1] unless a[1].length < 2 or @current_zip == "001"
20
+ end
21
+ else
22
+ end
23
+ end
24
+
25
+
26
+ File.open(File.join(File.dirname(__FILE__), '../..', 'data', 'data.yml'), "w") do |f|
27
+ f.write zip_to_state.to_yaml
28
+ end
29
+
30
+ # Double check that the data can be loaded properly.
31
+ data = YAML.load(File.open(File.join(File.dirname(__FILE__), '../..', 'data', 'data.yml')))
32
+ puts data
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ module StateOfZip
2
+ class DataLoad
3
+ def self.load
4
+ data = YAML.load(File.open(File.join(File.dirname(__FILE__), '../..', 'data', 'data.yml')))
5
+ data.each do |key, value|
6
+ #puts "Updating prefix #{key} with State #{value}"
7
+ zip_code = StateOfZip::ZipCode.where(zip_code_prefix: key).first || StateOfZip::ZipCode.new
8
+ zip_code.zip_code_prefix = key
9
+ zip_code.state_code = value
10
+ zip_code.save!
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,39 @@
1
+ module StateOfZip
2
+ class DataScrape
3
+ def self.scrape
4
+ require "nokogiri"
5
+ require "open-uri"
6
+ zip_to_state = {}
7
+
8
+ doc = Nokogiri::HTML(open("http://en.wikipedia.org/wiki/ZIP_code_prefixes"))
9
+
10
+ puts doc
11
+
12
+ doc.css('#bodyContent table td b').each do |code|
13
+ puts "zip prefix: #{code.content}\n"
14
+ if code.content.match /^[0-9]{3}/
15
+ # first three digits of the zip and the state. ex. 384 NJ
16
+ a = code.content.split
17
+ if a.length == 2
18
+ @current_zip = a[0].to_s
19
+ zip_to_state[@current_zip] = a[1][0..1] unless a[1].length < 2 or @current_zip == "001"
20
+ end
21
+ else
22
+ end
23
+ end
24
+
25
+
26
+ File.open(File.join(File.dirname(__FILE__), '../..', 'data', 'data.yml'), "w") do |f|
27
+ f.write zip_to_state.to_yaml
28
+ end
29
+
30
+ # Double check that the data can be loaded properly.
31
+ data = YAML.load(File.open(File.join(File.dirname(__FILE__), '../..', 'data', 'data.yml')))
32
+ puts data
33
+ end
34
+
35
+ def self.load
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ module StateOfZip
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace StateOfZip
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module StateOfZip
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ namespace :state_of_zip do
2
+ namespace :data do
3
+ desc "Load the data.yml into StateOfZip::ZipCode"
4
+ task :load => :environment do
5
+ StateOfZip::DataLoad.load
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: state-of-zip
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Cranston
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.11'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.11'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.6
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.6
62
+ - !ruby/object:Gem::Dependency
63
+ name: nokogiri
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.5.5
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.5.5
78
+ description: Rails Engine that returns a US State code that a given zip code's sectional
79
+ center facility resides in. It uses a database table to look up the record.
80
+ email:
81
+ - eric@foodydirect.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - app/models/state_of_zip/zip_code.rb
87
+ - config/routes.rb
88
+ - db/migrate/20121025173341_create_state_of_zip_zip_codes.rb
89
+ - lib/state-of-zip/data.rb
90
+ - lib/state-of-zip/data_load.rb
91
+ - lib/state-of-zip/data_scrape.rb
92
+ - lib/state-of-zip/engine.rb
93
+ - lib/state-of-zip/version.rb
94
+ - lib/state-of-zip.rb
95
+ - lib/tasks/state-of-zip_tasks.rake
96
+ - MIT-LICENSE
97
+ - Rakefile
98
+ - README.rdoc
99
+ homepage: http://github.com/foodydirect/state-of-zip
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ segments:
112
+ - 0
113
+ hash: -4358455165322927163
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ segments:
121
+ - 0
122
+ hash: -4358455165322927163
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.24
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Rails Engine that returns a US State code that a given zip code's sectional
129
+ center facility resides in.
130
+ test_files: []