zipcode_locator 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +1 -0
- data/README.rdoc +76 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/zipcode_locator.rb +30 -31
- data/lib/zipcode_locator/format.rb +17 -0
- data/lib/zipcode_locator/locator.rb +21 -0
- data/lib/zipcode_locator/response.rb +37 -0
- data/lib/zipcode_locator/unit.rb +15 -12
- data/test/helper.rb +1 -1
- data/test/test_zipcode_locator.rb +1 -1
- data/zipcode_locator.gemspec +10 -4
- metadata +20 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18e1a78bbee3dc754fee59d8fb53a4b89dbb9042
|
4
|
+
data.tar.gz: 50c3a34be10c188853778359bcc5c46435d83bb2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28077b809b6b21c955fda7dfbf8d0c7dc812b3b07f1c08cf144200f85e68f2ef5f5e8a3370f371b59f1d74da601be055f1d2d8fb907c329155e199d2acaa49cc
|
7
|
+
data.tar.gz: 5b8fa97fa956a88b86776a443b9e65165b13bcf79878c598f94c8f25c2f4e4c3c3007cf53a42d4f40fa96b19cbeb365d8888217c515d6f4fa423f0e18ac0c578
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,81 @@
|
|
1
1
|
= zipcode_locator
|
2
2
|
|
3
|
-
|
3
|
+
Get Zipcodes and Location info by using ZipCode APIS.
|
4
|
+
|
5
|
+
= Usage
|
6
|
+
|
7
|
+
To use this gem, first you need to register your application at Zipcode API
|
8
|
+
visit https://www.zipcodeapi.com/Register, give your application name and email, then obtain an apikey which can be used to communicate with zipcode apis.
|
9
|
+
|
10
|
+
apikey = "xxxxxxxx"
|
11
|
+
|
12
|
+
then,
|
13
|
+
|
14
|
+
$ gem install zipcode_locator
|
15
|
+
|
16
|
+
$ @object = ZipcodeLocator.new(apikey)
|
17
|
+
|
18
|
+
= Zipcode Locator methods
|
19
|
+
|
20
|
+
zipcode_locator supports different units and reposnse formats, To get repsonse in particular format with a particulat unit, you can pass these as an optional parameter
|
21
|
+
|
22
|
+
optional parameter includes unit and format for the request.
|
23
|
+
|
24
|
+
unit can be any of these availble units "km", "mile", "degree", and "radians"
|
25
|
+
|
26
|
+
format can be 'json', 'xml', and 'csv'
|
27
|
+
|
28
|
+
$ options = { :unit => "km", :format => "json" }
|
29
|
+
|
30
|
+
== Zipcode Distance
|
31
|
+
|
32
|
+
$ zipcode1 = 10001
|
33
|
+
$ zipcode2 = 10005
|
34
|
+
|
35
|
+
$ @object.distance_between_zipcodes(zipcode1, zipcode2, options)
|
36
|
+
|
37
|
+
== Zipcodes By Radius
|
38
|
+
|
39
|
+
$ zipcode = 10001
|
40
|
+
$ radius = 5
|
41
|
+
|
42
|
+
$ @object.get_zipcodes_by_radius(zipcode, radius, options)
|
43
|
+
|
44
|
+
== Find close Zipcodes
|
45
|
+
|
46
|
+
$ zipcodes = "10001, 10005"
|
47
|
+
|
48
|
+
or
|
49
|
+
|
50
|
+
$ zipcodes = [10001, 10005]
|
51
|
+
$ distance = 5
|
52
|
+
|
53
|
+
$ @object.find_close_zipcodes(zipcodes, distance, options)
|
54
|
+
|
55
|
+
== Zipcode to Location info
|
56
|
+
|
57
|
+
$ zipcode = 10005
|
58
|
+
|
59
|
+
$ @object.zipcode_location_info(zipcode, options)
|
60
|
+
|
61
|
+
== Multi Zipcodes to Location info
|
62
|
+
|
63
|
+
$ zipcodes = "10001, 10005"
|
64
|
+
|
65
|
+
or
|
66
|
+
|
67
|
+
$ zipcodes = [10001, 10005]
|
68
|
+
|
69
|
+
$ @object.multi_zipcodes_location_info(zipcodes, options)
|
70
|
+
|
71
|
+
== Location to Zipcodes
|
72
|
+
|
73
|
+
$ city = "Brooklyn"
|
74
|
+
$ state = "NY"
|
75
|
+
|
76
|
+
$ @object.get_zipcodes_by_locations(city, state, options)
|
77
|
+
|
78
|
+
|
4
79
|
|
5
80
|
== Contributing to zipcode_locator
|
6
81
|
|
data/Rakefile
CHANGED
@@ -17,7 +17,7 @@ Jeweler::Tasks.new do |gem|
|
|
17
17
|
gem.name = "zipcode_locator"
|
18
18
|
gem.homepage = "http://github.com/anildias/zipcode_locator"
|
19
19
|
gem.license = "MIT"
|
20
|
-
gem.summary = %Q{
|
20
|
+
gem.summary = %Q{Get Zipcodes and Location info by using ZipCode APISe}
|
21
21
|
gem.description = %Q{Locate place with the help of Zip code API within a given distance by inputting zipcode}
|
22
22
|
gem.email = "anildiasdavis@gmail.com"
|
23
23
|
gem.authors = ["anildias"]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/zipcode_locator.rb
CHANGED
@@ -1,61 +1,60 @@
|
|
1
1
|
require 'zipcode_locator/unit'
|
2
|
-
|
3
|
-
require '
|
2
|
+
require 'zipcode_locator/format'
|
3
|
+
require 'zipcode_locator/response'
|
4
|
+
require 'zipcode_locator/locator'
|
4
5
|
|
5
6
|
class ZipcodeLocator
|
6
|
-
DEFAULT_UNIT = "km"
|
7
|
-
DEFAULT_FORMAT = "json"
|
8
|
-
AVAILABLE_UNITS = ["km", "mile", "radians", "degrees"]
|
9
|
-
AVAILABLE_FORMATS = ["json", "xml", "csv"]
|
10
7
|
|
11
8
|
def initialize(api_key)
|
12
9
|
@api_key = api_key
|
13
10
|
end
|
14
11
|
|
15
|
-
def distance_between_zipcodes(zip_code1, zip_code2,
|
16
|
-
|
17
|
-
unit =
|
12
|
+
def distance_between_zipcodes(zip_code1, zip_code2, options = {})
|
13
|
+
options = {} unless options.is_a? Hash
|
14
|
+
unit, format = Locator.init(options[:unit], options[:format])
|
18
15
|
url = "http://www.zipcodeapi.com/rest/#{@api_key}/distance.#{format}/#{zip_code1}/#{zip_code2}/#{unit}"
|
19
|
-
|
16
|
+
response = Locator.get_response(url, format)
|
20
17
|
end
|
21
18
|
|
22
|
-
def get_zipcodes_by_radius(zip_code, radius,
|
23
|
-
|
24
|
-
unit =
|
19
|
+
def get_zipcodes_by_radius(zip_code, radius, options = {})
|
20
|
+
options = {} unless options.is_a? Hash
|
21
|
+
unit, format = Locator.init(options[:unit], options[:format])
|
25
22
|
url = "http://www.zipcodeapi.com/rest/#{@api_key}/radius.#{format}/#{zip_code}/#{radius}/#{unit}"
|
26
|
-
|
23
|
+
response = Locator.get_response(url, format)
|
27
24
|
end
|
28
25
|
|
29
26
|
# zip_codes = array of zipcodes
|
30
|
-
def find_close_zipcodes(zip_codes, distance,
|
31
|
-
|
32
|
-
unit =
|
33
|
-
zip_codes = zip_codes.join(',')
|
27
|
+
def find_close_zipcodes(zip_codes, distance, options = {})
|
28
|
+
options = {} unless options.is_a? Hash
|
29
|
+
unit, format = Locator.init(options[:unit], options[:format])
|
30
|
+
zip_codes = [zip_codes].flatten.join(',')
|
34
31
|
url = "http://www.zipcodeapi.com/rest/#{@api_key}/match-close.#{format}/#{zip_codes}/#{distance}/#{unit}"
|
35
|
-
|
32
|
+
response = Locator.get_response(url, format)
|
36
33
|
end
|
37
34
|
|
38
|
-
def zipcode_location_info(zip_code,
|
39
|
-
|
40
|
-
unit =
|
35
|
+
def zipcode_location_info(zip_code, options = {})
|
36
|
+
options = {} unless options.is_a? Hash
|
37
|
+
unit, format = Locator.init(options[:unit], options[:format])
|
41
38
|
url = "http://www.zipcodeapi.com/rest/#{@api_key}/info.#{format}/#{zip_code}/#{unit}"
|
42
|
-
|
39
|
+
response = Locator.get_response(url, format)
|
43
40
|
end
|
44
41
|
|
45
42
|
# zip_codes = array of zipcodes
|
46
|
-
def multi_zipcodes_location_info(zip_codes,
|
47
|
-
|
48
|
-
unit =
|
49
|
-
zip_codes = zip_codes.join(',')
|
43
|
+
def multi_zipcodes_location_info(zip_codes, options = {})
|
44
|
+
options = {} unless options.is_a? Hash
|
45
|
+
unit, format = Locator.init(options[:unit], options[:format])
|
46
|
+
zip_codes = [zip_codes].flatten.join(',')
|
50
47
|
url = "http://www.zipcodeapi.com/rest/#{@api_key}/multi-info.#{format}/#{zip_codes}/#{unit}"
|
51
|
-
|
48
|
+
response = Locator.get_response(url, format)
|
52
49
|
end
|
53
50
|
|
54
|
-
def get_zipcodes_by_locations(city, state)
|
51
|
+
def get_zipcodes_by_locations(city, state, options = {})
|
52
|
+
options = {} unless options.is_a? Hash
|
55
53
|
city = city.delete(' ')
|
56
54
|
state = state.delete(' ')
|
57
|
-
|
58
|
-
|
55
|
+
format = Format.set(options[:format])
|
56
|
+
url = "http://www.zipcodeapi.com/rest/#{@api_key}/city-zips.#{format}/#{city}/#{state}"
|
57
|
+
response = Locator.get_response(url, format)
|
59
58
|
end
|
60
59
|
|
61
60
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Format
|
2
|
+
DEFAULT_FORMAT = "json"
|
3
|
+
AVAILABLE_FORMATS = ["json", "xml", "csv"]
|
4
|
+
|
5
|
+
def self.default_format
|
6
|
+
DEFAULT_FORMAT
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.available_formats
|
10
|
+
AVAILABLE_FORMATS
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.set(format)
|
14
|
+
AVAILABLE_FORMATS.include?(format) ? format : DEFAULT_FORMAT
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'zipcode_locator/unit'
|
2
|
+
require 'zipcode_locator/format'
|
3
|
+
require 'zipcode_locator/response'
|
4
|
+
|
5
|
+
module Locator
|
6
|
+
|
7
|
+
def self.init(unit, format)
|
8
|
+
[Unit.set(unit), Format.set(format)]
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.get_response(url, format)
|
12
|
+
case format
|
13
|
+
when "json"
|
14
|
+
Response.json(url)
|
15
|
+
when "xml"
|
16
|
+
Response.xml(url)
|
17
|
+
when "csv"
|
18
|
+
Response.csv(url)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
module Response
|
5
|
+
|
6
|
+
ERROR_JSON_RESPONSE = { status: 400, message: "error" }
|
7
|
+
ERROR_XML_RESPONSE = "<?xml version=\"1.0\"?>\n<response>\n <status>400</status>\n<message>error</message>\n</response>\n"
|
8
|
+
ERROR_CSV_RESPONSE = "status,message\n400,error\n"
|
9
|
+
|
10
|
+
def self.json(url)
|
11
|
+
zipcode_response = HTTParty.get(url)
|
12
|
+
if zipcode_response.code == 200
|
13
|
+
JSON.parse(zipcode_response.body)
|
14
|
+
else
|
15
|
+
ERROR_JSON_RESPONSE
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.xml(url)
|
20
|
+
zipcode_response = HTTParty.get(url)
|
21
|
+
if zipcode_response.code == 200
|
22
|
+
Nokogiri::XML(zipcode_response.body).to_xml
|
23
|
+
else
|
24
|
+
ERROR_XML_RESPONSE
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.csv(url)
|
29
|
+
zipcode_response = HTTParty.get(url)
|
30
|
+
if zipcode_response.code == 200
|
31
|
+
zipcode_response.body
|
32
|
+
else
|
33
|
+
ERROR_CSV_RESPONSE
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
data/lib/zipcode_locator/unit.rb
CHANGED
@@ -1,14 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
module Unit
|
2
|
+
DEFAULT_UNIT = "km"
|
3
|
+
AVAILABLE_UNITS = ["km", "mile", "radians", "degrees"]
|
4
|
+
|
5
|
+
def self.default_unit
|
6
|
+
DEFAULT_UNIT
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.available_units
|
10
|
+
AVAILABLE_UNITS
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.set(unit)
|
14
|
+
AVAILABLE_UNITS.include?(unit) ? unit : DEFAULT_UNIT
|
13
15
|
end
|
16
|
+
end
|
14
17
|
|
data/test/helper.rb
CHANGED
@@ -46,7 +46,7 @@ class TestZipcodeLocator < Minitest::Test
|
|
46
46
|
def test_multi_zipcodes_location_info()
|
47
47
|
test_zipcodes = "10001,10005"
|
48
48
|
VCR.use_cassette("multi_zipcodes_location_info", :record => :new_episodes) do
|
49
|
-
res = @@loc.multi_zipcodes_location_info([10001, 10005]
|
49
|
+
res = @@loc.multi_zipcodes_location_info([10001, 10005])
|
50
50
|
response = Net::HTTP.get_response(URI("http://www.zipcodeapi.com/rest/3YJBA4WF9vpMJWjnh0otcznhkPzlsuRwtB74F4KOz9Bj6VGnV4Q8EepY3Zkwtc2X/multi-info.json/#{test_zipcodes}/km"))
|
51
51
|
assert_equal res, JSON.parse(response.body)
|
52
52
|
end
|
data/zipcode_locator.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: zipcode_locator 0.1.
|
5
|
+
# stub: zipcode_locator 0.1.2 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "zipcode_locator"
|
9
|
-
s.version = "0.1.
|
9
|
+
s.version = "0.1.2"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["anildias"]
|
14
|
-
s.date = "2016-03-
|
14
|
+
s.date = "2016-03-14"
|
15
15
|
s.description = "Locate place with the help of Zip code API within a given distance by inputting zipcode"
|
16
16
|
s.email = "anildiasdavis@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -27,6 +27,9 @@ Gem::Specification.new do |s|
|
|
27
27
|
"Rakefile",
|
28
28
|
"VERSION",
|
29
29
|
"lib/zipcode_locator.rb",
|
30
|
+
"lib/zipcode_locator/format.rb",
|
31
|
+
"lib/zipcode_locator/locator.rb",
|
32
|
+
"lib/zipcode_locator/response.rb",
|
30
33
|
"lib/zipcode_locator/unit.rb",
|
31
34
|
"test/helper.rb",
|
32
35
|
"test/test_zipcode_locator.rb",
|
@@ -41,7 +44,7 @@ Gem::Specification.new do |s|
|
|
41
44
|
s.homepage = "http://github.com/anildias/zipcode_locator"
|
42
45
|
s.licenses = ["MIT"]
|
43
46
|
s.rubygems_version = "2.4.3"
|
44
|
-
s.summary = "
|
47
|
+
s.summary = "Get Zipcodes and Location info by using ZipCode APISe"
|
45
48
|
|
46
49
|
if s.respond_to? :specification_version then
|
47
50
|
s.specification_version = 4
|
@@ -55,6 +58,7 @@ Gem::Specification.new do |s|
|
|
55
58
|
s.add_development_dependency(%q<vcr>, [">= 0"])
|
56
59
|
s.add_development_dependency(%q<webmock>, [">= 0"])
|
57
60
|
s.add_development_dependency(%q<httparty>, [">= 0"])
|
61
|
+
s.add_development_dependency(%q<nokogiri>, [">= 0"])
|
58
62
|
else
|
59
63
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
60
64
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
@@ -64,6 +68,7 @@ Gem::Specification.new do |s|
|
|
64
68
|
s.add_dependency(%q<vcr>, [">= 0"])
|
65
69
|
s.add_dependency(%q<webmock>, [">= 0"])
|
66
70
|
s.add_dependency(%q<httparty>, [">= 0"])
|
71
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
67
72
|
end
|
68
73
|
else
|
69
74
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
@@ -74,6 +79,7 @@ Gem::Specification.new do |s|
|
|
74
79
|
s.add_dependency(%q<vcr>, [">= 0"])
|
75
80
|
s.add_dependency(%q<webmock>, [">= 0"])
|
76
81
|
s.add_dependency(%q<httparty>, [">= 0"])
|
82
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
77
83
|
end
|
78
84
|
end
|
79
85
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zipcode_locator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- anildias
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: shoulda
|
@@ -122,6 +122,20 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: nokogiri
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
125
139
|
description: Locate place with the help of Zip code API within a given distance by
|
126
140
|
inputting zipcode
|
127
141
|
email: anildiasdavis@gmail.com
|
@@ -139,6 +153,9 @@ files:
|
|
139
153
|
- Rakefile
|
140
154
|
- VERSION
|
141
155
|
- lib/zipcode_locator.rb
|
156
|
+
- lib/zipcode_locator/format.rb
|
157
|
+
- lib/zipcode_locator/locator.rb
|
158
|
+
- lib/zipcode_locator/response.rb
|
142
159
|
- lib/zipcode_locator/unit.rb
|
143
160
|
- test/helper.rb
|
144
161
|
- test/test_zipcode_locator.rb
|
@@ -172,6 +189,5 @@ rubyforge_project:
|
|
172
189
|
rubygems_version: 2.4.3
|
173
190
|
signing_key:
|
174
191
|
specification_version: 4
|
175
|
-
summary:
|
176
|
-
zipcode
|
192
|
+
summary: Get Zipcodes and Location info by using ZipCode APISe
|
177
193
|
test_files: []
|