ww-postcodeapi 0.0.1
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/.gitignore +18 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/lib/postcodeapi.rb +51 -0
- data/lib/postcodeapi/version.rb +3 -0
- data/postcodeapi.gemspec +27 -0
- data/spec/lib/postcode_api_spec.rb +46 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/vcr/get_street.yml +42 -0
- metadata +172 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 martijnschouwe
|
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,51 @@
|
|
1
|
+
# Postcodeapi
|
2
|
+
|
3
|
+
[](https://travis-ci.org/martijnschouwe/postcodeapi) [](https://codeclimate.com/github/martijnschouwe/postcodeapi) [](http://badge.fury.io/rb/postcodeapi)
|
4
|
+
|
5
|
+
API wrapper for postcodeapi.nu zipcode api.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'ww-postcodeapi'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install postcodeapi
|
20
|
+
|
21
|
+
## Setup
|
22
|
+
|
23
|
+
Add file `config/initializers/postcodeapi.rb`.
|
24
|
+
|
25
|
+
require 'postcodeapi'
|
26
|
+
Postcodeapi.configure do |config|
|
27
|
+
config.api_key = "1234567890abcdefghizjklmnopqrstuvwxyz"
|
28
|
+
end
|
29
|
+
|
30
|
+
## Example usage
|
31
|
+
|
32
|
+
Get an address for a zipcode:
|
33
|
+
|
34
|
+
response = Postcodeapi.get_street("5041EB")
|
35
|
+
# => #<OpenStruct street="Wilhelminapark", postcode="5041EB", town="Tilburg", latitude=51.9401, longitude=5.61531, x=133505, y=397537>
|
36
|
+
|
37
|
+
|
38
|
+
You can then use:
|
39
|
+
|
40
|
+
response.street
|
41
|
+
# => "Wilhelminapark"
|
42
|
+
response.town
|
43
|
+
# => "Tilburg"
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/postcodeapi.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "postcodeapi/version"
|
2
|
+
require "rest-client"
|
3
|
+
require "ostruct"
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Postcodeapi
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :api_url, :api_key
|
10
|
+
def configure(&blk); class_eval(&blk); end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.get_street(zipcode)
|
14
|
+
# Format the zipcode
|
15
|
+
zipcode = format_zipcode(zipcode)
|
16
|
+
# Perform request
|
17
|
+
response = RestClient.get "#{@api_url}/#{zipcode}", {:content_type => :json, :accept => :json, :'Api-Key' => @api_key}
|
18
|
+
# Return openstruct output
|
19
|
+
res = JSON.parse(response.to_str,{symbolize_names: true})
|
20
|
+
OpenStruct.new res[:resource]
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.format_zipcode(zipcode = nil)
|
24
|
+
if zipcode.nil? || zipcode == ""
|
25
|
+
raise Exception.new("No zipcode provided")
|
26
|
+
elsif (zipcode =~ /^[0-9]{4}$|[0-9]{4}[\ ]{1}[a-zA-Z]{2}$|[0-9]{4}[a-zA-Z]{2}$/).nil?
|
27
|
+
raise Exception.new("Incorrect zipcode format (1234AB format expected)")
|
28
|
+
else
|
29
|
+
zipcode.gsub(" ", "").upcase
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.stringify(obj)
|
34
|
+
if obj.is_a? Array
|
35
|
+
obj.join
|
36
|
+
elsif obj.is_a? Hash
|
37
|
+
obj.merge( obj ) {|k, val| stringify val }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.version_string
|
42
|
+
"Postcodeapi version #{Postcodeapi::VERSION}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Set default configuration
|
47
|
+
Postcodeapi.configure do
|
48
|
+
@api_url = 'http://api.postcodeapi.nu'
|
49
|
+
@api_key = '1234567890abcdefghizjklmnopqrstuvwxyz'
|
50
|
+
end
|
51
|
+
|
data/postcodeapi.gemspec
ADDED
@@ -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 'postcodeapi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ww-postcodeapi"
|
8
|
+
spec.version = Postcodeapi::VERSION
|
9
|
+
spec.authors = ["martijnschouwe"]
|
10
|
+
spec.email = ["martijnschouwe@hotmail.com"]
|
11
|
+
spec.description = %q{API wrapper for postcodeapi.nu zipcode api}
|
12
|
+
spec.summary = %q{Get address details from the postcodeapi.nu api}
|
13
|
+
spec.homepage = "https://github.com/martijnschouwe/postcodeapi"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec", "~> 2.13.0"
|
23
|
+
spec.add_development_dependency "capybara", "~> 2.1.0"
|
24
|
+
spec.add_development_dependency "vcr", "~> 2.4.0"
|
25
|
+
spec.add_development_dependency "webmock", "~> 1.8.0"
|
26
|
+
spec.add_runtime_dependency "rest-client"
|
27
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Postcodeapi do
|
4
|
+
it 'should remove spaces from zipcode' do
|
5
|
+
Postcodeapi.format_zipcode("1234 AB").should eq "1234AB"
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should upcase the zipcode' do
|
9
|
+
Postcodeapi.format_zipcode("1234ab").should eq "1234AB"
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should raise an exception' do
|
13
|
+
expect { Postcodeapi.format_zipcode(nil) }.to raise_error
|
14
|
+
expect { Postcodeapi.format_zipcode("") }.to raise_error
|
15
|
+
expect { Postcodeapi.format_zipcode("111") }.to raise_error
|
16
|
+
expect { Postcodeapi.format_zipcode("1111A") }.to raise_error
|
17
|
+
expect { Postcodeapi.format_zipcode("11111A") }.to raise_error
|
18
|
+
expect { Postcodeapi.format_zipcode("AAAA") }.to raise_error
|
19
|
+
expect { Postcodeapi.format_zipcode("11AA") }.to raise_error
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should return an address result' do
|
23
|
+
VCR.use_cassette('get_street', :allow_playback_repeats => true) do
|
24
|
+
json_response_mock = '{
|
25
|
+
"success": true,
|
26
|
+
"resource": {
|
27
|
+
"street": "Wilhelminapark",
|
28
|
+
"postcode": "5041EB",
|
29
|
+
"town": "Tilburg",
|
30
|
+
"latitude": 51.9401,
|
31
|
+
"longitude": 5.61531,
|
32
|
+
"x": 133505,
|
33
|
+
"y": 397537
|
34
|
+
}
|
35
|
+
}'
|
36
|
+
res = JSON.parse(json_response_mock.to_str,{symbolize_names: true})
|
37
|
+
Postcodeapi.get_street("5041EB").should eq OpenStruct.new(res[:resource])
|
38
|
+
Postcodeapi.get_street("5041EB").street.should eq "Wilhelminapark"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
it 'should return correct version string' do
|
44
|
+
Postcodeapi.version_string.should eq "Postcodeapi version #{Postcodeapi::VERSION}"
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'vcr'
|
3
|
+
require 'webmock'
|
4
|
+
|
5
|
+
require 'postcodeapi'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.color_enabled = true
|
9
|
+
config.formatter = 'documentation'
|
10
|
+
end
|
11
|
+
|
12
|
+
VCR.configure do |config|
|
13
|
+
config.cassette_library_dir = 'spec/vcr'
|
14
|
+
config.hook_into :webmock
|
15
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
-
|
4
|
+
recorded_at: "Mon, 09 Dec 2013 15:02:48 GMT"
|
5
|
+
request:
|
6
|
+
body:
|
7
|
+
encoding: UTF-8
|
8
|
+
string: ""
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- "*/*; q=0.5, application/json"
|
12
|
+
Accept-Encoding:
|
13
|
+
- "gzip, deflate"
|
14
|
+
Api-Key:
|
15
|
+
- 1234567890abcdefghizjklmnopqrstuvwxyz
|
16
|
+
User-Agent:
|
17
|
+
- Ruby
|
18
|
+
method: get
|
19
|
+
uri: "http://api.postcodeapi.nu/5041EB"
|
20
|
+
response:
|
21
|
+
body:
|
22
|
+
encoding: UTF-8
|
23
|
+
string: "{ \"success\": true, \"resource\": { \"street\": \"Wilhelminapark\", \"postcode\": \"5041EB\", \"town\": \"Tilburg\", \"latitude\": 51.9401, \"longitude\": 5.61531, \"x\": 133505, \"y\": 397537 } }"
|
24
|
+
headers:
|
25
|
+
Alternate-Protocol:
|
26
|
+
- "80:quic,80:quic"
|
27
|
+
Cache-Control:
|
28
|
+
- no-cache
|
29
|
+
Content-Type:
|
30
|
+
- "application/json; charset=utf-8"
|
31
|
+
Date:
|
32
|
+
- "Mon, 09 Dec 2013 15:02:48 GMT"
|
33
|
+
Server:
|
34
|
+
- "Google Frontend"
|
35
|
+
Transfer-Encoding:
|
36
|
+
- chunked
|
37
|
+
Vary:
|
38
|
+
- Accept-Encoding
|
39
|
+
http_version: ~
|
40
|
+
status:
|
41
|
+
code: 200
|
42
|
+
message: OK
|
metadata
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ww-postcodeapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- martijnschouwe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
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'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
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: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.13.0
|
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: 2.13.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: capybara
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.1.0
|
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: 2.1.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: vcr
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 2.4.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 2.4.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: webmock
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.8.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: 1.8.0
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: rest-client
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description: API wrapper for postcodeapi.nu zipcode api
|
127
|
+
email:
|
128
|
+
- martijnschouwe@hotmail.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- .travis.yml
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE.txt
|
137
|
+
- README.md
|
138
|
+
- Rakefile
|
139
|
+
- lib/postcodeapi.rb
|
140
|
+
- lib/postcodeapi/version.rb
|
141
|
+
- postcodeapi.gemspec
|
142
|
+
- spec/lib/postcode_api_spec.rb
|
143
|
+
- spec/spec_helper.rb
|
144
|
+
- spec/vcr/get_street.yml
|
145
|
+
homepage: https://github.com/martijnschouwe/postcodeapi
|
146
|
+
licenses: []
|
147
|
+
post_install_message:
|
148
|
+
rdoc_options: []
|
149
|
+
require_paths:
|
150
|
+
- lib
|
151
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ! '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
158
|
+
none: false
|
159
|
+
requirements:
|
160
|
+
- - ! '>='
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 1.8.25
|
166
|
+
signing_key:
|
167
|
+
specification_version: 3
|
168
|
+
summary: Get address details from the postcodeapi.nu api
|
169
|
+
test_files:
|
170
|
+
- spec/lib/postcode_api_spec.rb
|
171
|
+
- spec/spec_helper.rb
|
172
|
+
- spec/vcr/get_street.yml
|