zip-code-info 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.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +30 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +47 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/data/data.yml +2794 -0
- data/dev/generate_data.rb +38 -0
- data/lib/zip-code-info.rb +38 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/zip-code-info_spec.rb +60 -0
- data/zip-code-info.gemspec +69 -0
- metadata +128 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
# Script to regenerate data/data.yml. This is only used in the gem's development.
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'open-uri'
|
6
|
+
require 'yaml'
|
7
|
+
|
8
|
+
zip_to_state = {}
|
9
|
+
|
10
|
+
doc = Nokogiri::HTML(open("http://en.wikipedia.org/wiki/ZIP_code_prefixes"))
|
11
|
+
|
12
|
+
# puts doc
|
13
|
+
|
14
|
+
doc.css('#bodyContent table td b').each do |code|
|
15
|
+
puts "zip prefix: #{code.content}\n"
|
16
|
+
if code.content.match /^[0-9]{3}/
|
17
|
+
# first three digits of the zip and the state. ex. 384 NJ
|
18
|
+
a = code.content.split
|
19
|
+
if a.length == 2
|
20
|
+
@current_zip = a[0].to_s
|
21
|
+
zip_to_state[@current_zip] = {:state => a[1][0..1]} unless a[1].length < 2 or @current_zip == "001"
|
22
|
+
end
|
23
|
+
else
|
24
|
+
# the city name ex. New York
|
25
|
+
# Make sure there are no line breaks in the name (one city has a line break)
|
26
|
+
zip_to_state[@current_zip][:city] = code.content.delete("\n")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# puts zip_to_state.to_yaml
|
31
|
+
|
32
|
+
File.open(File.join(File.dirname(__FILE__), '..', 'data', 'data.yml'), "w") do |f|
|
33
|
+
f.write zip_to_state.to_yaml
|
34
|
+
end
|
35
|
+
|
36
|
+
# Double check that the data can be loaded properly.
|
37
|
+
data = YAML.load(File.open(File.join(File.dirname(__FILE__), '..', 'data', 'data.yml')))
|
38
|
+
puts data
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
class ZipCodeInfo
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
attr_reader :code
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@data = YAML.load(File.open(File.join(File.dirname(__FILE__), '..', 'data', 'data.yml')))
|
11
|
+
end
|
12
|
+
|
13
|
+
def scf_city_for(code = "")
|
14
|
+
return false unless assign_and_validate_code(code)
|
15
|
+
@data[code_to_key][:city]
|
16
|
+
end
|
17
|
+
|
18
|
+
def state_for(code = "")
|
19
|
+
return false unless assign_and_validate_code(code)
|
20
|
+
@data[code_to_key][:state]
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def assign_and_validate_code(code)
|
27
|
+
@code = code.to_s
|
28
|
+
return @code.match /^[0-9]{5}$/i
|
29
|
+
end
|
30
|
+
|
31
|
+
# To look up information we only need the first three digits of the zip code
|
32
|
+
def code_to_key
|
33
|
+
@code[0..2]
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'zip-code-info'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "ZipCodeInfo" do
|
4
|
+
|
5
|
+
describe "Positive Test Cases: " do
|
6
|
+
|
7
|
+
it "should find the right city" do
|
8
|
+
zip = ZipCodeInfo.instance.scf_city_for "99100"
|
9
|
+
zip.should eql "Spokane"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should find the right state" do
|
13
|
+
zip = ZipCodeInfo.instance.state_for "99100"
|
14
|
+
zip.should eql "WA"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should find the right city when the zip is an integer" do
|
18
|
+
zip = ZipCodeInfo.instance.scf_city_for 60699
|
19
|
+
ZipCodeInfo.instance.code.should eql "60699"
|
20
|
+
zip.should eql "Chicago"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should find the right state when the zip is an integer" do
|
24
|
+
zip = ZipCodeInfo.instance.state_for 60699
|
25
|
+
ZipCodeInfo.instance.code.should eql "60699"
|
26
|
+
zip.should eql "IL"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should find the right city name when the city name contains an &" do
|
30
|
+
zip = ZipCodeInfo.instance.scf_city_for "11099"
|
31
|
+
zip.should eql "Queens &West Nassau"
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "Negative Test Cases: " do
|
37
|
+
|
38
|
+
it "should handle no zip code" do
|
39
|
+
zip = ZipCodeInfo.instance.scf_city_for
|
40
|
+
zip.should be_false
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should handle an invalid zip code - too long" do
|
44
|
+
zip = ZipCodeInfo.instance.scf_city_for("123456")
|
45
|
+
zip.should be_false
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should handle an invalid zip code - too short" do
|
49
|
+
zip = ZipCodeInfo.instance.scf_city_for "1234"
|
50
|
+
zip.should be_false
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should handle an invalid zip code - alphanum" do
|
54
|
+
zip = ZipCodeInfo.instance.scf_city_for "1a23g"
|
55
|
+
zip.should be_false
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{zip-code-info}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Monica Olinescu"]
|
12
|
+
s.date = %q{2011-03-09}
|
13
|
+
s.description = %q{The data has been collected from wikipedia and stored in a yml file. This gem does not use an external service.}
|
14
|
+
s.email = %q{monica.olinescu@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"data/data.yml",
|
29
|
+
"dev/generate_data.rb",
|
30
|
+
"lib/zip-code-info.rb",
|
31
|
+
"spec/spec_helper.rb",
|
32
|
+
"spec/zip-code-info_spec.rb",
|
33
|
+
"zip-code-info.gemspec"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/monicao/zip-code-info}
|
36
|
+
s.licenses = ["MIT"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.5.0}
|
39
|
+
s.summary = %q{Looks up the state and city of the Sectional Center Facility for a given zip code.}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/spec_helper.rb",
|
42
|
+
"spec/zip-code-info_spec.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
50
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
51
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
52
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<nokogiri>, [">= 0"])
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
56
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
57
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
58
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
59
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
60
|
+
end
|
61
|
+
else
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
64
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
65
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
66
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zip-code-info
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Monica Olinescu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-09 00:00:00 -05:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: bundler
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.0
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jeweler
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.5.2
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.3.0
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rcov
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: nokogiri
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
description: The data has been collected from wikipedia and stored in a yml file. This gem does not use an external service.
|
72
|
+
email: monica.olinescu@gmail.com
|
73
|
+
executables: []
|
74
|
+
|
75
|
+
extensions: []
|
76
|
+
|
77
|
+
extra_rdoc_files:
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.rdoc
|
80
|
+
files:
|
81
|
+
- .document
|
82
|
+
- .rspec
|
83
|
+
- Gemfile
|
84
|
+
- Gemfile.lock
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.rdoc
|
87
|
+
- Rakefile
|
88
|
+
- VERSION
|
89
|
+
- data/data.yml
|
90
|
+
- dev/generate_data.rb
|
91
|
+
- lib/zip-code-info.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/zip-code-info_spec.rb
|
94
|
+
- zip-code-info.gemspec
|
95
|
+
has_rdoc: true
|
96
|
+
homepage: http://github.com/monicao/zip-code-info
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: -1445853004520550373
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: "0"
|
119
|
+
requirements: []
|
120
|
+
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.5.0
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Looks up the state and city of the Sectional Center Facility for a given zip code.
|
126
|
+
test_files:
|
127
|
+
- spec/spec_helper.rb
|
128
|
+
- spec/zip-code-info_spec.rb
|