zippopotamus-client 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +88 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +38 -0
- data/LICENSE.txt +22 -0
- data/README.md +81 -0
- data/Rakefile +7 -0
- data/lib/zippopotamus/client.rb +81 -0
- data/lib/zippopotamus/configuration.rb +15 -0
- data/lib/zippopotamus/version.rb +3 -0
- data/lib/zippopotamus.rb +57 -0
- data/spec/place_spec.rb +48 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/vcr_cassettes/fr_01000.yml +43 -0
- data/spec/vcr_cassettes/us_90210.yml +41 -0
- data/spec/vcr_cassettes/xx_1234.yml +36 -0
- data/spec/zippopotamus_spec.rb +85 -0
- data/zippopotamus.gemspec +27 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a5f89966abe53d876bcdfbcbd687ce0b906e16a0
|
4
|
+
data.tar.gz: f12e8db0fc202bca323b0ab5a77b680b2de63820
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 73258d385e23038417f55de799cb898df42853e3e95b8551d113f16108b13a76f93fc302f28512dc63f4072f89631f9e13fd367b9b235de38d979acd19375a05
|
7
|
+
data.tar.gz: 6679c078a4eac07a8909d4f3a7cdbec505b3a2035113053514324273627b302c4135b2ebaf23437d34d591515222d88dfcde48a7425d3c1f50170207111bbffd
|
data/.gitignore
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# RVM, Zeus, Rails related
|
2
|
+
.ruby-version
|
3
|
+
.ruby-gemset
|
4
|
+
.rvmrc
|
5
|
+
.zeus.sock
|
6
|
+
zeus.json
|
7
|
+
custom_plan.rb
|
8
|
+
database.yml
|
9
|
+
|
10
|
+
# Ignore all logfiles and tempfiles.
|
11
|
+
/log/*.log
|
12
|
+
/tmp
|
13
|
+
# Rails
|
14
|
+
.bundle
|
15
|
+
db/*.sqlite3
|
16
|
+
db/*.sqlite3-journal
|
17
|
+
*.log
|
18
|
+
tmp
|
19
|
+
tmp/**/*
|
20
|
+
|
21
|
+
# Documentation
|
22
|
+
doc/api
|
23
|
+
doc/app
|
24
|
+
.yardoc
|
25
|
+
.yardopts
|
26
|
+
coverage
|
27
|
+
|
28
|
+
# Public Uploads
|
29
|
+
public/system/*
|
30
|
+
public/themes/*
|
31
|
+
|
32
|
+
# Public Cache
|
33
|
+
public/javascripts/cache
|
34
|
+
public/stylesheets/cache
|
35
|
+
|
36
|
+
# Vendor Cache
|
37
|
+
vendor/cache
|
38
|
+
|
39
|
+
# Acts as Indexed
|
40
|
+
index/**/*
|
41
|
+
|
42
|
+
# Refinery Specific
|
43
|
+
*.tmproj
|
44
|
+
*.autobackupbyrefinery.*
|
45
|
+
refinerycms-*.gem
|
46
|
+
|
47
|
+
# Mac
|
48
|
+
.DS_Store
|
49
|
+
|
50
|
+
# Windows
|
51
|
+
Thumbs.db
|
52
|
+
|
53
|
+
# NetBeans
|
54
|
+
nbproject
|
55
|
+
|
56
|
+
# Eclipse
|
57
|
+
.project
|
58
|
+
|
59
|
+
# Redcar
|
60
|
+
.redcar
|
61
|
+
|
62
|
+
# Rubinius
|
63
|
+
*.rbc
|
64
|
+
|
65
|
+
# Vim
|
66
|
+
*.swp
|
67
|
+
*.swo
|
68
|
+
|
69
|
+
# RubyMine
|
70
|
+
.idea
|
71
|
+
|
72
|
+
# E-texteditor
|
73
|
+
.eprj
|
74
|
+
|
75
|
+
# Backup
|
76
|
+
*~
|
77
|
+
|
78
|
+
# Capybara Bug
|
79
|
+
capybara-*html
|
80
|
+
|
81
|
+
# sass
|
82
|
+
.sass-cache
|
83
|
+
.sass-cache/*
|
84
|
+
|
85
|
+
|
86
|
+
# vendor/extensions dummy applications.
|
87
|
+
vendor/extensions/**/spec/dummy
|
88
|
+
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
zippopotamus-client (0.1.1)
|
5
|
+
excon (>= 0, < 1.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.3.5)
|
11
|
+
crack (0.4.1)
|
12
|
+
safe_yaml (~> 0.9.0)
|
13
|
+
diff-lcs (1.2.5)
|
14
|
+
excon (0.31.0)
|
15
|
+
rake (10.1.1)
|
16
|
+
rspec (2.14.1)
|
17
|
+
rspec-core (~> 2.14.0)
|
18
|
+
rspec-expectations (~> 2.14.0)
|
19
|
+
rspec-mocks (~> 2.14.0)
|
20
|
+
rspec-core (2.14.7)
|
21
|
+
rspec-expectations (2.14.4)
|
22
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
23
|
+
rspec-mocks (2.14.4)
|
24
|
+
safe_yaml (0.9.7)
|
25
|
+
vcr (2.8.0)
|
26
|
+
webmock (1.15.2)
|
27
|
+
addressable (>= 2.2.7)
|
28
|
+
crack (>= 0.3.2)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
rake
|
35
|
+
rspec
|
36
|
+
vcr (~> 2.8.0)
|
37
|
+
webmock (~> 1.15, < 1.16)
|
38
|
+
zippopotamus-client!
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Radu Cugut
|
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,81 @@
|
|
1
|
+
# Zippopotamus Client (for Ruby)
|
2
|
+
|
3
|
+
This is a simple ruby client for the [zippopotam.us](http://zippopotam.us) API.
|
4
|
+
It uses [excon](https://github.com/geemus/excon) http client for it's speed and persistent connections support.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile (only available through github for the moment):
|
9
|
+
|
10
|
+
gem 'zippopotamus-client', github: 'rcugut/zippopotamus-ruby-client'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
#### Configure
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
|
24
|
+
require 'zippopotamus'
|
25
|
+
|
26
|
+
Zippopotamus.configure do |c|
|
27
|
+
# enable `use_persistent_connection` to keep the connection alive between subsequent calls
|
28
|
+
c.use_persistent_connection = false # default: true
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
#### Use it
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
|
36
|
+
# Lookup a zip code by default in US
|
37
|
+
Zippopotamus.lookup_postal_code('90210')
|
38
|
+
|
39
|
+
# returns a Zippopotamus::Place
|
40
|
+
# @name: 'Beverly Hills'
|
41
|
+
# @region: 'California'
|
42
|
+
# @region_code: 'CA'
|
43
|
+
# @latitude: 34.0901
|
44
|
+
# @longitude: -118.4065
|
45
|
+
# @alternatives: []
|
46
|
+
# #has_alternatives?: false
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
# Lookup a postal code in France, with multiple place results (alternatives)
|
51
|
+
place = Zippopotamus.lookup_postal_code('01000', 'fr')
|
52
|
+
|
53
|
+
# returns the first Place in the list of multiple places
|
54
|
+
puts place.name
|
55
|
+
#> 'Bourg-en-Bresse'
|
56
|
+
|
57
|
+
# this place has alternatives (with same postal_code)
|
58
|
+
puts place.has_alternatives?
|
59
|
+
#> true
|
60
|
+
|
61
|
+
# get array of Zippotamus::Place representing alternatives (with the same postal_code)
|
62
|
+
puts place.alternatives
|
63
|
+
#> [#<Place @name="Saint-Denis-lès-Bourg", @region="Rhône-Alpes", @region_code="B9", @latitude=46.2022, @longitude=5.1892>]
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
# Works if postal code is a number (although not recommended)
|
68
|
+
Zippopotamus.lookup_postal_code(90210)
|
69
|
+
#=> #<Place @name="Beverly Hills", @region="California", @region_code="CA", @latitude=34.0901, @longitude=-118.4065, @alternatives=[]>
|
70
|
+
```
|
71
|
+
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create new Pull Request
|
80
|
+
|
81
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'excon'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Zippopotamus
|
5
|
+
|
6
|
+
class Client
|
7
|
+
|
8
|
+
|
9
|
+
def initialize(use_persistent_connection = false)
|
10
|
+
@connection = Excon.new('http://api.zippopotam.us', persistent: use_persistent_connection)
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
# @param [String] postal_code
|
16
|
+
# @param [String] country 2 letter country code (default: 'us')
|
17
|
+
def lookup_postal_code(postal_code, country = 'us')
|
18
|
+
postal_code = postal_code.to_s if postal_code.is_a?(Integer)
|
19
|
+
raise "Invalid postal_code parameter: '#{postal_code}'" if not_blank_string?(postal_code)
|
20
|
+
raise "Invalid country parameter: '#{country}'" if not_blank_string?(country) || country.length != 2
|
21
|
+
country = country.downcase
|
22
|
+
begin
|
23
|
+
r = @connection.get(path: "/#{country}/#{postal_code}")
|
24
|
+
rescue Excon::Errors::SocketError
|
25
|
+
# just retry
|
26
|
+
r = @connection.get(path: "/#{country}/#{postal_code}")
|
27
|
+
end
|
28
|
+
if r.status == 200
|
29
|
+
return Place.new(*JSON.parse(r.body)['places'])
|
30
|
+
else
|
31
|
+
return nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def not_blank_string?(str)
|
39
|
+
str.nil? || ( ! str.is_a?(String)) || str.strip.length == 0
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
class Place
|
49
|
+
|
50
|
+
attr_reader :name, :region, :region_code, :latitude, :longitude
|
51
|
+
attr_reader :alternatives
|
52
|
+
|
53
|
+
def initialize(*places)
|
54
|
+
place = places.shift
|
55
|
+
@name = place['place name']
|
56
|
+
@region = place['state']
|
57
|
+
@region_code = place['state abbreviation']
|
58
|
+
@latitude = place['latitude'].to_f if place['latitude']
|
59
|
+
@longitude = place['longitude'].to_f if place['longitude']
|
60
|
+
|
61
|
+
@alternatives = []
|
62
|
+
places.each do |place|
|
63
|
+
@alternatives << Place.new(place)
|
64
|
+
end
|
65
|
+
@alternatives.freeze
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
def has_alternatives?
|
70
|
+
@alternatives && @alternatives.length > 0
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def to_s
|
75
|
+
"#<Place name='#{name}', region='#{region} (#{region_code})', lat/lng='#{latitude},#{longitude}'#{has_alternatives? ? " alternatives=[#{alternatives.map(&:name).join(',')}]" : ''}>"
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
end
|
data/lib/zippopotamus.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'zippopotamus/version'
|
2
|
+
require 'zippopotamus/configuration'
|
3
|
+
require 'zippopotamus/client'
|
4
|
+
|
5
|
+
|
6
|
+
# Configure
|
7
|
+
#
|
8
|
+
# Zippopotamus.configure do |c|
|
9
|
+
# # enable `use_persistent_connection` to keep the connection alive between subsequent calls
|
10
|
+
# c.use_persistent_connection = fase # default: true
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
#
|
14
|
+
#
|
15
|
+
# Usage
|
16
|
+
#
|
17
|
+
# # Lookup US zip codes by default
|
18
|
+
# Zippopotamus.lookup_postal_code('90210')
|
19
|
+
#
|
20
|
+
# # Specify country by 2 letter code
|
21
|
+
# Zippopotamus.lookup_postal_code('01000', 'fr')
|
22
|
+
#
|
23
|
+
#
|
24
|
+
# # Raise an error when parameters are invalid
|
25
|
+
# Zippopotamus.lookup_postal_code(12345)
|
26
|
+
#
|
27
|
+
module Zippopotamus
|
28
|
+
|
29
|
+
extend self
|
30
|
+
|
31
|
+
def configure(&block)
|
32
|
+
block.call(get_configuration)
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
# lookup a postal_code for a country and return the place(s) for it
|
37
|
+
#
|
38
|
+
# @param [String] postal_code
|
39
|
+
# @param [String] country 2 letter country code (default: 'us')
|
40
|
+
# @return [Zippopotamus::Place] place with postal_code for country OR nil if not found
|
41
|
+
# @raise if invalid parameters
|
42
|
+
def lookup_postal_code(postal_code, country = 'us')
|
43
|
+
@client ||= Zippopotamus::Client.new(get_configuration.use_persistent_connection)
|
44
|
+
return @client.lookup_postal_code(postal_code, country)
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
|
52
|
+
def get_configuration
|
53
|
+
@configuration ||= Configuration.new
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
end
|
data/spec/place_spec.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
describe Zippopotamus::Place do
|
5
|
+
|
6
|
+
DATA_WO_ALTERNATIVES = [{"place name"=>"Beverly Hills", "longitude"=>"-118.4065", "state"=>"California", "state abbreviation"=>"CA", "latitude"=>"34.0901"}]
|
7
|
+
DATA_WITH_ALTERNATIVES = [{"place name"=>"Bourg-en-Bresse", "longitude"=>"5.2258", "state"=>"Rhône-Alpes", "state abbreviation"=>"B9", "latitude"=>"46.2057"},
|
8
|
+
{"place name"=>"Saint-Denis-lès-Bourg", "longitude"=>"5.1892", "state"=>"Rhône-Alpes", "state abbreviation"=>"B9", "latitude"=>"46.2022"}]
|
9
|
+
|
10
|
+
context "with no alternative places" do
|
11
|
+
before do
|
12
|
+
@place = Zippopotamus::Place.new(*DATA_WO_ALTERNATIVES)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "is a valid Place without alternatives" do
|
16
|
+
@place.name.should == 'Beverly Hills'
|
17
|
+
@place.region.should == 'California'
|
18
|
+
@place.region_code.should == 'CA'
|
19
|
+
@place.latitude.should be_within(1.0).of(34.0)
|
20
|
+
@place.longitude.should be_within(1.0).of(-118.4)
|
21
|
+
@place.has_alternatives?.should be_false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
context "with multiple alternative places" do
|
27
|
+
before do
|
28
|
+
@place = Zippopotamus::Place.new(*DATA_WITH_ALTERNATIVES)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "is a valid Place without alternatives" do
|
32
|
+
@place.name.should == 'Bourg-en-Bresse'
|
33
|
+
@place.region.should == 'Rhône-Alpes'
|
34
|
+
@place.region_code.should == 'B9'
|
35
|
+
@place.has_alternatives?.should be_true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "has valid values for alternative Place" do
|
39
|
+
place1 = @place.alternatives[0]
|
40
|
+
place1.should be_a(Zippopotamus::Place)
|
41
|
+
place1.name.should == 'Saint-Denis-lès-Bourg'
|
42
|
+
place1.region.should == 'Rhône-Alpes'
|
43
|
+
place1.region_code.should == 'B9'
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api.zippopotam.us/fr/01000
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- excon/0.31.0
|
12
|
+
response:
|
13
|
+
status:
|
14
|
+
code: 200
|
15
|
+
message: ''
|
16
|
+
headers:
|
17
|
+
Date:
|
18
|
+
- Mon, 13 Jan 2014 13:48:09 GMT
|
19
|
+
Server:
|
20
|
+
- Apache/2.2.20
|
21
|
+
Content-Length:
|
22
|
+
- '372'
|
23
|
+
X-Cache:
|
24
|
+
- hit
|
25
|
+
Charset:
|
26
|
+
- UTF-8
|
27
|
+
Vary:
|
28
|
+
- Accept-Encoding
|
29
|
+
Access-Control-Allow-Origin:
|
30
|
+
- "*"
|
31
|
+
Content-Type:
|
32
|
+
- application/json
|
33
|
+
body:
|
34
|
+
encoding: UTF-8
|
35
|
+
string: "{\"post code\": \"01000\", \"country\": \"France\", \"country abbreviation\":
|
36
|
+
\"FR\", \"places\": [{\"place name\": \"Bourg-en-Bresse\", \"longitude\":
|
37
|
+
\"5.2258\", \"state\": \"Rh\\u00f4ne-Alpes\", \"state abbreviation\": \"B9\",
|
38
|
+
\"latitude\": \"46.2057\"}, {\"place name\": \"Saint-Denis-l\\u00e8s-Bourg\",
|
39
|
+
\"longitude\": \"5.1892\", \"state\": \"Rh\\u00f4ne-Alpes\", \"state abbreviation\":
|
40
|
+
\"B9\", \"latitude\": \"46.2022\"}]}"
|
41
|
+
http_version:
|
42
|
+
recorded_at: Mon, 13 Jan 2014 13:48:09 GMT
|
43
|
+
recorded_with: VCR 2.8.0
|
@@ -0,0 +1,41 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api.zippopotam.us/us/90210
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- excon/0.31.0
|
12
|
+
response:
|
13
|
+
status:
|
14
|
+
code: 200
|
15
|
+
message: ''
|
16
|
+
headers:
|
17
|
+
Date:
|
18
|
+
- Mon, 13 Jan 2014 13:43:09 GMT
|
19
|
+
Server:
|
20
|
+
- Apache/2.2.20
|
21
|
+
Content-Length:
|
22
|
+
- '225'
|
23
|
+
X-Cache:
|
24
|
+
- hit
|
25
|
+
Charset:
|
26
|
+
- UTF-8
|
27
|
+
Vary:
|
28
|
+
- Accept-Encoding
|
29
|
+
Access-Control-Allow-Origin:
|
30
|
+
- "*"
|
31
|
+
Content-Type:
|
32
|
+
- application/json
|
33
|
+
body:
|
34
|
+
encoding: UTF-8
|
35
|
+
string: "{\"post code\": \"90210\", \"country\": \"United States\", \"country
|
36
|
+
abbreviation\": \"US\", \"places\": [{\"place name\": \"Beverly Hills\", \"longitude\":
|
37
|
+
\"-118.4065\", \"state\": \"California\", \"state abbreviation\": \"CA\",
|
38
|
+
\"latitude\": \"34.0901\"}]}"
|
39
|
+
http_version:
|
40
|
+
recorded_at: Mon, 13 Jan 2014 13:43:09 GMT
|
41
|
+
recorded_with: VCR 2.8.0
|
@@ -0,0 +1,36 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api.zippopotam.us/xx/1234
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- excon/0.31.0
|
12
|
+
response:
|
13
|
+
status:
|
14
|
+
code: 404
|
15
|
+
message: ''
|
16
|
+
headers:
|
17
|
+
Date:
|
18
|
+
- Mon, 13 Jan 2014 13:50:10 GMT
|
19
|
+
Server:
|
20
|
+
- Apache/2.2.20
|
21
|
+
Vary:
|
22
|
+
- Accept-Encoding
|
23
|
+
Access-Control-Allow-Origin:
|
24
|
+
- "*"
|
25
|
+
Charset:
|
26
|
+
- UTF-8
|
27
|
+
Content-Length:
|
28
|
+
- '2'
|
29
|
+
Content-Type:
|
30
|
+
- application/json
|
31
|
+
body:
|
32
|
+
encoding: UTF-8
|
33
|
+
string: "{}"
|
34
|
+
http_version:
|
35
|
+
recorded_at: Mon, 13 Jan 2014 13:50:10 GMT
|
36
|
+
recorded_with: VCR 2.8.0
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Zippopotamus do
|
4
|
+
|
5
|
+
context "VCR enabled tests" do
|
6
|
+
|
7
|
+
context "valid postal_code with single Place returned" do
|
8
|
+
before do
|
9
|
+
VCR.use_cassette 'us_90210' do
|
10
|
+
@result = Zippopotamus.lookup_postal_code!('90210')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns correct data" do
|
15
|
+
@result.should be_a(Zippopotamus::Place)
|
16
|
+
@result.name.should == 'Beverly Hills'
|
17
|
+
@result.region.should == 'California'
|
18
|
+
@result.region_code.should == 'CA'
|
19
|
+
@result.latitude.should be_within(2.0).of(34.0)
|
20
|
+
@result.longitude.should be_within(2.0).of(-118.4)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
context "valid postal_code with multiple Places returned" do
|
27
|
+
before do
|
28
|
+
VCR.use_cassette 'fr_01000' do
|
29
|
+
@result = Zippopotamus.lookup_postal_code!('01000', 'fr')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns correct data" do
|
34
|
+
@result.should be_a(Zippopotamus::Place)
|
35
|
+
@result.name.should == 'Bourg-en-Bresse'
|
36
|
+
@result.region.should == 'Rhône-Alpes'
|
37
|
+
@result.region_code.should == 'B9'
|
38
|
+
@result.latitude.should be_within(2.0).of(46.2)
|
39
|
+
@result.longitude.should be_within(2.0).of(5.2)
|
40
|
+
@result.has_alternatives?.should be_true
|
41
|
+
|
42
|
+
@result.alternatives[0].name.should == 'Saint-Denis-lès-Bourg'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
context "invalid postal_code or country" do
|
48
|
+
before do
|
49
|
+
VCR.use_cassette 'xx_1234' do
|
50
|
+
@result = Zippopotamus.lookup_postal_code('1234', 'xx')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
it "returns nil" do
|
56
|
+
@result.should be_nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
context "live HTTP tests with US/90210 valid data" do
|
66
|
+
|
67
|
+
before do
|
68
|
+
@result = Zippopotamus.lookup_postal_code!('90210')
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns correct data" do
|
72
|
+
@result.should be_a(Zippopotamus::Place)
|
73
|
+
@result.name.should == 'Beverly Hills'
|
74
|
+
@result.region.should == 'California'
|
75
|
+
@result.region_code.should == 'CA'
|
76
|
+
@result.latitude.should be_within(2.0).of(34.0)
|
77
|
+
@result.longitude.should be_within(2.0).of(-118.4)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'zippopotamus/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'zippopotamus-client'
|
8
|
+
spec.version = Zippopotamus::VERSION
|
9
|
+
spec.authors = ['Radu Cugut']
|
10
|
+
spec.email = ['rcugut@gmail.com']
|
11
|
+
spec.description = %q{Client for zippopotam.us API}
|
12
|
+
spec.summary = %q{Client for zippopotam.us API}
|
13
|
+
spec.homepage = 'https://github.com/rcugut/zippopotamus-ruby-client'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split("\n")
|
17
|
+
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_development_dependency 'rspec'
|
23
|
+
spec.add_development_dependency 'vcr', '~> 2.8.0'
|
24
|
+
spec.add_development_dependency 'webmock', '~> 1.15', '< 1.16'
|
25
|
+
|
26
|
+
spec.add_runtime_dependency 'excon', '>= 0', '< 1.0.0'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zippopotamus-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Radu Cugut
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: vcr
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.8.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.8.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.15'
|
62
|
+
- - "<"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '1.16'
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '1.15'
|
72
|
+
- - "<"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.16'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: excon
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
- - "<"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 1.0.0
|
85
|
+
type: :runtime
|
86
|
+
prerelease: false
|
87
|
+
version_requirements: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
- - "<"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 1.0.0
|
95
|
+
description: Client for zippopotam.us API
|
96
|
+
email:
|
97
|
+
- rcugut@gmail.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- ".gitignore"
|
103
|
+
- ".rspec"
|
104
|
+
- Gemfile
|
105
|
+
- Gemfile.lock
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/zippopotamus.rb
|
110
|
+
- lib/zippopotamus/client.rb
|
111
|
+
- lib/zippopotamus/configuration.rb
|
112
|
+
- lib/zippopotamus/version.rb
|
113
|
+
- spec/place_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
- spec/vcr_cassettes/fr_01000.yml
|
116
|
+
- spec/vcr_cassettes/us_90210.yml
|
117
|
+
- spec/vcr_cassettes/xx_1234.yml
|
118
|
+
- spec/zippopotamus_spec.rb
|
119
|
+
- zippopotamus.gemspec
|
120
|
+
homepage: https://github.com/rcugut/zippopotamus-ruby-client
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.2.1
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Client for zippopotam.us API
|
144
|
+
test_files:
|
145
|
+
- spec/place_spec.rb
|
146
|
+
- spec/spec_helper.rb
|
147
|
+
- spec/vcr_cassettes/fr_01000.yml
|
148
|
+
- spec/vcr_cassettes/us_90210.yml
|
149
|
+
- spec/vcr_cassettes/xx_1234.yml
|
150
|
+
- spec/zippopotamus_spec.rb
|