postcodes_io 0.2.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a025c5964930e2fdd31f1d4ac696d14ef8db7244
4
- data.tar.gz: 3d5e0090a957e1a6af46f68d1204eb0c2ee9b5a9
2
+ SHA256:
3
+ metadata.gz: 2c6a93dd223ae7a6cc8c04b1f7079f955eb4d93d0ebab8bd14f6cffa87f57b82
4
+ data.tar.gz: 28052e4ba75fa32901fc03b8997cbc67575df1e97c7902fbead915086b88f758
5
5
  SHA512:
6
- metadata.gz: 5ca6179bdb36644e0d64dc65f3ff37efa574cbe9e0440200ef788514de7728c9c6d0fa95c87adaa258022f0d8aca834d4c0177c4ef8de817ab2610bc6e98727d
7
- data.tar.gz: 1cc677ceaf30e1c0db12e0c44d604a77b35b44d05533a81440e545553806fca89f1f7222bfaa8b1fa321b9fd8f4b00a9cb78ae35b8b8048896e331349a7e3ea6
6
+ metadata.gz: 3cb7ab91ceba1f04d2aed32d96a5c4b1c9754c06fef797246311ed340aeaa78cd69157f05f59a9a47d04f6ba3c8d66a537108cd361f00940d30a3189e88b1342
7
+ data.tar.gz: e91286586e6dd99db43ae487df61d621df6e623877facc54117a132eab964a2f8d506c3c27fb8655d956a771bce39ec0859f3398b3375253b54b2766b678fe2f
@@ -0,0 +1,43 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+
7
+ jobs:
8
+ build:
9
+ name: Build + Publish
10
+ runs-on: ubuntu-latest
11
+ permissions:
12
+ contents: read
13
+ packages: write
14
+
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - name: Set up Ruby 2.6
18
+ uses: actions/setup-ruby@v1
19
+ with:
20
+ ruby-version: 2.6.x
21
+
22
+ - name: Publish to GPR
23
+ run: |
24
+ mkdir -p $HOME/.gem
25
+ touch $HOME/.gem/credentials
26
+ chmod 0600 $HOME/.gem/credentials
27
+ printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
28
+ gem build *.gemspec
29
+ gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
30
+ env:
31
+ GEM_HOST_API_KEY: "Bearer ${{secrets.GITHUB_TOKEN}}"
32
+ OWNER: ${{ github.repository_owner }}
33
+
34
+ - name: Publish to RubyGems
35
+ run: |
36
+ mkdir -p $HOME/.gem
37
+ touch $HOME/.gem/credentials
38
+ chmod 0600 $HOME/.gem/credentials
39
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
40
+ gem build *.gemspec
41
+ gem push *.gem
42
+ env:
43
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
@@ -0,0 +1,35 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.6', '2.7', '3.0']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Set up Ruby
27
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
+ # uses: ruby/setup-ruby@v1
30
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
31
+ with:
32
+ ruby-version: ${{ matrix.ruby-version }}
33
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
+ - name: Run tests
35
+ run: bundle exec rake
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Build Status](https://travis-ci.org/jamesruston/postcodes_io.svg?branch=master)](https://travis-ci.org/jamesruston/postcodes_io) [![Gem Version](https://badge.fury.io/rb/postcodes_io.svg)](https://badge.fury.io/rb/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
- ####Lookup a postcode
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
- ####Make a batch postcode lookup request
47
+ ### Make a batch postcode lookup request
47
48
 
48
49
  > max 100 postcodes per request
49
50
 
@@ -54,14 +55,33 @@ postcodes.each do |p|
54
55
  end
55
56
  ```
56
57
 
57
- ####Autocomplete a postcode
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
- ##All available fields
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
+ ### Lookup a terminated postcode
74
+ ```ruby
75
+ postcode = pio.lookup_terminated('SO23 7SP')
76
+ ```
77
+
78
+ Inspect the results!
79
+ ```ruby
80
+ postcode.longitude # -1.02144562675883
81
+ postcode.latitude # 52.0776806788868
82
+ ```
83
+
84
+ ### All available fields
65
85
 
66
86
  |name|description|
67
87
  |----|-----------|
@@ -82,14 +102,14 @@ puts postcodes.list # ["NN12 6AA", "NN12 6AB", "NN12 6AD", "NN12 6AE", "NN12 6AF
82
102
  |region| [former GORs](http://www.ons.gov.uk/ons/guide-method/geography/beginner-s-guide/administrative/england/government-office-regions/index.html)|
83
103
  |parish| The smallest type of administrative area|
84
104
  |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](The 2011 Census lower layer SOA code for England and Wales](http://www.ons.gov.uk/ons/guide-method/geography/beginner-s-guide/census/super-output-areas--soas-/index.html)|
105
+ |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
106
  |ccg| Clinical Commissioning Group |
87
107
  |nuts| [see here](http://en.wikipedia.org/wiki/Nomenclature_of_Territorial_Units_for_Statistics)|
88
108
 
89
109
  For more details see [the documentation on postcodes.io](http://postcodes.io/docs)
90
110
  ## Contributing
91
111
 
92
- 1. Fork it ( https://github.com/[my-github-username]/postcodes_io/fork )
112
+ 1. Fork it ( https://github.com/jamesruston/postcodes_io/fork )
93
113
  2. Create your feature branch (`git checkout -b my-new-feature`)
94
114
  3. Commit your changes (`git commit -am 'Add some feature'`)
95
115
  4. Push to the branch (`git push origin my-new-feature`)
data/Rakefile CHANGED
@@ -1,5 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
2
7
 
3
8
  task :console do
4
9
  exec "irb -r postcodes_io -I ./lib"
5
- end
10
+ end
@@ -0,0 +1,33 @@
1
+ require 'excon'
2
+ require 'json'
3
+ require 'postcodes_io/postcode'
4
+
5
+ module Postcodes
6
+ module LookupTerminated
7
+ def lookup_terminated(postcode)
8
+ lookup_terminated_postcode postcode
9
+ end
10
+
11
+ private
12
+
13
+ def lookup_terminated_postcode(postcode)
14
+ postcode = remove_whitespace postcode
15
+ response = Excon.get("https://api.postcodes.io/terminated_postcodes/#{postcode}")
16
+
17
+ unless response.status == 404
18
+ parsed_response = JSON.parse(response.body)
19
+ return Postcodes::Postcode.new(parsed_response['result'])
20
+ end
21
+ nil
22
+ end
23
+
24
+ def remove_whitespace(string)
25
+ string.gsub(/\s+/, '') # remove any whitespace. m1 1ab => m11ab
26
+ end
27
+
28
+ def process_response(response)
29
+ yield JSON.parse(response.body) unless response.status == 404
30
+ nil
31
+ end
32
+ end
33
+ 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
@@ -1,3 +1,3 @@
1
1
  module Postcodes
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/postcodes_io.rb CHANGED
@@ -1,11 +1,15 @@
1
- require "postcodes_io/version"
2
- require "postcodes_io/lookup"
3
- require "postcodes_io/autocomplete"
4
- require "postcodes_io/postcode"
1
+ require 'postcodes_io/version'
2
+ require 'postcodes_io/lookup'
3
+ require 'postcodes_io/autocomplete'
4
+ require 'postcodes_io/postcode'
5
+ require 'postcodes_io/reverse_geocode'
6
+ require 'postcodes_io/lookup_terminated'
5
7
 
6
8
  module Postcodes
7
9
  class IO
8
10
  include Lookup
9
11
  include Autocomplete
12
+ include ReverseGeocode
13
+ include LookupTerminated
10
14
  end
11
15
  end
data/postcodes_io.gemspec CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.7"
22
- spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "webmock"
25
25
 
@@ -0,0 +1,10 @@
1
+ {
2
+ "status": 200,
3
+ "result": {
4
+ "postcode": "SO23 9DD",
5
+ "year_terminated": 2001,
6
+ "month_terminated": 8,
7
+ "longitude": -1.316261,
8
+ "latitude": 51.062297
9
+ }
10
+ }
@@ -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,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Postcodes::IO do
4
+ let(:base_url) { 'https://api.postcodes.io' }
5
+ let(:stub_response) { File.read('spec/fixtures/lookup_terminated_response.json') }
6
+
7
+ describe '#lookup_terminated' do
8
+ before :each do
9
+ stub_request(:get, "#{base_url}/terminated_postcodes/CM129TR")
10
+ .to_return(status: 200, body: stub_response)
11
+ end
12
+
13
+ let(:input) { 'CM12 9TR' }
14
+ let(:output) { subject.lookup_terminated(input) }
15
+
16
+ it 'requests a terminated postcode' do
17
+ output
18
+ WebMock.should have_requested(:get, "#{base_url}/terminated_postcodes/CM129TR")
19
+ end
20
+
21
+ it 'returns the long and lat' do
22
+ output.longitude.should == -1.316261
23
+ output.latitude.should == 51.062297
24
+ end
25
+ end
26
+ end
@@ -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,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postcodes_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.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: 2017-02-17 00:00:00.000000000 Z
11
+ date: 2021-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.7'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.7'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -87,8 +87,11 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
+ - ".github/workflows/gem-push.yml"
91
+ - ".github/workflows/ruby.yml"
90
92
  - ".gitignore"
91
93
  - ".rspec"
94
+ - ".travis.yml"
92
95
  - Gemfile
93
96
  - LICENSE.txt
94
97
  - README.md
@@ -98,15 +101,22 @@ files:
98
101
  - lib/postcodes_io/autocomplete_list.rb
99
102
  - lib/postcodes_io/base.rb
100
103
  - lib/postcodes_io/lookup.rb
104
+ - lib/postcodes_io/lookup_terminated.rb
101
105
  - lib/postcodes_io/postcode.rb
106
+ - lib/postcodes_io/reverse_geocode.rb
102
107
  - lib/postcodes_io/version.rb
103
108
  - postcodes_io.gemspec
104
109
  - spec/autocomplete_spec.rb
105
110
  - spec/fixtures/autocomplete_response.json
106
111
  - spec/fixtures/lookup_multi_response.json
107
112
  - spec/fixtures/lookup_response.json
113
+ - spec/fixtures/lookup_terminated_response.json
114
+ - spec/fixtures/no_results_response.json
115
+ - spec/fixtures/reverse_geocode_response.json
108
116
  - spec/lookup_spec.rb
117
+ - spec/lookup_terminated_spec.rb
109
118
  - spec/postcode_io_spec.rb
119
+ - spec/reverse_geocode_spec.rb
110
120
  - spec/spec_helper.rb
111
121
  homepage: ''
112
122
  licenses:
@@ -127,8 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
137
  - !ruby/object:Gem::Version
128
138
  version: '0'
129
139
  requirements: []
130
- rubyforge_project:
131
- rubygems_version: 2.5.1
140
+ rubygems_version: 3.0.3.1
132
141
  signing_key:
133
142
  specification_version: 4
134
143
  summary: Lookup postcodes
@@ -137,6 +146,11 @@ test_files:
137
146
  - spec/fixtures/autocomplete_response.json
138
147
  - spec/fixtures/lookup_multi_response.json
139
148
  - spec/fixtures/lookup_response.json
149
+ - spec/fixtures/lookup_terminated_response.json
150
+ - spec/fixtures/no_results_response.json
151
+ - spec/fixtures/reverse_geocode_response.json
140
152
  - spec/lookup_spec.rb
153
+ - spec/lookup_terminated_spec.rb
141
154
  - spec/postcode_io_spec.rb
155
+ - spec/reverse_geocode_spec.rb
142
156
  - spec/spec_helper.rb