postcode_validation 0.0.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 +7 -0
- data/.env +1 -0
- data/.gitignore +62 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +66 -0
- data/Rakefile +6 -0
- data/lib/postcode_validation/domain/potential_address_match.rb +21 -0
- data/lib/postcode_validation/error/request_error.rb +6 -0
- data/lib/postcode_validation/gateway/pca_potential_address_match.rb +43 -0
- data/lib/postcode_validation/plugins/solidus/models/concerns/spree_order_postcode_valid.rb +33 -0
- data/lib/postcode_validation/plugins/solidus.rb +7 -0
- data/lib/postcode_validation/use_case/validate_address.rb +37 -0
- data/lib/postcode_validation/version.rb +3 -0
- data/lib/postcode_validation.rb +9 -0
- data/postcode_validation.gemspec +29 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ea004687bfaa8fea973fd7b953dcd19b26dd224c
|
4
|
+
data.tar.gz: 454c66d84f41f92518b984dcbc058bce3e1e5b06
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fbab8666322cf4a8bfdea31004fda11f676f514cbe83de005a6119e52a569e53271ea9a30196fd7e33a99baf47c8668f9a70f153ad8bc521de74573e9f8e8f43
|
7
|
+
data.tar.gz: d4a4627d2e1880fd0546d46ea31f0b2fca2075ec5acdaec14a26753a87171f540e1da043eaeeb8a6d6ee6cb302de8e8886ed37857884b7c934ad30d3e956e7e4
|
data/.gitignore
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
/bin
|
13
|
+
|
14
|
+
# Used by dotenv library to load environment variables.
|
15
|
+
# .env
|
16
|
+
|
17
|
+
## Specific to RubyMotion:
|
18
|
+
.dat*
|
19
|
+
.repl_history
|
20
|
+
build/
|
21
|
+
*.bridgesupport
|
22
|
+
build-iPhoneOS/
|
23
|
+
build-iPhoneSimulator/
|
24
|
+
|
25
|
+
## Specific to RubyMotion (use of CocoaPods):
|
26
|
+
#
|
27
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
28
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
29
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
30
|
+
#
|
31
|
+
# vendor/Pods/
|
32
|
+
|
33
|
+
## Documentation cache and generated files:
|
34
|
+
/.yardoc/
|
35
|
+
/_yardoc/
|
36
|
+
/doc/
|
37
|
+
/rdoc/
|
38
|
+
|
39
|
+
## Environment normalization:
|
40
|
+
/.bundle/
|
41
|
+
/vendor/bundle
|
42
|
+
/lib/bundler/man/
|
43
|
+
|
44
|
+
# for a library or gem, you might want to ignore these files since the code is
|
45
|
+
# intended to run in multiple environments; otherwise, check them in:
|
46
|
+
# Gemfile.lock
|
47
|
+
# .ruby-version
|
48
|
+
# .ruby-gemset
|
49
|
+
|
50
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
51
|
+
.rvmrc
|
52
|
+
|
53
|
+
/.bundle/
|
54
|
+
/.yardoc
|
55
|
+
/Gemfile.lock
|
56
|
+
/_yardoc/
|
57
|
+
/coverage/
|
58
|
+
/doc/
|
59
|
+
/pkg/
|
60
|
+
/spec/reports/
|
61
|
+
/tmp/
|
62
|
+
.idea
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Craig J. Bass
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# PostcodeValidation
|
2
|
+
|
3
|
+
This gem provides basic postcode validation functionality which can be used in any application
|
4
|
+
|
5
|
+
It also comes with out of the box plugins for:
|
6
|
+
- Solidus
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'postcode_validation'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install postcode_validation
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Using with Solidus
|
27
|
+
|
28
|
+
TODO
|
29
|
+
|
30
|
+
### Bespoke application usage
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
use_case = PostcodeValidation::UseCase::ValidateAddress.new(
|
34
|
+
address_match_gateway: PostcodeValidation::Gateway::PCAPotentialAddressMatch.new
|
35
|
+
)
|
36
|
+
|
37
|
+
valid = use_case.execute(postcode: 'SE10SW', country: 'GB')[:valid?]
|
38
|
+
```
|
39
|
+
|
40
|
+
Country must be a valid 2-letter ISO country code.
|
41
|
+
Postcode is the postcode under test.
|
42
|
+
|
43
|
+
## Development
|
44
|
+
|
45
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
46
|
+
|
47
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/madetech/postcode_validation.
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
56
|
+
|
57
|
+
##Credits
|
58
|
+
|
59
|
+
Developed and maintained by [Made](http://www.madetech.co.uk?ref=github&repo=postcode_validation).
|
60
|
+
|
61
|
+
[](http://www.madetech.co.uk?ref=github&repo=postcode_validation)
|
62
|
+
|
63
|
+
Key contributions:
|
64
|
+
|
65
|
+
* [Craig J. Bass](https://github.com/craigjbass)
|
66
|
+
* [Seb Ashton](https://github.com/sebashton)
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module PostcodeValidation
|
2
|
+
module Domain
|
3
|
+
class PotentialAddressMatch
|
4
|
+
def initialize(text:)
|
5
|
+
@text = text
|
6
|
+
end
|
7
|
+
|
8
|
+
def postcode_matches?(postcode)
|
9
|
+
letters_and_numbers_only(text).include?(letters_and_numbers_only(postcode))
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :text
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def letters_and_numbers_only(postcode)
|
17
|
+
postcode.gsub(/[^0-9a-zA-Z]/, '')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module PostcodeValidation
|
2
|
+
module Gateway
|
3
|
+
class PCAPotentialAddressMatch
|
4
|
+
include HTTParty
|
5
|
+
|
6
|
+
class PCARequestError < PostcodeValidation::Error::RequestError; end
|
7
|
+
|
8
|
+
KEY = ENV['POSTCODE_ANYWHERE_KEY']
|
9
|
+
base_uri 'https://services.postcodeanywhere.co.uk'
|
10
|
+
|
11
|
+
def query(search_term:, country:)
|
12
|
+
response = find_postcode(country, search_term)
|
13
|
+
|
14
|
+
response.map do |row|
|
15
|
+
raise PCARequestError, error_message(row) if row.key?('Error')
|
16
|
+
|
17
|
+
PostcodeValidation::Domain::PotentialAddressMatch.new(text: row['Text'])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def error_message(row)
|
24
|
+
"#{row['Error']} #{row['Cause']} #{row['Resolution']}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_postcode(country, search_term)
|
28
|
+
JSON.parse(self.class.get('/Capture/Interactive/Find/v1.00/json.ws', lookup_parameters(country, search_term)).body)
|
29
|
+
end
|
30
|
+
|
31
|
+
def lookup_parameters(country, search_term)
|
32
|
+
{
|
33
|
+
query: {
|
34
|
+
Key: KEY,
|
35
|
+
Countries: country,
|
36
|
+
Text: search_term,
|
37
|
+
Filter: 'PostCode'
|
38
|
+
}
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module PostcodeValidation
|
2
|
+
module SpreeOrderPostcodeValid
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
state_machine.before_transition to: :delivery,
|
7
|
+
do: :validate_delivery_postcode
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def validate_delivery_postcode
|
13
|
+
return if valid_postcode?
|
14
|
+
|
15
|
+
errors.add(:ship_address_zipcode, Spree.t(:couldnt_find_postcode))
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
19
|
+
def valid_postcode?
|
20
|
+
use_case.execute(postcode: ship_address.zipcode, country: shipping_country_iso)[:valid?]
|
21
|
+
end
|
22
|
+
|
23
|
+
def shipping_country_iso
|
24
|
+
ship_address.country.iso
|
25
|
+
end
|
26
|
+
|
27
|
+
def use_case
|
28
|
+
PostcodeValidation::UseCase::ValidateAddress.new(
|
29
|
+
address_match_gateway: PostcodeValidation::Gateway::PCAPotentialAddressMatch.new
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module PostcodeValidation
|
2
|
+
module UseCase
|
3
|
+
class ValidateAddress
|
4
|
+
def initialize(address_match_gateway:)
|
5
|
+
@address_match_gateway = address_match_gateway
|
6
|
+
end
|
7
|
+
|
8
|
+
def execute(postcode:, country:)
|
9
|
+
@postcode = postcode
|
10
|
+
@country = country
|
11
|
+
{ valid?: valid_postcode? }
|
12
|
+
rescue PostcodeValidation::Error::RequestError => e
|
13
|
+
on_error(e)
|
14
|
+
{ valid?: true }
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :address_match_gateway, :postcode, :country
|
20
|
+
|
21
|
+
def on_error(e) ; end
|
22
|
+
|
23
|
+
def valid_postcode?
|
24
|
+
address = first_potential_address
|
25
|
+
return false if address.nil?
|
26
|
+
|
27
|
+
address.postcode_matches? postcode
|
28
|
+
end
|
29
|
+
|
30
|
+
def first_potential_address
|
31
|
+
potential_address_matches = @address_match_gateway.query(search_term: postcode,
|
32
|
+
country: country)
|
33
|
+
potential_address_matches.first
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'postcode_validation/version'
|
2
|
+
|
3
|
+
module PostcodeValidation
|
4
|
+
require 'httparty'
|
5
|
+
require 'postcode_validation/domain/potential_address_match'
|
6
|
+
require 'postcode_validation/error/request_error'
|
7
|
+
require 'postcode_validation/gateway/pca_potential_address_match'
|
8
|
+
require 'postcode_validation/use_case/validate_address'
|
9
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'postcode_validation/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "postcode_validation"
|
8
|
+
spec.version = PostcodeValidation::VERSION
|
9
|
+
spec.authors = ["Craig J. Bass"]
|
10
|
+
spec.email = ["craig@madetech.co.uk"]
|
11
|
+
|
12
|
+
spec.summary = %q{Provides really basic postcode validation (intended for eCommerce platforms).}
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "bin"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency 'httparty'
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
|
+
spec.add_development_dependency 'vcr'
|
27
|
+
spec.add_development_dependency 'webmock'
|
28
|
+
spec.add_development_dependency 'dotenv'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postcode_validation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Craig J. Bass
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: vcr
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: dotenv
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- craig@madetech.co.uk
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".env"
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- lib/postcode_validation.rb
|
127
|
+
- lib/postcode_validation/domain/potential_address_match.rb
|
128
|
+
- lib/postcode_validation/error/request_error.rb
|
129
|
+
- lib/postcode_validation/gateway/pca_potential_address_match.rb
|
130
|
+
- lib/postcode_validation/plugins/solidus.rb
|
131
|
+
- lib/postcode_validation/plugins/solidus/models/concerns/spree_order_postcode_valid.rb
|
132
|
+
- lib/postcode_validation/use_case/validate_address.rb
|
133
|
+
- lib/postcode_validation/version.rb
|
134
|
+
- postcode_validation.gemspec
|
135
|
+
homepage:
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 2.5.1
|
156
|
+
signing_key:
|
157
|
+
specification_version: 4
|
158
|
+
summary: Provides really basic postcode validation (intended for eCommerce platforms).
|
159
|
+
test_files: []
|