ziptastic 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rvmrc +34 -0
- data/.travis.yml +5 -0
- data/Gemfile +7 -0
- data/LICENSE +22 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/ziptastic +7 -0
- data/lib/ziptastic.rb +32 -0
- data/lib/ziptastic/app.rb +18 -0
- data/lib/ziptastic/data/zipcodes.db +0 -0
- data/lib/ziptastic/version.rb +3 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/ziptastic_spec.rb +33 -0
- data/ziptastic.gemspec +24 -0
- metadata +166 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p125@ziptastic"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.12.1 (master)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
else
|
29
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
30
|
+
rvm --create "$environment_id" || {
|
31
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
32
|
+
return 1
|
33
|
+
}
|
34
|
+
fi
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ezekiel Templin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Ziptastic Ruby
|
2
|
+
A simple Ruby port of Thomas Schultz's [Ziptastic](https://github.com/daspecster/ziptastic) zip code API.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
gem 'ziptastic'
|
9
|
+
|
10
|
+
And then execute:
|
11
|
+
|
12
|
+
$ bundle
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
$ gem install ziptastic
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
To search against the database locally:
|
20
|
+
|
21
|
+
require 'ziptastic'
|
22
|
+
results = Ziptastic.search("16335")
|
23
|
+
# => [ {:city=>"MEADVILLE", :state=>"PA", :country=>"US"},
|
24
|
+
# {:city=>"BLOOMING VALLEY", :state=>"PA", :country=>"US"},
|
25
|
+
# {:city=>"KERRTOWN", :state=>"PA", :country=>"US"} ]
|
26
|
+
|
27
|
+
Local database searches often return multiple results for a single zip code.
|
28
|
+
|
29
|
+
Alternatively, an executable is also included that provides a search API via Sinatra:
|
30
|
+
|
31
|
+
$ ziptastic
|
32
|
+
$ curl http://localhost:5678?zip=16335
|
33
|
+
# => {"city":"MEADVILLE","state":"PA","country":"US"}
|
34
|
+
|
35
|
+
The web API is compatible with the [example JS script](http://daspecster.github.com/ziptastic/demo.html) provided by Thomas Schultz. You'll just need to update the client URL.
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/ziptastic
ADDED
data/lib/ziptastic.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "sqlite3"
|
2
|
+
|
3
|
+
module Ziptastic
|
4
|
+
class NotZipCode < StandardError; end
|
5
|
+
|
6
|
+
DATABASE_PATH = File.expand_path(File.dirname(__FILE__) + "/ziptastic/data/zipcodes.db")
|
7
|
+
ZIP_DATABASE = SQLite3::Database.open(DATABASE_PATH, :readonly => true, :results_as_hash => true)
|
8
|
+
|
9
|
+
def self.search(zip_code)
|
10
|
+
search_results = []
|
11
|
+
|
12
|
+
search_results = search_for_zip_code(zip_code).collect do |row|
|
13
|
+
{:city => row['city'], :state => row['state'], :country => row['country']}
|
14
|
+
end
|
15
|
+
|
16
|
+
search_results
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def self.search_for_zip_code(zip_code)
|
21
|
+
validate_zip_code(zip_code)
|
22
|
+
ZIP_DATABASE.execute('select City, State, Country from zipcodes where ZipCode=?', zip_code)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.validate_zip_code(zip_code)
|
26
|
+
raise NotZipCode, "#{zip_code} is not a zip code!" if zip_code =~ /\D/
|
27
|
+
end
|
28
|
+
end
|
29
|
+
require "ziptastic/app"
|
30
|
+
require "ziptastic/version"
|
31
|
+
|
32
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "sinatra/base"
|
2
|
+
require "sinatra/json"
|
3
|
+
require "multi_json"
|
4
|
+
|
5
|
+
module Ziptastic
|
6
|
+
class App < Sinatra::Base
|
7
|
+
helpers Sinatra::JSON
|
8
|
+
|
9
|
+
get '/' do
|
10
|
+
results = Ziptastic.search(params[:zip])
|
11
|
+
if results.any?
|
12
|
+
json results.first
|
13
|
+
else
|
14
|
+
status 404
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
Binary file
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Ziptastic do
|
4
|
+
describe "#query" do
|
5
|
+
context "valid zipcode" do
|
6
|
+
context "w/ results" do
|
7
|
+
it "should return an array of results" do
|
8
|
+
Ziptastic.search("16335").count.should eq 3
|
9
|
+
end
|
10
|
+
|
11
|
+
it "each result should have city, state, and country" do
|
12
|
+
meadville = Ziptastic.search("16335")[0]
|
13
|
+
|
14
|
+
meadville[:city].should eq "MEADVILLE"
|
15
|
+
meadville[:state].should eq "PA"
|
16
|
+
meadville[:country].should eq "US"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "no result" do
|
21
|
+
it "should return false" do
|
22
|
+
Ziptastic.search("8675309").should eq []
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "invalid zipcode" do
|
28
|
+
it "should return an exception" do
|
29
|
+
expect { Ziptastic.search("16335a") }.to raise_error(Ziptastic::NotZipCode, "16335a is not a zip code!")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/ziptastic.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/ziptastic/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Ezekiel Templin"]
|
6
|
+
gem.email = ["zeke@templ.in"]
|
7
|
+
gem.description = %q{Ruby port of Thomas Schultz's Ziptastic API}
|
8
|
+
gem.summary = %q{Ruby port of Thomas Schultz's Ziptastic API}
|
9
|
+
gem.homepage = "https://github.com/ezkl/ziptastic"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "ziptastic"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Ziptastic::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency("sqlite3", "~> 1.3.5")
|
19
|
+
gem.add_dependency("sinatra")
|
20
|
+
gem.add_dependency("sinatra-contrib")
|
21
|
+
gem.add_dependency("multi_json")
|
22
|
+
gem.add_dependency("vegas")
|
23
|
+
gem.add_development_dependency("rspec", "~> 2.9.0")
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ziptastic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ezekiel Templin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sqlite3
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.3.5
|
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: 1.3.5
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sinatra
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sinatra-contrib
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: multi_json
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: vegas
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rspec
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 2.9.0
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 2.9.0
|
110
|
+
description: Ruby port of Thomas Schultz's Ziptastic API
|
111
|
+
email:
|
112
|
+
- zeke@templ.in
|
113
|
+
executables:
|
114
|
+
- ziptastic
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rspec
|
120
|
+
- .rvmrc
|
121
|
+
- .travis.yml
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- bin/ziptastic
|
127
|
+
- lib/ziptastic.rb
|
128
|
+
- lib/ziptastic/app.rb
|
129
|
+
- lib/ziptastic/data/zipcodes.db
|
130
|
+
- lib/ziptastic/version.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/ziptastic_spec.rb
|
133
|
+
- ziptastic.gemspec
|
134
|
+
homepage: https://github.com/ezkl/ziptastic
|
135
|
+
licenses: []
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
none: false
|
142
|
+
requirements:
|
143
|
+
- - ! '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
segments:
|
147
|
+
- 0
|
148
|
+
hash: -3288914156171438784
|
149
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
|
+
none: false
|
151
|
+
requirements:
|
152
|
+
- - ! '>='
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
segments:
|
156
|
+
- 0
|
157
|
+
hash: -3288914156171438784
|
158
|
+
requirements: []
|
159
|
+
rubyforge_project:
|
160
|
+
rubygems_version: 1.8.21
|
161
|
+
signing_key:
|
162
|
+
specification_version: 3
|
163
|
+
summary: Ruby port of Thomas Schultz's Ziptastic API
|
164
|
+
test_files:
|
165
|
+
- spec/spec_helper.rb
|
166
|
+
- spec/ziptastic_spec.rb
|