ea-address_lookup 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +21 -0
- data/.gitignore +39 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/.simplecov +9 -0
- data/.travis.yml +47 -0
- data/Gemfile +8 -0
- data/LICENSE +8 -0
- data/README.md +93 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ea-address_lookup.gemspec +35 -0
- data/lib/ea/address_lookup.rb +15 -0
- data/lib/ea/address_lookup/adapters.rb +32 -0
- data/lib/ea/address_lookup/adapters/address_facade.rb +132 -0
- data/lib/ea/address_lookup/adapters/locate_api.rb +25 -0
- data/lib/ea/address_lookup/configuration.rb +28 -0
- data/lib/ea/address_lookup/errors.rb +15 -0
- data/lib/ea/address_lookup/finders.rb +15 -0
- data/lib/ea/address_lookup/logger.rb +20 -0
- data/lib/ea/address_lookup/test_helper/address_lookup.yml +64 -0
- data/lib/ea/address_lookup/test_helper/mock_data.rb +32 -0
- data/lib/ea/address_lookup/test_helper/rspec_mocks.rb +41 -0
- data/lib/ea/address_lookup/version.rb +5 -0
- metadata +237 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b57bfc1eb09fcdd170481081f7c7c69cf9888564
|
4
|
+
data.tar.gz: e9805598175f36cc5d3454791932167ac6168579
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 51a51be32e5af468c845fd839943324150ad69987d2d3d7a5f93f27d5ac89aa433f01afe7fd739c3536e550366e152f38059289fa2e018ea288974e599f1299c
|
7
|
+
data.tar.gz: ae2d01648bca7f2467b83a15634605741ba3b1febcfb33be823f6025059f6d765dd6d4bffa67f4325e4f601c0f6af8927db8266109dbf5e6e7db3b89f57a7bb8
|
data/.codeclimate.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
---
|
2
|
+
engines:
|
3
|
+
duplication:
|
4
|
+
enabled: true
|
5
|
+
config:
|
6
|
+
languages:
|
7
|
+
- ruby
|
8
|
+
fixme:
|
9
|
+
enabled: true
|
10
|
+
rubocop:
|
11
|
+
enabled: true
|
12
|
+
brakeman:
|
13
|
+
enabled: false
|
14
|
+
rubymotion:
|
15
|
+
enabled: true
|
16
|
+
ratings:
|
17
|
+
paths:
|
18
|
+
- "**.module"
|
19
|
+
- "**.rb"
|
20
|
+
exclude_paths:
|
21
|
+
- spec/
|
data/.gitignore
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/spec/fixtures/results/*
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
# Ignore ruby_mine folder
|
13
|
+
/.idea
|
14
|
+
|
15
|
+
*~
|
16
|
+
.DS_Store
|
17
|
+
.svn
|
18
|
+
.*.swp
|
19
|
+
___*
|
20
|
+
|
21
|
+
# Ignore bundler config.
|
22
|
+
/.bundle
|
23
|
+
|
24
|
+
# Ignore all logfiles and tempfiles.
|
25
|
+
/log/*
|
26
|
+
!/log/.keep
|
27
|
+
|
28
|
+
# Overcommit configuration files (added by before_commit)
|
29
|
+
.overcommit*
|
30
|
+
.git-hooks/
|
31
|
+
.rubocop.yml
|
32
|
+
.htmlhintrc
|
33
|
+
|
34
|
+
# Ignore developer specific .rspec files
|
35
|
+
.rspec-local
|
36
|
+
|
37
|
+
./ea-address_lookup*.gem
|
38
|
+
|
39
|
+
.byebug_history
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/.simplecov
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
SimpleCov.start "rails" do
|
2
|
+
# any custom configs like groups and filters can be here at a central place
|
3
|
+
|
4
|
+
# The enrollment builder controller is just used to create dummy records when
|
5
|
+
# working in development as a time saver. We are happy there is no test coverage
|
6
|
+
# for it.
|
7
|
+
add_filter "lib/ea/address_lookup/version.rb"
|
8
|
+
add_filter "lib/ea/address_lookup/adapters/locate_api.rb" # demo class
|
9
|
+
end
|
data/.travis.yml
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
|
4
|
+
rvm:
|
5
|
+
- 2.3.1
|
6
|
+
|
7
|
+
before_install:
|
8
|
+
- gem install bundler -v 1.11.2
|
9
|
+
|
10
|
+
bundler_args: --binstubs=./bundler_stubs
|
11
|
+
|
12
|
+
before_script:
|
13
|
+
|
14
|
+
script:
|
15
|
+
- bundle exec rake --trace before_commit:run_without_checks
|
16
|
+
- git config --global user.email test@example.com # overcommit Author
|
17
|
+
- git config --global user.name "Test Example" # overcommit Author
|
18
|
+
- BUNDLE_GEMFILE=.overcommit_gems.rb bundle exec overcommit --run
|
19
|
+
- bundle exec rspec
|
20
|
+
|
21
|
+
# Have this option to stop travis-ci building twice. Currently we have travis set to build both
|
22
|
+
# PR's and pushes. However this means when we push to a PR we have to wait for Travis to finish
|
23
|
+
# 2 builds. If we unticked 'pushes' when the PR was finally merged that would not be built. The
|
24
|
+
# brute force approach would be to untick build PR's and just build all pushes. We instead have
|
25
|
+
# gone with the approach outlined here http://stackoverflow.com/a/31882307
|
26
|
+
branches:
|
27
|
+
only:
|
28
|
+
- master
|
29
|
+
|
30
|
+
# This section was added as per https://docs.travis-ci.com/user/code-climate/
|
31
|
+
# To protect our codeclimate stats rather than adding the Codeclimate API key for ea-area_lookup
|
32
|
+
# in the open we used this guide https://docs.travis-ci.com/user/encryption-keys/ to encryt the
|
33
|
+
# value. Essentially install travis gem, then run `travis encrypt <my_code_climate_api_key>`
|
34
|
+
addons:
|
35
|
+
code_climate:
|
36
|
+
repo_token:
|
37
|
+
secure: "NHoT4H5wYUhdGoByjcnViaLzHFkxKgSgZPCNtWtaCdrWXUwX8VLYYl6NPqvANMII0z81Fq8P0+Nzo0o0YfWAvXsimsuhXd4hdzMSKW+xp6G5KRmCaU8FfIjqGhf0yYs3jmTD6Bz2rv1uhZ080k39OXBE/pGR3pkd229lVxrVR91zYYnERqNGD7p78NDGCTPlyoYYNB+wMLq76p6Hl2dSZgwMQtDS/Qi95vG+3Zq2y3z+iarlXSxm2xqfYMgUTcoO3w1eL2P/I+y/SqKgqKEpNKTzicFySUUg5NEvQy/GQit6iArkemU3q364fp4RqkUzauDenEsE64Q6MNY/n7Pj+ZVUKvT3mFKr2OAQojrJko2vtM7QuxBKmaRvkA89JRJvXDTREqbB1gY9pmnAbxBQbRW25KRSucKRPIGhK+gfwjM01eINveR86KECyTOLkrVcWFihrXzrIxLzv37UQUh9UVE/+aca0QVivYa6jJOebPKeLxS3sPLco8clv4A1H7HfvAkzg9lzWC6cz/cSOmQWWvAcDsd+riMaq188fCGuPhHcGywzwH4qt/75b8vnOoOSZSxqNAvQfsf0np0V+N2ahiGqpBhTgPKD3B+Mhff2KN0WY0eejzUMdPdi6zNcjJySyvLBZ9Jrmb+Vf/X1DhEqg05n/V2lQCWzoeJw4zSyRyU="
|
38
|
+
|
39
|
+
deploy:
|
40
|
+
provider: rubygems
|
41
|
+
api_key:
|
42
|
+
secure: "HPERtzREWDTHbl0EKL9E6J5SKEdKhcbvhgAEgDJ7h0TERiPmmhzXEMN8wFUz3xFx4JVn+cNYdnqmRaJ6KJQKUsC7aNwKkwBM220/xIUYksBzbcqQ7YODSnkLknc7uDNXWlny+oqY/KWqdcfsHr+CJImbLg0XIZEm4EH28f6vQctfKyjov2IhML8JFcnzWvjP1NHw03MZf1CDlDdh84hl0gv+lPe6lWtvKZarRqJIaP7Tzq+x/PcO0vdZfe37gmKNxxswPDmLbjPmjJEts5qO+9QcidBi/lXrTxGuXBGzXxcOmqlsr7SZVyPWAHOs9Lc/eB/iNJBQ5X5JSS2FgSmNrvh8w9WcNNZN7Ng398zEsqJTDVmPjCmkYMxvcpoB/6XBiPjmhji3kWqHicP0u1FkWFlTlaZk0wCjoPAuDiAJI8V0fzEc/73S3t+sP8igiNgw8qq2JegvRaVAvYdbmovWASaMb8x64BLpEGv5VVIe+ML5w99Y33mWPO8bOz99ZBFNG5pDJerM6mHB+hrVh+YC7UAb0yS/yn90Id02fQlt6Uxn/rSKuuRX096ha1oexYUaIKzjOGD0ReuKs33JpoqzPPjz7mu0+ncQClZtQyEBPgFrS5AqIauSxMzC+UsRrZ5M8cz9dZ3QrxHuhWoDLjVtAIqJq0eE0LbJaknvwphvtSA="
|
43
|
+
gem: ea-address_lookup
|
44
|
+
on:
|
45
|
+
tags: true
|
46
|
+
repo: EnvironmentAgency/ea-address_lookup
|
47
|
+
all_branches: true
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
The Open Government Licence (OGL) Version 3
|
2
|
+
|
3
|
+
Copyright (c) 2014 Environment Agency
|
4
|
+
|
5
|
+
This source code is licensed under the Open Government Licence v3.0. To view this
|
6
|
+
licence, visit www.nationalarchives.gov.uk/doc/open-government-licence/version/3
|
7
|
+
or write to the Information Policy Team, The National Archives, Kew, Richmond,
|
8
|
+
Surrey, TW9 4DU.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# EA::AddressLookup
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/EnvironmentAgency/ea-address_lookup.svg?branch=master)](https://travis-ci.org/EnvironmentAgency/ea-address_lookup)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/EnvironmentAgency/ea-address_lookup/badges/gpa.svg)](https://codeclimate.com/github/EnvironmentAgency/ea-address_lookup)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/EnvironmentAgency/ea-address_lookup/badges/coverage.svg)](https://codeclimate.com/github/EnvironmentAgency/ea-address_lookup/coverage)
|
6
|
+
|
7
|
+
This ruby gem provides address lookup functionality by postcode.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add the gem to your Gemfile
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'ea-address_lookup'
|
15
|
+
```
|
16
|
+
|
17
|
+
Then execute:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Rails
|
26
|
+
|
27
|
+
Create an intializer eg `config/initializers/address_lookup.rb`
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
EA::AddressLookup.configure do |config|
|
31
|
+
config.address_facade_server = <some_host_name>
|
32
|
+
config.address_facade_port = ""
|
33
|
+
config.address_facade_url = "/address-service/v1/addresses/""
|
34
|
+
config.address_facade_client_id = <client_id>
|
35
|
+
config.address_facade_key = <key>
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Now you can do the following:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
EA::AddressLookup.adapter = :address_facade # optional as its the default
|
43
|
+
hash = EA::AddressLookup.find_by_postcode('BA1 5AH')
|
44
|
+
hash = EA::AddressLookup.find_by_uprn('12345678')
|
45
|
+
```
|
46
|
+
|
47
|
+
## Testing with RSpec
|
48
|
+
A test helper is included that provides methods that will stub calls to
|
49
|
+
EA::AddressLookup methods in RSpec tests. To enable them add this to
|
50
|
+
the rspec configuration (for example, within a `RSpec.configure do |config|`
|
51
|
+
block in a Rails app's `spec/rails_helper.rb`):
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
config.include EA::AddressLookup::TestHelper::RspecMocks
|
55
|
+
```
|
56
|
+
|
57
|
+
This will make the methods defined in `lib/ea/address_lookup/test_helper/rspec_mocks.rb`
|
58
|
+
available within the host app's rspec tests. For example:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
describe "postcode lookup" do
|
62
|
+
before do
|
63
|
+
mock_ea_address_lookup_find_by_postcode
|
64
|
+
end
|
65
|
+
|
66
|
+
it "some tests that use data returned by a postcode lookup" do
|
67
|
+
....
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
|
73
|
+
## Contributing to this project
|
74
|
+
|
75
|
+
If you have an idea you'd like to contribute please log an issue.
|
76
|
+
|
77
|
+
All contributions should be submitted via a pull request.
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:
|
82
|
+
|
83
|
+
http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3
|
84
|
+
|
85
|
+
The following attribution statement MUST be cited in your products and applications when using this information.
|
86
|
+
|
87
|
+
>Contains public sector information licensed under the Open Government license v3
|
88
|
+
|
89
|
+
### About the license
|
90
|
+
|
91
|
+
The Open Government Licence (OGL) was developed by the Controller of Her Majesty's Stationery Office (HMSO) to enable information providers in the public sector to license the use and re-use of their information under a common open licence.
|
92
|
+
|
93
|
+
It is designed to encourage use and re-use of information freely and flexibly, with only a few conditions.
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
require 'before_commit'
|
7
|
+
spec = Gem::Specification.find_by_name 'before_commit'
|
8
|
+
load "#{spec.gem_dir}/lib/tasks/before_commit.rake"
|
9
|
+
|
10
|
+
task default: :spec
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "whereabouts"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "ea/address_lookup/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ea-address_lookup"
|
8
|
+
spec.version = EA::AddressLookup::VERSION
|
9
|
+
spec.authors = ["Digital Services Team, EnvironmentAgency"]
|
10
|
+
spec.email = ["dst@environment-agency.gov.uk"]
|
11
|
+
|
12
|
+
spec.summary = "Address lookup by postcode"
|
13
|
+
spec.description = "Address lookup by postcode. Wraps calls to osplaces an api."
|
14
|
+
spec.homepage = "https://github.com/EnvironmentAgency"
|
15
|
+
spec.license = "The Open Government Licence (OGL) Version 3"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "activesupport", ">= 4.2"
|
23
|
+
spec.add_dependency "rest-client", "~> 2.0.0.rc2"
|
24
|
+
spec.add_dependency "nesty", "~> 1.0"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "vcr", "~> 3.0"
|
30
|
+
spec.add_development_dependency "webmock", "~> 1.24"
|
31
|
+
spec.add_development_dependency "shoulda-matchers", "~> 3.1"
|
32
|
+
spec.add_development_dependency "simplecov"
|
33
|
+
spec.add_development_dependency "byebug"
|
34
|
+
spec.add_development_dependency "before_commit"
|
35
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "active_support/core_ext/string"
|
2
|
+
require "ea/address_lookup/version"
|
3
|
+
require "ea/address_lookup/configuration"
|
4
|
+
require "ea/address_lookup/logger"
|
5
|
+
require "ea/address_lookup/errors"
|
6
|
+
require "ea/address_lookup/adapters"
|
7
|
+
require "ea/address_lookup/finders"
|
8
|
+
require "ea/address_lookup/test_helper/rspec_mocks"
|
9
|
+
|
10
|
+
module EA
|
11
|
+
module AddressLookup
|
12
|
+
extend Adapters
|
13
|
+
extend Finders
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# The specific wrapper class for the address lookup API
|
2
|
+
# is wired up using the Adapter patter. This allows us to present a common
|
3
|
+
# interface to potentially varying address api interfaces.
|
4
|
+
require "ea/address_lookup/adapters/address_facade"
|
5
|
+
|
6
|
+
module EA
|
7
|
+
module AddressLookup
|
8
|
+
module Adapters
|
9
|
+
def adapter
|
10
|
+
@adapter ||= create_adapter(EA::AddressLookup.config.default_adapter)
|
11
|
+
end
|
12
|
+
|
13
|
+
def adapter=(adapter_name)
|
14
|
+
@adapter = create_adapter(adapter_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
# Given an adpater_nam of e.g. :address_facade, return an instance of
|
20
|
+
# EA::AddressLookup::AddressLookup::Adapters::AddressFacade
|
21
|
+
def create_adapter(adapter_name)
|
22
|
+
raise MissingAdapterError if adapter_name.blank?
|
23
|
+
|
24
|
+
adapter_klass = adapter_name.to_s.classify.to_s
|
25
|
+
Adapters.const_get(adapter_klass).new
|
26
|
+
|
27
|
+
rescue NameError
|
28
|
+
raise UnrecognisedAdapterError, adapter_name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# Adaptor for EA AddressFacade :
|
2
|
+
#
|
3
|
+
# Relies on the following configuration options:
|
4
|
+
#
|
5
|
+
# :address_facade_server
|
6
|
+
# :address_facade_port
|
7
|
+
# :address_facade_url
|
8
|
+
# :address_facade_client_id
|
9
|
+
# :address_facade_key
|
10
|
+
#
|
11
|
+
# Testing The Service
|
12
|
+
#
|
13
|
+
# The service is a simple rest service and can be tested using any browser
|
14
|
+
# or the curl command from a Unix command line:
|
15
|
+
#
|
16
|
+
# ## Match By String
|
17
|
+
#
|
18
|
+
# N.B '\ char is ruby string continue - not part of the URL
|
19
|
+
|
20
|
+
# curl http://servername:port/address-service/v1/addresses/find?\
|
21
|
+
# client-id=example%20team&201&key=client1&query-string=buckingham%20palace
|
22
|
+
#
|
23
|
+
# ## Search Post Code
|
24
|
+
#
|
25
|
+
# curl http://servername:port/address-service/v1/addresses/postcode?\
|
26
|
+
# client-id=example%20team&201&key=key1&postcode=bs1%205ah
|
27
|
+
#
|
28
|
+
# It also comes with a swagger interface, which can also visit via your locally
|
29
|
+
# running server @ http://localhost:9002/swagger
|
30
|
+
#
|
31
|
+
require "rest_client"
|
32
|
+
require "benchmark"
|
33
|
+
|
34
|
+
module EA
|
35
|
+
module AddressLookup
|
36
|
+
module Adapters
|
37
|
+
class AddressFacade
|
38
|
+
attr_reader :base_url
|
39
|
+
|
40
|
+
def reset
|
41
|
+
@base_url = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def base_url
|
45
|
+
@base_url ||= begin
|
46
|
+
server = config.address_facade_server
|
47
|
+
port = config.address_facade_port
|
48
|
+
url = config.address_facade_url
|
49
|
+
host = "http://#{server}:#{port}"
|
50
|
+
URI.join(host, url || "").to_s
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def find_by_uprn(uprn)
|
55
|
+
with_logging(:find_by_uprn, uprn) do
|
56
|
+
result = http_get(uprn)
|
57
|
+
parse_json(result)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def find_by_postcode(post_code)
|
62
|
+
with_logging(:find_by_postcode, post_code) do
|
63
|
+
result = http_get("postcode", postcode: post_code)
|
64
|
+
parse_json(result)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def default_query_params
|
71
|
+
{
|
72
|
+
'client-id': config.address_facade_client_id,
|
73
|
+
'key': config.address_facade_key
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
def config
|
78
|
+
EA::AddressLookup.config
|
79
|
+
end
|
80
|
+
|
81
|
+
def logger
|
82
|
+
EA::AddressLookup.logger
|
83
|
+
end
|
84
|
+
|
85
|
+
# The AddressBaseFacade is internal within AWS, so we need to
|
86
|
+
# ensure that we DO NOT use a proxy in http calls.
|
87
|
+
def http_get(path, query_params = {})
|
88
|
+
http_address = URI.join(base_url, path).to_s
|
89
|
+
args = build_request_args(http_address, query_params)
|
90
|
+
RestClient::Request.execute(args)
|
91
|
+
rescue => ex
|
92
|
+
raise ex if ex.class.to_s =~ /^VCR/
|
93
|
+
raise EA::AddressLookup::AddressServiceUnavailableError,
|
94
|
+
"#{http_address} "\
|
95
|
+
"params:#{default_query_params.merge(query_params)} - "\
|
96
|
+
"#{ex.message}"
|
97
|
+
end
|
98
|
+
|
99
|
+
def build_request_args(http_address, query_params)
|
100
|
+
{
|
101
|
+
method: :get,
|
102
|
+
url: http_address,
|
103
|
+
proxy: false,
|
104
|
+
timeout: config.timeout_in_seconds,
|
105
|
+
headers: {
|
106
|
+
params: default_query_params.merge(query_params)
|
107
|
+
}
|
108
|
+
}
|
109
|
+
end
|
110
|
+
|
111
|
+
def parse_json(json)
|
112
|
+
JSON.parse(json)
|
113
|
+
rescue => e
|
114
|
+
logger.error("Failed to parse JSON results "\
|
115
|
+
"#{e.message} #{json}")
|
116
|
+
{}
|
117
|
+
end
|
118
|
+
|
119
|
+
def with_logging(scope, arg)
|
120
|
+
parsed_result = nil
|
121
|
+
time = Benchmark.realtime do
|
122
|
+
parsed_result = yield
|
123
|
+
end
|
124
|
+
logger.info "#{scope}(#{arg}) took "\
|
125
|
+
"#{sprintf('%05.2fms', time * 1000)}"
|
126
|
+
logger.debug "#{scope}(#{arg}) #{parsed_result}"
|
127
|
+
parsed_result
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#
|
2
|
+
# Example of how me might define an Adaptor for
|
3
|
+
# https://github.com/alphagov/locate-api
|
4
|
+
#
|
5
|
+
module EA
|
6
|
+
module AddressLookup
|
7
|
+
module Adapters
|
8
|
+
module LocateApi
|
9
|
+
module_function
|
10
|
+
|
11
|
+
def url
|
12
|
+
Rails.configuration.dcs_locate_api_url
|
13
|
+
end
|
14
|
+
|
15
|
+
def find_by_postcode(post_code)
|
16
|
+
result = RestClient::Request.execute(method: :get,
|
17
|
+
url: url,
|
18
|
+
proxy: false,
|
19
|
+
params: { postcode: post_code })
|
20
|
+
JSON.parse(result)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "active_support/configurable"
|
2
|
+
|
3
|
+
module EA
|
4
|
+
module AddressLookup
|
5
|
+
class Configuration
|
6
|
+
include ActiveSupport::Configurable
|
7
|
+
config_accessor(:address_facade_server)
|
8
|
+
config_accessor(:address_facade_port)
|
9
|
+
config_accessor(:address_facade_url)
|
10
|
+
config_accessor(:address_facade_client_id)
|
11
|
+
config_accessor(:address_facade_key)
|
12
|
+
config_accessor(:timeout_in_seconds) { 5 }
|
13
|
+
config_accessor(:default_adapter) { :address_facade }
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.config
|
17
|
+
@config ||= Configuration.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.configure
|
21
|
+
yield config
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.reset
|
25
|
+
@config = Configuration.new
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "nesty"
|
2
|
+
|
3
|
+
module EA
|
4
|
+
module AddressLookup
|
5
|
+
class MissingAdapterError < StandardError
|
6
|
+
include Nesty::NestedError
|
7
|
+
end
|
8
|
+
class UnrecognisedAdapterError < StandardError
|
9
|
+
include Nesty::NestedError
|
10
|
+
end
|
11
|
+
class AddressServiceUnavailableError < StandardError
|
12
|
+
include Nesty::NestedError
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "ea/address_lookup/adapters/address_facade"
|
2
|
+
|
3
|
+
module EA
|
4
|
+
module AddressLookup
|
5
|
+
module Finders
|
6
|
+
def find_by_postcode(post_code)
|
7
|
+
adapter.find_by_postcode(post_code)
|
8
|
+
end
|
9
|
+
|
10
|
+
def find_by_uprn(uprn)
|
11
|
+
adapter.find_by_uprn(uprn)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# A consumer can hook into logging like so:
|
2
|
+
# EA::AddressLookup.logger = Rails.logger
|
3
|
+
# or to silence logging unless there are errors for example:
|
4
|
+
# EA::AddressLookup.logger.level = Logger::ERROR
|
5
|
+
require "logger"
|
6
|
+
|
7
|
+
module EA
|
8
|
+
module AddressLookup
|
9
|
+
class << self
|
10
|
+
attr_writer :logger
|
11
|
+
|
12
|
+
def logger
|
13
|
+
@logger ||= Logger.new($stdout).tap do |log|
|
14
|
+
log.progname = name
|
15
|
+
log.level = Logger::DEBUG
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
---
|
2
|
+
uprn_lookup:
|
3
|
+
totalMatches: 1
|
4
|
+
startMatch: 1
|
5
|
+
endMatch: 1
|
6
|
+
uri_to_supplier: https://api.ordnancesurvey.co.uk/places/v1/addresses/uprn?uprn=10010175140&lr=EN&dataset=DPA
|
7
|
+
uri_from_client: stub
|
8
|
+
results:
|
9
|
+
- uprn: 340116
|
10
|
+
address: ENVIRONMENT AGENCY, HORIZON HOUSE, DEANERY ROAD, BRISTOL, BS1 5AH
|
11
|
+
organisation: ENVIRONMENT AGENCY
|
12
|
+
premises: HORIZON HOUSE
|
13
|
+
street_address: DEANERY ROAD
|
14
|
+
locality: SOMEWHERE
|
15
|
+
city: BRISTOL
|
16
|
+
postcode: BS1 5AH
|
17
|
+
x: 358205.03
|
18
|
+
y: 172708.07
|
19
|
+
coordinate_system: ""
|
20
|
+
state_date: 12/10/2009
|
21
|
+
blpu_state_code: ""
|
22
|
+
postal_address_code: ""
|
23
|
+
logical_status_code: ""
|
24
|
+
source_data_type: dpa
|
25
|
+
|
26
|
+
postcode_lookup:
|
27
|
+
totalMatches: 2
|
28
|
+
startMatch: 1
|
29
|
+
endMatch: 2
|
30
|
+
uri_to_supplier: "https://api.ordnancesurvey.co.uk/places/v1/addresses/postcode?lr=EN&postcode=BS1%205AH&maxresults=100&dataset=DPA"
|
31
|
+
uri_from_client: stub
|
32
|
+
results:
|
33
|
+
- uprn: 340116
|
34
|
+
address: ENVIRONMENT AGENCY, HORIZON HOUSE, DEANERY ROAD, BRISTOL, BS1 5AH
|
35
|
+
organisation: ENVIRONMENT AGENCY
|
36
|
+
premises: HORIZON HOUSE
|
37
|
+
street_address: DEANERY ROAD
|
38
|
+
locality: ""
|
39
|
+
city: BRISTOL
|
40
|
+
postcode: BS1 5AH
|
41
|
+
x: 358205.03
|
42
|
+
y: 172708.07
|
43
|
+
coordinate_system: ""
|
44
|
+
state_date: 12/10/2009
|
45
|
+
blpu_state_code: ""
|
46
|
+
postal_address_code: ""
|
47
|
+
logical_status_code: ""
|
48
|
+
source_data_type: dpa
|
49
|
+
- uprn: 10091760640
|
50
|
+
address: HARMSEN GROUP, TRIODOS BANK, DEANERY ROAD, BRISTOL, BS1 5AH
|
51
|
+
organisation: HARMSEN GROUP
|
52
|
+
premises: TRIODOS BANK
|
53
|
+
street_address: DEANERY ROAD
|
54
|
+
locality: ""
|
55
|
+
city: BRISTOL
|
56
|
+
postcode: BS1 5AH
|
57
|
+
x: 358130.1
|
58
|
+
y: 172687.87
|
59
|
+
coordinate_system: ""
|
60
|
+
state_date: ""
|
61
|
+
blpu_state_code: ""
|
62
|
+
postal_address_code: ""
|
63
|
+
logical_status_code: ""
|
64
|
+
source_data_type: dpa
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module EA
|
2
|
+
module AddressLookup
|
3
|
+
module TestHelper
|
4
|
+
class MockData
|
5
|
+
|
6
|
+
def initialize(file_name)
|
7
|
+
@file_name = file_name.to_s.strip
|
8
|
+
end
|
9
|
+
|
10
|
+
def data_for(key)
|
11
|
+
yaml[key.to_s]
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def file_name
|
17
|
+
return @file_name if @file_name =~ /\.yml$/
|
18
|
+
"#{@file_name}.yml"
|
19
|
+
end
|
20
|
+
|
21
|
+
def path
|
22
|
+
File.expand_path file_name, File.dirname(__FILE__)
|
23
|
+
end
|
24
|
+
|
25
|
+
def yaml
|
26
|
+
@yaml ||= YAML.load_file path
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative "mock_data"
|
2
|
+
module EA
|
3
|
+
module AddressLookup
|
4
|
+
module TestHelper
|
5
|
+
# Uses data from address_lookup.yml to mock calls to EA::AddressLookup methods
|
6
|
+
module RspecMocks
|
7
|
+
|
8
|
+
def mock_ea_address_lookup_find_by_uprn
|
9
|
+
allow(EA::AddressLookup)
|
10
|
+
.to receive(:find_by_uprn)
|
11
|
+
.and_return(mock_data.data_for(:uprn_lookup))
|
12
|
+
end
|
13
|
+
|
14
|
+
def mock_failure_of_ea_address_lookup_find_by_uprn
|
15
|
+
allow(EA::AddressLookup)
|
16
|
+
.to receive(:find_by_uprn)
|
17
|
+
.and_raise(AddressServiceUnavailableError, "Whoops")
|
18
|
+
end
|
19
|
+
|
20
|
+
def mock_ea_address_lookup_find_by_postcode
|
21
|
+
allow(EA::AddressLookup)
|
22
|
+
.to receive(:find_by_postcode)
|
23
|
+
.and_return(mock_data.data_for(:postcode_lookup))
|
24
|
+
end
|
25
|
+
|
26
|
+
def mock_failure_of_ea_address_lookup_find_by_postcode
|
27
|
+
allow(EA::AddressLookup)
|
28
|
+
.to receive(:find_by_postcode)
|
29
|
+
.and_raise(AddressServiceUnavailableError, "Whoops")
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def mock_data
|
35
|
+
@mock_data ||= MockData.new :address_lookup
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,237 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ea-address_lookup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Digital Services Team, EnvironmentAgency
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rest-client
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.0.rc2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.0.rc2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: nesty
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.11'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: vcr
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: webmock
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.24'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.24'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: shoulda-matchers
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '3.1'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '3.1'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: simplecov
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: byebug
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: before_commit
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
description: Address lookup by postcode. Wraps calls to osplaces an api.
|
182
|
+
email:
|
183
|
+
- dst@environment-agency.gov.uk
|
184
|
+
executables: []
|
185
|
+
extensions: []
|
186
|
+
extra_rdoc_files: []
|
187
|
+
files:
|
188
|
+
- ".codeclimate.yml"
|
189
|
+
- ".gitignore"
|
190
|
+
- ".rspec"
|
191
|
+
- ".ruby-version"
|
192
|
+
- ".simplecov"
|
193
|
+
- ".travis.yml"
|
194
|
+
- Gemfile
|
195
|
+
- LICENSE
|
196
|
+
- README.md
|
197
|
+
- Rakefile
|
198
|
+
- bin/console
|
199
|
+
- bin/setup
|
200
|
+
- ea-address_lookup.gemspec
|
201
|
+
- lib/ea/address_lookup.rb
|
202
|
+
- lib/ea/address_lookup/adapters.rb
|
203
|
+
- lib/ea/address_lookup/adapters/address_facade.rb
|
204
|
+
- lib/ea/address_lookup/adapters/locate_api.rb
|
205
|
+
- lib/ea/address_lookup/configuration.rb
|
206
|
+
- lib/ea/address_lookup/errors.rb
|
207
|
+
- lib/ea/address_lookup/finders.rb
|
208
|
+
- lib/ea/address_lookup/logger.rb
|
209
|
+
- lib/ea/address_lookup/test_helper/address_lookup.yml
|
210
|
+
- lib/ea/address_lookup/test_helper/mock_data.rb
|
211
|
+
- lib/ea/address_lookup/test_helper/rspec_mocks.rb
|
212
|
+
- lib/ea/address_lookup/version.rb
|
213
|
+
homepage: https://github.com/EnvironmentAgency
|
214
|
+
licenses:
|
215
|
+
- The Open Government Licence (OGL) Version 3
|
216
|
+
metadata: {}
|
217
|
+
post_install_message:
|
218
|
+
rdoc_options: []
|
219
|
+
require_paths:
|
220
|
+
- lib
|
221
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
222
|
+
requirements:
|
223
|
+
- - ">="
|
224
|
+
- !ruby/object:Gem::Version
|
225
|
+
version: '0'
|
226
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
227
|
+
requirements:
|
228
|
+
- - ">="
|
229
|
+
- !ruby/object:Gem::Version
|
230
|
+
version: '0'
|
231
|
+
requirements: []
|
232
|
+
rubyforge_project:
|
233
|
+
rubygems_version: 2.5.1
|
234
|
+
signing_key:
|
235
|
+
specification_version: 4
|
236
|
+
summary: Address lookup by postcode
|
237
|
+
test_files: []
|