postcodes_io 0.2.0 → 0.3.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.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/README.md +15 -6
- data/Rakefile +7 -2
- data/lib/postcodes_io.rb +2 -0
- data/lib/postcodes_io/reverse_geocode.rb +20 -0
- data/lib/postcodes_io/version.rb +1 -1
- data/spec/fixtures/no_results_response.json +1 -0
- data/spec/fixtures/reverse_geocode_response.json +1 -0
- data/spec/reverse_geocode_spec.rb +52 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2af10edff2373cf3d3de97762a174f7e1da23241
|
4
|
+
data.tar.gz: 5916275a146d0a847586aff21213c96909f66e5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1f5a7279e5f42b2a5409c515b3ca94d7c1efdcb4f79d63dedc47a49064b4374bc5b9a520c7d3633e2883e21bdb3d10894522371209bd514e49aee45a13c3836
|
7
|
+
data.tar.gz: 25036a30da99012db100939d972250b82590bc1c298b907b1cf9d722f39fccbbb61d38671cabda92ac22ba2847fcf643a9e3f06883c6e5f64a56fd0b98f8c1cd
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[](https://travis-ci.org/jamesruston/postcodes_io)
|
1
2
|
# PostcodesIo
|
2
3
|
|
3
4
|
A simple wrapper around [postcodes.io](http://postcodes.io/)
|
@@ -32,7 +33,7 @@ Create new instance of the Postcodes::IO
|
|
32
33
|
pio = Postcodes::IO.new
|
33
34
|
```
|
34
35
|
|
35
|
-
|
36
|
+
### Lookup a postcode
|
36
37
|
```ruby
|
37
38
|
postcode = pio.lookup('NN12 8TN')
|
38
39
|
```
|
@@ -43,7 +44,7 @@ postcode.longitude # -1.02144562675883
|
|
43
44
|
postcode.latitude # 52.0776806788868
|
44
45
|
```
|
45
46
|
|
46
|
-
|
47
|
+
### Make a batch postcode lookup request
|
47
48
|
|
48
49
|
> max 100 postcodes per request
|
49
50
|
|
@@ -54,14 +55,22 @@ postcodes.each do |p|
|
|
54
55
|
end
|
55
56
|
```
|
56
57
|
|
57
|
-
|
58
|
+
### Autocomplete a postcode
|
58
59
|
|
59
60
|
```ruby
|
60
61
|
postcodes = pio.autocomplete('NN12')
|
61
62
|
puts postcodes.list # ["NN12 6AA", "NN12 6AB", "NN12 6AD", "NN12 6AE", "NN12 6AF", "NN12 6AG", "NN12 6AH", "NN12 6AJ", "NN12 6AL", "NN12 6AN"]
|
62
63
|
```
|
63
64
|
|
64
|
-
|
65
|
+
|
66
|
+
### Perform a reverse geocode lookup
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
postcodes = pio.reverse_geocode(longitude: 0.629834, latitude: 51.79232)
|
70
|
+
puts postcodes.first.postcode # CM8 1EF
|
71
|
+
```
|
72
|
+
|
73
|
+
### All available fields
|
65
74
|
|
66
75
|
|name|description|
|
67
76
|
|----|-----------|
|
@@ -82,14 +91,14 @@ puts postcodes.list # ["NN12 6AA", "NN12 6AB", "NN12 6AD", "NN12 6AE", "NN12 6AF
|
|
82
91
|
|region| [former GORs](http://www.ons.gov.uk/ons/guide-method/geography/beginner-s-guide/administrative/england/government-office-regions/index.html)|
|
83
92
|
|parish| The smallest type of administrative area|
|
84
93
|
|lsoa| [The 2011 Census lower layer SOA code](http://www.ons.gov.uk/ons/guide-method/geography/beginner-s-guide/census/super-output-areas--soas-/index.html)|
|
85
|
-
|msoa| [The 2011 Census middle layer SOA (MSOA) code](
|
94
|
+
|msoa| [The 2011 Census middle layer SOA (MSOA) code](http://www.ons.gov.uk/ons/guide-method/geography/beginner-s-guide/census/super-output-areas--soas-/index.html)|
|
86
95
|
|ccg| Clinical Commissioning Group |
|
87
96
|
|nuts| [see here](http://en.wikipedia.org/wiki/Nomenclature_of_Territorial_Units_for_Statistics)|
|
88
97
|
|
89
98
|
For more details see [the documentation on postcodes.io](http://postcodes.io/docs)
|
90
99
|
## Contributing
|
91
100
|
|
92
|
-
1. Fork it ( https://github.com/
|
101
|
+
1. Fork it ( https://github.com/jamesruston/postcodes_io/fork )
|
93
102
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
94
103
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
95
104
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
data/lib/postcodes_io.rb
CHANGED
@@ -2,10 +2,12 @@ require "postcodes_io/version"
|
|
2
2
|
require "postcodes_io/lookup"
|
3
3
|
require "postcodes_io/autocomplete"
|
4
4
|
require "postcodes_io/postcode"
|
5
|
+
require "postcodes_io/reverse_geocode"
|
5
6
|
|
6
7
|
module Postcodes
|
7
8
|
class IO
|
8
9
|
include Lookup
|
9
10
|
include Autocomplete
|
11
|
+
include ReverseGeocode
|
10
12
|
end
|
11
13
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'excon'
|
2
|
+
require 'json'
|
3
|
+
require 'postcodes_io/postcode'
|
4
|
+
|
5
|
+
module Postcodes
|
6
|
+
module ReverseGeocode
|
7
|
+
def reverse_geocode(longitude:, latitude:, limit: 10, radius: 100, wide_search: false)
|
8
|
+
response = Excon.get(
|
9
|
+
"https://api.postcodes.io/postcodes?lon=#{longitude}&lat=#{latitude}&limit=#{limit}&radius=#{radius}&wideSearch=#{wide_search}")
|
10
|
+
return unless response.status == 200
|
11
|
+
|
12
|
+
json = JSON.parse(response.body)
|
13
|
+
return unless json['result']
|
14
|
+
|
15
|
+
json['result'].map do |result|
|
16
|
+
Postcode.new(result)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/postcodes_io/version.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
{"status":200,"result":null}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"status":200,"result":[{"postcode":"PR7 4HF","quality":1,"eastings":360059,"northings":412765,"country":"England","nhs_ha":"North West","longitude":-2.60515477763266,"latitude":53.6099106263766,"european_electoral_region":"North West","primary_care_trust":"Central Lancashire","region":"North West","lsoa":"Chorley 014C","msoa":"Chorley 014","incode":"4HF","outcode":"PR7","distance":0,"parliamentary_constituency":"Chorley","admin_district":"Chorley","parish":"Adlington","admin_county":"Lancashire","admin_ward":"Adlington and Anderton","ccg":"NHS Chorley and South Ribble","nuts":"Chorley and West Lancashire","codes":{"admin_district":"E07000118","admin_county":"E10000017","admin_ward":"E05005165","parish":"E04005139","parliamentary_constituency":"E14000637","ccg":"E38000034","nuts":"UKD47"}},{"postcode":"PR7 4HR","quality":1,"eastings":360038,"northings":412741,"country":"England","nhs_ha":"North West","longitude":-2.60546907187036,"latitude":53.6096933152903,"european_electoral_region":"North West","primary_care_trust":"Central Lancashire","region":"North West","lsoa":"Chorley 014C","msoa":"Chorley 014","incode":"4HR","outcode":"PR7","distance":31.90158431,"parliamentary_constituency":"Chorley","admin_district":"Chorley","parish":"Adlington","admin_county":"Lancashire","admin_ward":"Adlington and Anderton","ccg":"NHS Chorley and South Ribble","nuts":"Chorley and West Lancashire","codes":{"admin_district":"E07000118","admin_county":"E10000017","admin_ward":"E05005165","parish":"E04005139","parliamentary_constituency":"E14000637","ccg":"E38000034","nuts":"UKD47"}},{"postcode":"PR7 4HJ","quality":1,"eastings":360076,"northings":412843,"country":"England","nhs_ha":"North West","longitude":-2.60490787349104,"latitude":53.6106129688375,"european_electoral_region":"North West","primary_care_trust":"Central Lancashire","region":"North West","lsoa":"Chorley 014C","msoa":"Chorley 014","incode":"4HJ","outcode":"PR7","distance":79.85903188,"parliamentary_constituency":"Chorley","admin_district":"Chorley","parish":"Adlington","admin_county":"Lancashire","admin_ward":"Adlington and Anderton","ccg":"NHS Chorley and South Ribble","nuts":"Chorley and West Lancashire","codes":{"admin_district":"E07000118","admin_county":"E10000017","admin_ward":"E05005165","parish":"E04005139","parliamentary_constituency":"E14000637","ccg":"E38000034","nuts":"UKD47"}}]}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Postcodes::IO, '#reverse_geocode' do
|
4
|
+
let(:latitiude) { '53.6099106263766' }
|
5
|
+
let(:longitude) { '2.60515477763266' }
|
6
|
+
let(:base_url) { 'https://api.postcodes.io' }
|
7
|
+
let(:url) { "#{base_url}/postcodes?lon=#{longitude}&lat=#{latitiude}&limit=10&radius=100&wideSearch=false" }
|
8
|
+
let(:no_results_response) do
|
9
|
+
File.read('spec/fixtures/no_results_response.json')
|
10
|
+
end
|
11
|
+
let(:reverse_geocode_response) do
|
12
|
+
File.read('spec/fixtures/reverse_geocode_response.json')
|
13
|
+
end
|
14
|
+
|
15
|
+
subject(:reverse_geocode) do
|
16
|
+
described_class.new.reverse_geocode(
|
17
|
+
longitude: longitude, latitude: latitiude)
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when a 400 is returned' do
|
21
|
+
before do
|
22
|
+
stub_request(:get, url).to_return(status: 400)
|
23
|
+
end
|
24
|
+
|
25
|
+
it { is_expected.to be_nil }
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when there are no results returned' do
|
29
|
+
before do
|
30
|
+
stub_request(:get, url).to_return(status: 200, body: no_results_response)
|
31
|
+
end
|
32
|
+
|
33
|
+
it { is_expected.to be_nil }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when results are returned' do
|
37
|
+
before do
|
38
|
+
stub_request(:get, url)
|
39
|
+
.to_return(status: 200, body: reverse_geocode_response)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns the correct number of postcodes' do
|
43
|
+
expect(reverse_geocode.count).to eq(3)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'instantiates an object for each returned postcode' do
|
47
|
+
reverse_geocode.each do |result|
|
48
|
+
expect(result).to be_a(Postcodes::Postcode)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postcodes_io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Ruston
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -89,6 +89,7 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
91
|
- ".rspec"
|
92
|
+
- ".travis.yml"
|
92
93
|
- Gemfile
|
93
94
|
- LICENSE.txt
|
94
95
|
- README.md
|
@@ -99,14 +100,18 @@ files:
|
|
99
100
|
- lib/postcodes_io/base.rb
|
100
101
|
- lib/postcodes_io/lookup.rb
|
101
102
|
- lib/postcodes_io/postcode.rb
|
103
|
+
- lib/postcodes_io/reverse_geocode.rb
|
102
104
|
- lib/postcodes_io/version.rb
|
103
105
|
- postcodes_io.gemspec
|
104
106
|
- spec/autocomplete_spec.rb
|
105
107
|
- spec/fixtures/autocomplete_response.json
|
106
108
|
- spec/fixtures/lookup_multi_response.json
|
107
109
|
- spec/fixtures/lookup_response.json
|
110
|
+
- spec/fixtures/no_results_response.json
|
111
|
+
- spec/fixtures/reverse_geocode_response.json
|
108
112
|
- spec/lookup_spec.rb
|
109
113
|
- spec/postcode_io_spec.rb
|
114
|
+
- spec/reverse_geocode_spec.rb
|
110
115
|
- spec/spec_helper.rb
|
111
116
|
homepage: ''
|
112
117
|
licenses:
|
@@ -137,6 +142,9 @@ test_files:
|
|
137
142
|
- spec/fixtures/autocomplete_response.json
|
138
143
|
- spec/fixtures/lookup_multi_response.json
|
139
144
|
- spec/fixtures/lookup_response.json
|
145
|
+
- spec/fixtures/no_results_response.json
|
146
|
+
- spec/fixtures/reverse_geocode_response.json
|
140
147
|
- spec/lookup_spec.rb
|
141
148
|
- spec/postcode_io_spec.rb
|
149
|
+
- spec/reverse_geocode_spec.rb
|
142
150
|
- spec/spec_helper.rb
|