uk_postcodes_io 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 78f23082bd2711c0813278e5c24016b05c3dadb0
4
+ data.tar.gz: 53c4eb412940649bc0c9f93912eee53cc57efde0
5
+ SHA512:
6
+ metadata.gz: 1b443a042828178b7822cf40a086e83f7d4a1f1c8963d55635f2fd12fd1be8b4a44376a6b6aec668b30698ea0f2146ff835eeee1f832465d724c9fa7ce83cfeb
7
+ data.tar.gz: eca7cc68856deae04a57d79e35b71ce17818947f0afb264d2e62d4d8401d0a288b81b4cf9c33aa23cf3fc56adc99eefaa62fe7d6be5cfd4efbeaa1c259e66294
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uk_postcodes_io.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'minitest-rg'
8
+ gem 'webmock'
9
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Rob Nichols & Marks and Spencer plc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,102 @@
1
+ # UkPostcodesIo
2
+
3
+ This gem uses data gathered from the API at http://postcodes.io
4
+
5
+ It is an alternative to [James Ruston's postcodes_io](https://github.com/jamesruston/postcodes_io).
6
+ _postcode_io_ works well on successful lookups, but does not provide information
7
+ on why a lookup has failed. As I could not see an easy way of extending _postcode_io_
8
+ to provide this information without breaking previous implementations, the best option
9
+ seemed to be to write an alternative.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'uk_postcodes_io'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install uk_postcodes_io
26
+
27
+ ## Usage
28
+
29
+ To look up a postcode:
30
+
31
+ ```ruby
32
+ lookup = UkPostcodesIo.lookup 'SW1A 2AA'
33
+
34
+ lookup.postcode == 'SW1A 2AA'
35
+ lookup.success? == true # depending on success of lookup
36
+ ```
37
+
38
+ The data returned from the _postcode.io_ API is presented as an object at `lookup.data`
39
+ if the lookup is successful. For example, for the postcode of 10 Downing St ('SW1A 2AA'):
40
+
41
+ ```ruby
42
+ lookup.data.longitude == -0.127695242183412
43
+ lookup.data.latitude == 51.5035398826274
44
+ ```
45
+
46
+ See the [_postcode.io_ documentation](http://postcodes.io/docs#Data) for a list of
47
+ attributes available.
48
+
49
+ ### Multiple postcodes
50
+
51
+ Postcodes can be passed to `UkPostcodesIo.multi_lookup` as a list or an array:
52
+
53
+ ```ruby
54
+ lookup = UkPostcodesIo.multi_lookup 'SW1A 2AA', 'NW1 7JS'
55
+ lookup = UkPostcodesIo.multi_lookup ['SW1A 2AA', 'NW1 7JS']
56
+ ```
57
+
58
+ `UkPostcodesIo.multi_lookup`, returns the data in a hash where the keys are
59
+ the postcodes passed in, and the values are the matching data object. So:
60
+
61
+ ```ruby
62
+ lookup.data['SW1A 2AA'].longitude == -0.127695242183412
63
+ lookup.data['SW1A 2AA'].latitude == 51.5035398826274
64
+ ```
65
+
66
+ However, note that if a postcode is not found, the request will be successful,
67
+ but the value matching the problem postcode will be `nil`. So:
68
+
69
+ ```ruby
70
+ lookup.success? == true
71
+ lookup['NW1 7JS'] == nil
72
+ ```
73
+
74
+ ### Failure
75
+
76
+ If the lookup fails:
77
+
78
+ ```ruby
79
+ lookup = UkPostcodesIo.lookup 'invalid'
80
+ lookup.success? == false
81
+ lookup.data == nil
82
+ lookup.error == 'Invalid postcode'
83
+
84
+ no_longer_valid_postcode = 'NW1 7JS'
85
+ lookup = UkPostcodesIo.lookup no_longer_valid_postcode
86
+ lookup.success? == false
87
+ lookup.data == nil
88
+ lookup.error == 'Postcode not found'
89
+ ```
90
+
91
+ ## Development
92
+
93
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/msventurelabs/uk_postcodes_io.
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
102
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "uk_postcodes_io"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,14 @@
1
+ require "uk_postcodes_io/version"
2
+ require "uk_postcodes_io/lookup"
3
+ require "uk_postcodes_io/multi_lookup"
4
+
5
+ module UkPostcodesIo
6
+ def self.lookup(postcode)
7
+ Lookup.new(postcode)
8
+ end
9
+
10
+ def self.multi_lookup(*postcodes)
11
+ postcodes.flatten!
12
+ MultiLookup.new(postcodes)
13
+ end
14
+ end
@@ -0,0 +1,43 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'ostruct'
5
+ module UkPostcodesIo
6
+ class Lookup
7
+ API_URL = 'http://api.postcodes.io/postcodes'
8
+
9
+ attr_reader :postcode
10
+ def initialize(postcode)
11
+ @postcode = postcode
12
+ end
13
+
14
+ def data
15
+ @data ||= OpenStruct.new(body['result']) if body['result']
16
+ end
17
+
18
+ def status
19
+ body['status']
20
+ end
21
+
22
+ def error
23
+ body['error']
24
+ end
25
+
26
+ def success?
27
+ response.kind_of?(Net::HTTPSuccess) && status == 200
28
+ end
29
+
30
+ def body
31
+ @body ||= JSON.parse response.body
32
+ end
33
+
34
+ def response
35
+ @response ||= Net::HTTP.get_response(uri)
36
+ end
37
+
38
+ def uri
39
+ URI(File.join API_URL, URI.escape(postcode))
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,40 @@
1
+ module UkPostcodesIo
2
+ class MultiLookup < Lookup
3
+
4
+ attr_reader :postcodes
5
+ def initialize(postcodes)
6
+ @postcodes = postcodes
7
+ end
8
+
9
+ def data
10
+ @data ||= build_data_from_body_result
11
+ end
12
+
13
+ def build_data_from_body_result
14
+ return unless body['result']
15
+ body['result'].each_with_object({}) do |item, hash|
16
+ hash[item['query']] = item['result'] ? OpenStruct.new(item['result']) : nil
17
+ end
18
+ end
19
+
20
+ def response
21
+ @response ||= make_request
22
+ end
23
+
24
+ def request
25
+ @request ||= Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
26
+ end
27
+
28
+ def make_request
29
+ request.body = { postcodes: postcodes }.to_json
30
+ Net::HTTP.start(uri.hostname, uri.port) do |http|
31
+ http.request(request)
32
+ end
33
+ end
34
+
35
+ def uri
36
+ URI(API_URL)
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module UkPostcodesIo
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'uk_postcodes_io/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "uk_postcodes_io"
8
+ spec.version = UkPostcodesIo::VERSION
9
+ spec.authors = ["Rob Nichols"]
10
+ spec.email = ["rob@nicholshayes.co.uk"]
11
+
12
+ spec.summary = %q{Look up UK postcodes data from http://postcodes.io/.}
13
+ spec.homepage = "https://github.com/msventurelabs/uk_postcodes_io"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uk_postcodes_io
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Nichols
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - rob@nicholshayes.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/uk_postcodes_io.rb
71
+ - lib/uk_postcodes_io/lookup.rb
72
+ - lib/uk_postcodes_io/multi_lookup.rb
73
+ - lib/uk_postcodes_io/version.rb
74
+ - uk_postcodes_io.gemspec
75
+ homepage: https://github.com/msventurelabs/uk_postcodes_io
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.6.11
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Look up UK postcodes data from http://postcodes.io/.
99
+ test_files: []