postcodes_io 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad9cc32017365faafe6c67cf7d18da5aa5010e1b
4
- data.tar.gz: b04650165b7ec3830d90f1fc5eec3ce617273868
3
+ metadata.gz: 196afbb36d87c52f7caeab5b4f071e38c374fb0d
4
+ data.tar.gz: 40c3c8ffd86ad103c4813d9b291b55c07537707b
5
5
  SHA512:
6
- metadata.gz: ec45114d531178260c93decbc083b0b4b7d6a02ae45aee0730c6658d18b3b79d0a1b776387099dd6685e6826e9573d51d019f3d1eedc56aaad4277e424408485
7
- data.tar.gz: 890e6ca0d609aaf1204a6269c6b3426b71e8c2a67d3c06651b40b663cb186238ecacf48f1a1180786f08a1a1cddf168e229b4e076a5dc29fcb947bad7d7c5c8c
6
+ metadata.gz: f54fc9694d50ac7e89e5976d0c8c07c6bda8daf4f47389ce8106d69f55acbb2744f691fe232c940d25220a6e64f32b3257da81c0dbe52cd9f7fae19540c35d66
7
+ data.tar.gz: b3ea796621c10a802e7e846f99f7bef95d2fbbf16663f1050199098a4977f02f43fb498b513a0f2643faa3f6aabe96473f4755626267e97765e298ad870352ff
data/README.md CHANGED
@@ -26,7 +26,7 @@ Require the gem
26
26
  require 'PostcodesIO'
27
27
  ```
28
28
 
29
- Create new instance of the Postcode::IO
29
+ Create new instance of the Postcodes::IO
30
30
 
31
31
  ```ruby
32
32
  pio = Postcodes::IO.new
@@ -4,14 +4,56 @@ require 'postcodes_io/postcode'
4
4
 
5
5
  module Postcodes
6
6
  module Lookup
7
- def lookup(postcode)
8
- postcode.gsub!(/\s+/, '') # remove any whitespace. m1 1ab => m11ab
7
+
8
+
9
+ def lookup(*postcodes)
10
+ postcodes.flatten!
11
+ if postcodes.count > 1
12
+ lookup_multiple postcodes
13
+ else
14
+ lookup_postcode postcodes.first
15
+ end
16
+ end
17
+
18
+
19
+ private
20
+
21
+ def lookup_postcode(postcode)
22
+ postcode = remove_whitespace postcode
9
23
  response = Excon.get("https://api.postcodes.io/postcodes/#{postcode}")
24
+
10
25
  unless response.status == 404
11
26
  parsed_response = JSON.parse(response.body)
12
27
  return Postcodes::Postcode.new(parsed_response['result'])
13
28
  end
14
29
  return nil
15
30
  end
31
+
32
+ def lookup_multiple(postcodes)
33
+ payload = {postcodes: postcodes.map {|p| remove_whitespace p}}
34
+ response = Excon.post(
35
+ "https://api.postcodes.io/postcodes",
36
+ body: payload.to_json,
37
+ headers: {'Content-Type' => 'application/json'}
38
+ )
39
+
40
+ process_response(response) do |r|
41
+ return r['result'].map do |result|
42
+ Postcodes::Postcode.new(result['result'])
43
+ end
44
+ end
45
+ end
46
+
47
+ def remove_whitespace(string)
48
+ string.gsub(/\s+/, '') # remove any whitespace. m1 1ab => m11ab
49
+ end
50
+
51
+ def process_response(response, &block)
52
+ unless response.status == 404
53
+ yield JSON.parse(response.body)
54
+ end
55
+ nil
56
+ end
57
+
16
58
  end
17
59
  end
@@ -9,8 +9,8 @@ module Postcodes
9
9
 
10
10
  # allow accessing info values with dot notation
11
11
  def method_missing(name, *args, &block)
12
+ return @info[name.to_s] if @info.key? name.to_s
12
13
  return @info[name] if @info.key? name
13
- @info.each { |k,v| return v if k.to_s.to_sym == name }
14
14
  super.method_missing name
15
15
  end
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module Postcodes
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,86 @@
1
+ {
2
+ "status": 200,
3
+ "result": [
4
+ {
5
+ "query": "OX49 5NU",
6
+ "result": {
7
+ "postcode": "OX49 5NU",
8
+ "quality": 1,
9
+ "eastings": 464435,
10
+ "northings": 195686,
11
+ "country": "England",
12
+ "nhs_ha": "South Central",
13
+ "admin_county": "Oxfordshire",
14
+ "admin_district": "South Oxfordshire",
15
+ "admin_ward": "Benson",
16
+ "longitude": -1.06993881320412,
17
+ "latitude": 51.6562791294687,
18
+ "parliamentary_constituency": "Henley",
19
+ "european_electoral_region": "South East",
20
+ "primary_care_trust": "Oxfordshire",
21
+ "region": "South East",
22
+ "parish": "Brightwell Baldwin",
23
+ "lsoa": "South Oxfordshire 011B",
24
+ "msoa": "South Oxfordshire 011",
25
+ "nuts": "Benson",
26
+ "incode": "5NU",
27
+ "outcode": "OX49",
28
+ "ccg": "NHS Oxfordshire CCG"
29
+ }
30
+ },
31
+ {
32
+ "query": "M32 0JG",
33
+ "result": {
34
+ "postcode": "M32 0JG",
35
+ "quality": 1,
36
+ "eastings": 379988,
37
+ "northings": 395476,
38
+ "country": "England",
39
+ "nhs_ha": "North West",
40
+ "admin_county": null,
41
+ "admin_district": "Trafford",
42
+ "admin_ward": "Gorse Hill",
43
+ "longitude": -2.30283674284007,
44
+ "latitude": 53.4556572899372,
45
+ "parliamentary_constituency": "Stretford and Urmston",
46
+ "european_electoral_region": "North West",
47
+ "primary_care_trust": "Trafford",
48
+ "region": "North West",
49
+ "parish": null,
50
+ "lsoa": "Trafford 003C",
51
+ "msoa": "Trafford 003",
52
+ "nuts": "Gorse Hill",
53
+ "incode": "0JG",
54
+ "outcode": "M32",
55
+ "ccg": "NHS Trafford CCG"
56
+ }
57
+ },
58
+ {
59
+ "query": "NE30 1DP",
60
+ "result": {
61
+ "postcode": "NE30 1DP",
62
+ "quality": 1,
63
+ "eastings": 435982,
64
+ "northings": 568683,
65
+ "country": "England",
66
+ "nhs_ha": "North East",
67
+ "admin_county": null,
68
+ "admin_district": "North Tyneside",
69
+ "admin_ward": "Tynemouth",
70
+ "longitude": -1.43889223951028,
71
+ "latitude": 55.0114112900573,
72
+ "parliamentary_constituency": "Tynemouth",
73
+ "european_electoral_region": "North East",
74
+ "primary_care_trust": "North Tyneside",
75
+ "region": "North East",
76
+ "parish": null,
77
+ "lsoa": "North Tyneside 016C",
78
+ "msoa": "North Tyneside 016",
79
+ "nuts": "Tynemouth",
80
+ "incode": "1DP",
81
+ "outcode": "NE30",
82
+ "ccg": "NHS North Tyneside CCG"
83
+ }
84
+ }
85
+ ]
86
+ }
@@ -3,14 +3,16 @@ require 'spec_helper'
3
3
  describe Postcodes::IO do
4
4
 
5
5
  let(:base_url) {'https://api.postcodes.io'}
6
- let(:stub_response) { File.read('spec/fixtures/lookup_response.json') }
7
-
8
- before :each do
9
- stub_request(:get, "#{base_url}/postcodes/CM129TR")
10
- .to_return(status: 200, body: stub_response)
11
- end
6
+ let(:stub_single_response) { File.read('spec/fixtures/lookup_response.json') }
7
+ let(:stub_multi_response) { File.read('spec/fixtures/lookup_multi_response.json') }
12
8
 
13
9
  describe '#lookup' do
10
+
11
+ before :each do
12
+ stub_request(:get, "#{base_url}/postcodes/CM129TR")
13
+ .to_return(status: 200, body: stub_single_response)
14
+ end
15
+
14
16
  let(:input) { 'CM12 9TR' }
15
17
  let(:output) { subject.lookup(input) }
16
18
 
@@ -22,6 +24,39 @@ describe Postcodes::IO do
22
24
  it 'returns the long and lat' do
23
25
  output.longitude.should == 0.411227930206834
24
26
  end
27
+ end
28
+
29
+ describe '#lookup with multiple postcodes' do
30
+ before :each do
31
+ stub_request(:post, "#{base_url}/postcodes")
32
+ .with(body: postcode_payload, headers: {'Content-Type' => 'application/json'})
33
+ .to_return(status: 200, body: stub_multi_response)
34
+ end
25
35
 
36
+ let(:input) { ['OX495NU', 'M320JG', 'NE301DP'] }
37
+ let(:output) { subject.lookup(input) }
38
+ let(:postcode_payload) { {postcodes: input}.to_json }
39
+
40
+ it 'makes a request with mutliple postcodes' do
41
+ output
42
+ WebMock.should have_requested(:post, "#{base_url}/postcodes")
43
+ end
44
+
45
+ it 'returns a postcode object for each result' do
46
+ output.count.should == 3
47
+ output.each do |o|
48
+ o.class.should == Postcodes::Postcode
49
+ end
50
+ end
51
+
52
+ describe 'as splat' do
53
+ let(:output) { subject.lookup('OX495NU', 'M320JG', 'NE301DP') }
54
+
55
+ it 'allows passing postcodes as multiple arguments' do
56
+ output
57
+ WebMock.should have_requested(:post, "#{base_url}/postcodes")
58
+ end
59
+
60
+ end
26
61
  end
27
62
  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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Ruston
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-20 00:00:00.000000000 Z
11
+ date: 2014-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,6 +98,7 @@ files:
98
98
  - lib/postcodes_io/postcode.rb
99
99
  - lib/postcodes_io/version.rb
100
100
  - postcodes_io.gemspec
101
+ - spec/fixtures/lookup_multi_response.json
101
102
  - spec/fixtures/lookup_response.json
102
103
  - spec/postcode_io_spec.rb
103
104
  - spec/spec_helper.rb
@@ -126,6 +127,7 @@ signing_key:
126
127
  specification_version: 4
127
128
  summary: Lookup postcodes
128
129
  test_files:
130
+ - spec/fixtures/lookup_multi_response.json
129
131
  - spec/fixtures/lookup_response.json
130
132
  - spec/postcode_io_spec.rb
131
133
  - spec/spec_helper.rb