postman_paf 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/.github/workflows/gem-push.yml +48 -0
- data/.github/workflows/gem-test.yml +22 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.rubocop.yml +36 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +174 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/postman_paf/converter.rb +65 -0
- data/lib/postman_paf/exceptions/exceptions.rb +29 -0
- data/lib/postman_paf/exceptions/last_part_exceptions.rb +36 -0
- data/lib/postman_paf/exceptions/rule_3_exceptions.rb +35 -0
- data/lib/postman_paf/exceptions/rule_5_and_7_exceptions.rb +24 -0
- data/lib/postman_paf/exceptions/rule_6_exceptions.rb +48 -0
- data/lib/postman_paf/exceptions/which_exception.rb +103 -0
- data/lib/postman_paf/printable_address.rb +35 -0
- data/lib/postman_paf/rules/address_builder.rb +30 -0
- data/lib/postman_paf/rules/building_number.rb +35 -0
- data/lib/postman_paf/rules/rule_1.rb +27 -0
- data/lib/postman_paf/rules/rule_2.rb +23 -0
- data/lib/postman_paf/rules/rule_3.rb +45 -0
- data/lib/postman_paf/rules/rule_4.rb +24 -0
- data/lib/postman_paf/rules/rule_5.rb +35 -0
- data/lib/postman_paf/rules/rule_6.rb +69 -0
- data/lib/postman_paf/rules/rule_7.rb +33 -0
- data/lib/postman_paf/rules/rules.rb +71 -0
- data/lib/postman_paf/rules/which_rule.rb +40 -0
- data/lib/postman_paf/validator.rb +65 -0
- data/lib/postman_paf/version.rb +5 -0
- data/lib/postman_paf.rb +32 -0
- data/postman_paf.gemspec +53 -0
- metadata +210 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 64c9d116c12727c20da2d3ff68940d59f958e4d3ad45a75a533c39d544d8e8c0
|
4
|
+
data.tar.gz: a8bb01aacc41ab7fa5fe07b09b236801438e9f98cbf2df5307ff793667c5ce02
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3232296fa683d32a639cd1d21793881bbcd3664c4d8cd782cb023f34b16d9d58d65f1b062132c7889c42bdfb0f557a695382bc18f83573a35bf6ff1e3ff5647c
|
7
|
+
data.tar.gz: 5fe99352caea2a57198b7614ad2348ecdaf2a34d15fd882f53015664c128f4860057cae955ce43c4a9e7cf4aff9051e950ca1093b91f20816623a09ae250defa
|
@@ -0,0 +1,48 @@
|
|
1
|
+
name: Ruby Gem Publish
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ "main" ]
|
6
|
+
|
7
|
+
jobs:
|
8
|
+
test:
|
9
|
+
runs-on: ubuntu-latest
|
10
|
+
strategy:
|
11
|
+
matrix:
|
12
|
+
ruby-version: [ '3.0', '3.1', '3.2' ]
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
- name: Set up Ruby
|
17
|
+
uses: ruby/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: ${{ matrix.ruby-version }}
|
20
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
21
|
+
- name: Run tests
|
22
|
+
run: bundle exec rspec
|
23
|
+
|
24
|
+
deploy:
|
25
|
+
needs: test
|
26
|
+
runs-on: ubuntu-latest
|
27
|
+
permissions:
|
28
|
+
packages: write
|
29
|
+
contents: read
|
30
|
+
|
31
|
+
steps:
|
32
|
+
- uses: actions/checkout@v2
|
33
|
+
- name: Set up Ruby 3.0.1
|
34
|
+
uses: ruby/setup-ruby@v1
|
35
|
+
with:
|
36
|
+
ruby-version: 3.1.2
|
37
|
+
bundler-cache: true
|
38
|
+
|
39
|
+
- name: Publish to RubyGems
|
40
|
+
run: |
|
41
|
+
mkdir -p $HOME/.gem
|
42
|
+
touch $HOME/.gem/credentials
|
43
|
+
chmod 0600 $HOME/.gem/credentials
|
44
|
+
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
45
|
+
gem build *.gemspec
|
46
|
+
gem push *.gem
|
47
|
+
env:
|
48
|
+
GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
name: Ruby Test
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
branches: [ "main" ]
|
6
|
+
|
7
|
+
jobs:
|
8
|
+
test:
|
9
|
+
runs-on: ubuntu-latest
|
10
|
+
strategy:
|
11
|
+
matrix:
|
12
|
+
ruby-version: [ '3.0', '3.1', '3.2' ]
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
- name: Set up Ruby
|
17
|
+
uses: ruby/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: ${{ matrix.ruby-version }}
|
20
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
21
|
+
- name: Run tests
|
22
|
+
run: bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.0.0
|
3
|
+
|
4
|
+
Layout/LineLength:
|
5
|
+
Max: 180
|
6
|
+
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Metrics/BlockLength:
|
11
|
+
IgnoredMethods:
|
12
|
+
- 'describe'
|
13
|
+
- 'context'
|
14
|
+
|
15
|
+
Metrics/CyclomaticComplexity:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics/MethodLength:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/PerceivedComplexity:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Style/Documentation:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/IdenticalConditionalBranches:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Style/StringLiterals:
|
31
|
+
Enabled: true
|
32
|
+
EnforcedStyle: single_quotes
|
33
|
+
|
34
|
+
Style/StringLiteralsInInterpolation:
|
35
|
+
Enabled: true
|
36
|
+
EnforcedStyle: double_quotes
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at mark.isaac@dvla.gov.uk. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 HM Government (Driver and Vehicle Licensing Agency)
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
# PostmanPAF
|
2
|
+
|
3
|
+
Convert PAF (Postcode Address File) addresses to a printable format for an envelope or address label.
|
4
|
+
|
5
|
+
This is an unofficial gem to apply Royal Mail Rules & Exceptions when converting PAF addresses.
|
6
|
+
Based on the [Royal Mail Programmers' Guide](https://www.poweredbypaf.com/wp-content/uploads/2017/07/Latest-Programmers_guide_Edition-7-Version-6.pdf), 'Formatting a PAF address for printing' (page 27).
|
7
|
+
|
8
|
+
Conversions aim to resemble addresses returned by [Royal Mail Find a PostCode](https://www.royalmail.com/find-a-postcode) as accurately as possible.
|
9
|
+
|
10
|
+
---
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'postman_paf'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle install
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install postman_paf
|
26
|
+
|
27
|
+
---
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Add PostmanPAF into a project:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
include PostmanPAF
|
34
|
+
```
|
35
|
+
|
36
|
+
### Convert method
|
37
|
+
|
38
|
+
Converting this simple address:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
PostmanPAF.convert({
|
42
|
+
buildingName: "1A",
|
43
|
+
thoroughfareName: "EXAMPLE ROAD",
|
44
|
+
postTown: "EXAMPLE TOWN",
|
45
|
+
postcode: "SA99 1BN",
|
46
|
+
dps: "1A",
|
47
|
+
language: "EN",
|
48
|
+
country: "WALES"
|
49
|
+
})
|
50
|
+
```
|
51
|
+
|
52
|
+
Returns:
|
53
|
+
```ruby
|
54
|
+
{"line1"=>"1A EXAMPLE ROAD", "line5"=>"EXAMPLE TOWN", "postcode"=>"SA99 1BN", "country"=>"WALES", "language"=>"EN", "dps"=>"1A"}
|
55
|
+
```
|
56
|
+
|
57
|
+
Converting this more complex address:
|
58
|
+
```ruby
|
59
|
+
PostmanPAF.convert({
|
60
|
+
subBuildingName: "1 EXAMPLE HOUSE 2A",
|
61
|
+
buildingName: "EXAMPLE PLACE 3-4",
|
62
|
+
thoroughfareName: "EXAMPLE ROAD",
|
63
|
+
postTown: "EXAMPLE TOWN",
|
64
|
+
postcode: "SA99 1BN",
|
65
|
+
dps: "1A",
|
66
|
+
language: "EN",
|
67
|
+
country: "WALES"
|
68
|
+
})
|
69
|
+
```
|
70
|
+
|
71
|
+
Returns:
|
72
|
+
```ruby
|
73
|
+
{"line1"=>"1 EXAMPLE HOUSE 2A EXAMPLE PLACE", "line2"=>"3-4 EXAMPLE ROAD", "line5"=>"EXAMPLE TOWN", "postcode"=>"SA99 1BN", "country"=>"WALES", "language"=>"EN", "dps"=>"1A"}
|
74
|
+
```
|
75
|
+
|
76
|
+
PostmanPAF can convert a single PAF address as a Hash, or multiple PAF addresses as an Array of Hashes.
|
77
|
+
|
78
|
+
PAF address Hashes must include `postTown` and `postcode` keys as a minimum requirement.
|
79
|
+
|
80
|
+
#### Optional Arguments
|
81
|
+
|
82
|
+
To limit lines 1-5 of a printable address to a maximum number of characters, use the optional argument `max_line_length`. Note that this **will** cause data to be 'cut off' in places, e.g. 'TOWN' in postTown below:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
PostmanPAF.convert({
|
86
|
+
buildingName: "1A",
|
87
|
+
thoroughfareName: "EXAMPLE ROAD",
|
88
|
+
postTown: "EXAMPLE TOWN",
|
89
|
+
postcode: "SA99 1BN",
|
90
|
+
dps: "1A",
|
91
|
+
language: "EN",
|
92
|
+
country: "WALES" }, max_line_length: 10)
|
93
|
+
```
|
94
|
+
Returns:
|
95
|
+
```ruby
|
96
|
+
{"line1"=>"1A EXAMPLE", "line5"=>"EXAMPLE TO", "postcode"=>"SA99 1BN", "country"=>"WALES", "language"=>"EN", "dps"=>"1A"}
|
97
|
+
```
|
98
|
+
|
99
|
+
If you want to view the Rule & Exception(s) applied to the address during format conversion, set the optional argument `logging` to `true`. This:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
PostmanPAF.convert({
|
103
|
+
buildingName: "1A",
|
104
|
+
thoroughfareName: "EXAMPLE ROAD",
|
105
|
+
postTown: "EXAMPLE TOWN",
|
106
|
+
postcode: "SA99 1BN",
|
107
|
+
dps: "1A",
|
108
|
+
language: "EN",
|
109
|
+
country: "WALES" }, logging: true)
|
110
|
+
```
|
111
|
+
|
112
|
+
Returns the converted address format along with a log to `STDOUT`:
|
113
|
+
|
114
|
+
```shell
|
115
|
+
[2023-12-25 11:11:11 +0000 INFO PostmanPAF] -- ["1A EXAMPLE ROAD", "EXAMPLE TOWN"] | rule3 | exception_ii | SA99 1BN
|
116
|
+
```
|
117
|
+
|
118
|
+
## Development
|
119
|
+
|
120
|
+
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.
|
121
|
+
|
122
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
123
|
+
|
124
|
+
---
|
125
|
+
|
126
|
+
Special thanks to Alexander O'Mahoney, Christopher Thomas and Deepak Thankachan.
|
127
|
+
|
128
|
+
.,,,.
|
129
|
+
,@#. /@*
|
130
|
+
&* %/
|
131
|
+
%# .@
|
132
|
+
/@/ ,#
|
133
|
+
(@%. ./%%%(, &
|
134
|
+
/@( (@* /./. ,@ */
|
135
|
+
@/ @, .. ,( @ &
|
136
|
+
%( %. #*& & &
|
137
|
+
(/ & @ .*
|
138
|
+
@ % ,&@@@& *&@@@@@.
|
139
|
+
@ & /@@@@@@@@@&%.,/(@@@@@@@.
|
140
|
+
#* / # *@,(%@@@#/ ./#. *(
|
141
|
+
(# / (. # % %
|
142
|
+
(@* ., (# .* # (/
|
143
|
+
., . ,, % & * *, ,,
|
144
|
+
/ *./%,. .@&(, . &@,%&, &.
|
145
|
+
( ( %#..%% @ &@& @ @./%( &
|
146
|
+
# # / & @ /(
|
147
|
+
% % *. .( %
|
148
|
+
% %/ ,&. .& ,/
|
149
|
+
# .( (.
|
150
|
+
,( % .///*,. *&/
|
151
|
+
.&/ &
|
152
|
+
& (,
|
153
|
+
/, %. # %
|
154
|
+
& // #( &
|
155
|
+
# /.
|
156
|
+
# /
|
157
|
+
@*@*&. % (
|
158
|
+
% * .% %% .%
|
159
|
+
#@, # *, @ && & .(
|
160
|
+
* (. # % /&( *.,& &.& ,@#.
|
161
|
+
% ** # & (# #, & #%. /& % ,/ (((
|
162
|
+
# & ( #, // # .,@@@&, / .%#(. .(
|
163
|
+
, .% /@%, .(*@ .@ & ,@@@@@.#.@ (#, %.
|
164
|
+
# #(. ,# # %&. @.@@@% ( % ( &
|
165
|
+
* .@ #, ,& #@@@@( & /# ( &.
|
166
|
+
.&. ( *% #* *% .@@@@@@@ @ //
|
167
|
+
& % , .& (* &*&@@@@@@, .@ ,%
|
168
|
+
(. % ( ,%&. *# #@@@@, %* @
|
169
|
+
& // * *@ @ .#/ .% *. . @
|
170
|
+
& ,/* & ( # . &
|
171
|
+
& .@, % % ,#.# (.
|
172
|
+
/( &. , *. % /,
|
173
|
+
.@* .@ , ( % ( %# &.
|
174
|
+
&* /@. .. &,@ @ * * *#
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'postman_paf'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PostmanPAF
|
4
|
+
# Converter class for converting validated PAF addresses to printable
|
5
|
+
# format addresses using applicable Royal Mail rules and exceptions.
|
6
|
+
class Converter
|
7
|
+
POST_TOWN = 'postTown'
|
8
|
+
POSTCODE = 'postcode'
|
9
|
+
|
10
|
+
# @param paf_address_data [Hash, Array<Hash>] to be converted.
|
11
|
+
# @param max_line_length [Integer, String] optional argument to set max
|
12
|
+
# length of printable address lines 1-5.
|
13
|
+
# @param logging [Boolean] optional argument for logging of rules and
|
14
|
+
# exceptions applied to address during conversion.
|
15
|
+
# @return Converter object.
|
16
|
+
def initialize(paf_address_data:, max_line_length: nil, logging: false)
|
17
|
+
@paf_address_data = paf_address_data.is_a?(Array) ? paf_address_data : [paf_address_data]
|
18
|
+
@max_line_length = if max_line_length.is_a?(Integer) || max_line_length.is_a?(String)
|
19
|
+
max_line_length.to_i.positive? ? max_line_length.to_i : nil
|
20
|
+
end
|
21
|
+
@logging = logging.is_a?(TrueClass) ? true : false
|
22
|
+
end
|
23
|
+
|
24
|
+
# Handles conversion of single or multiple PAF addresses (i.e. single
|
25
|
+
# Hash or Array of Hashes) - in both cases Hash keys are sanitised
|
26
|
+
# in case of newer JSON-style syntax, where Ruby stores keys as
|
27
|
+
# symbols.
|
28
|
+
# @return [Hash, Array<Hash>, nil] converted address data as JSON Hash
|
29
|
+
# or nil if invalid PAF address data is provided.
|
30
|
+
def convert
|
31
|
+
converted_addresses = @paf_address_data.map { |address| address.transform_keys { |k| SimpleSymbolize.camelize(k).to_s } }
|
32
|
+
.map { |address| paf_to_printable(paf_address: address) }
|
33
|
+
|
34
|
+
converted_addresses.count.eql?(1) ? converted_addresses.pop : converted_addresses
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# Converts a single PAF address to printable format after determining
|
40
|
+
# applicable rule and exception(s). Truncation and logging optional.
|
41
|
+
# @param paf_address [Hash] to be converted.
|
42
|
+
# @return [Hash] converted address data as JSON Hash.
|
43
|
+
def paf_to_printable(paf_address:)
|
44
|
+
rule = PostmanPAF::Rules.which_rule(paf_address: paf_address)
|
45
|
+
exception = PostmanPAF::Exceptions.which_exception(rule: rule, paf_address: paf_address)
|
46
|
+
lines = PostmanPAF::Rules.apply_rule(rule: rule, paf_address: paf_address, exception: exception).compact
|
47
|
+
|
48
|
+
lines[4] = paf_address[POST_TOWN]
|
49
|
+
|
50
|
+
if @max_line_length
|
51
|
+
lines = lines.map do |line|
|
52
|
+
if line.nil?
|
53
|
+
line
|
54
|
+
else
|
55
|
+
line.slice(0..(@max_line_length - 1))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
LOG.info("#{lines.compact} | #{rule} | #{exception} | #{paf_address[POSTCODE]}") if @logging
|
61
|
+
|
62
|
+
PostmanPAF::Rules::BuildAddress.build_printable_address(paf_address: paf_address, lines: lines)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PostmanPAF
|
4
|
+
module Exceptions
|
5
|
+
# Only buildingName and subBuildingName values determine if an exception
|
6
|
+
# applies.
|
7
|
+
BUILDING_NAME = 'buildingName'
|
8
|
+
SUB_BUILDING_NAME = 'subBuildingName'
|
9
|
+
|
10
|
+
# Determines exception(s) to be applied to PAF address during conversion
|
11
|
+
# if specific criteria are met - exceptions can only apply to Rule 3,
|
12
|
+
# Rule 5, Rule 6 and Rule 7 PAF addresses.
|
13
|
+
# @param rule [Symbol] the conversion rule to be applied.
|
14
|
+
# @param paf_address [Hash] to be converted.
|
15
|
+
# @return [Symbol, Array<Symbol>] the exception(s) to be applied.
|
16
|
+
def self.which_exception(rule:, paf_address:)
|
17
|
+
case rule
|
18
|
+
when :rule3
|
19
|
+
PostmanPAF::Exceptions::WhichException.exception_to_apply_rule3(paf_address: paf_address)
|
20
|
+
when :rule5, :rule7
|
21
|
+
PostmanPAF::Exceptions::WhichException.exception_to_apply_rule5_rule7(paf_address: paf_address)
|
22
|
+
when :rule6
|
23
|
+
PostmanPAF::Exceptions::WhichException.exception_to_apply_rule6(paf_address: paf_address)
|
24
|
+
else
|
25
|
+
:exceptions_not_applicable
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PostmanPAF
|
4
|
+
module Exceptions
|
5
|
+
module WhichException
|
6
|
+
# Checks if last part of the buildingName String (AKA numeric part)
|
7
|
+
# contains an exception. Checks buildingName ONLY - no numeric
|
8
|
+
# part exceptions were found for subBuildingName in testing.
|
9
|
+
# @param paf_address [Hash] to be converted.
|
10
|
+
# @param last_part [String] the numeric part of the buildingName.
|
11
|
+
# @return [Symbol] the exception to apply to the address based
|
12
|
+
# on criteria met by last_part.
|
13
|
+
def self.last_part_exception_to_apply(paf_address:, last_part:)
|
14
|
+
if last_part.match?(/^\d+$/) && contains_digit_between_0_and_9999?(paf_address[BUILDING_NAME])
|
15
|
+
:exception_to_rule
|
16
|
+
elsif contains_digit_as_first_and_last?(last_part)
|
17
|
+
if contains_address_keyword?(paf_address[BUILDING_NAME])
|
18
|
+
:exception_iv
|
19
|
+
elsif contains_digits_with_decimals_or_forward_slashes?(paf_address[BUILDING_NAME])
|
20
|
+
:exception_v
|
21
|
+
else
|
22
|
+
:exception_i_numeric_part
|
23
|
+
end
|
24
|
+
elsif contains_digit_as_first_and_penultimate_and_alpha_as_last?(last_part)
|
25
|
+
if contains_address_keyword?(paf_address[BUILDING_NAME])
|
26
|
+
:exception_iv
|
27
|
+
else
|
28
|
+
:exception_ii_numeric_part
|
29
|
+
end
|
30
|
+
else
|
31
|
+
:no_exception
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PostmanPAF
|
4
|
+
module Exceptions
|
5
|
+
module WhichException
|
6
|
+
# Checks if the whole buildingName String contains an Exception I,
|
7
|
+
# Exception II or Exception III. Then checks if last part of the
|
8
|
+
# buildingName String (AKA numeric part) contains an exception.
|
9
|
+
# @param paf_address [Hash] to be converted.
|
10
|
+
# @return [Symbol] the Rule 3 conversion exception to apply to the
|
11
|
+
# address based on criteria met.
|
12
|
+
def self.exception_to_apply_rule3(paf_address:)
|
13
|
+
if contains_digit_as_first_and_last?(paf_address[BUILDING_NAME])
|
14
|
+
:exception_i
|
15
|
+
elsif contains_digit_as_first_and_penultimate_and_alpha_as_last?(paf_address[BUILDING_NAME])
|
16
|
+
:exception_ii
|
17
|
+
elsif contains_one_alpha_character?(paf_address[BUILDING_NAME])
|
18
|
+
:exception_iii
|
19
|
+
else
|
20
|
+
building_name_parts = paf_address[BUILDING_NAME].split(' ')
|
21
|
+
if building_name_parts.size > 1
|
22
|
+
last_part = building_name_parts.last
|
23
|
+
if last_part.match?(/\d/)
|
24
|
+
PostmanPAF::Exceptions::WhichException.last_part_exception_to_apply(paf_address: paf_address, last_part: last_part)
|
25
|
+
else
|
26
|
+
:no_exception
|
27
|
+
end
|
28
|
+
else
|
29
|
+
:no_exception
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PostmanPAF
|
4
|
+
module Exceptions
|
5
|
+
module WhichException
|
6
|
+
# Checks if the whole subBuildingName String contains an Exception I,
|
7
|
+
# Exception II or Exception III.
|
8
|
+
# @param paf_address [Hash] to be converted.
|
9
|
+
# @return [Symbol] the Rule 5 or Rule 7 conversion exception to apply
|
10
|
+
# to the address based on criteria met.
|
11
|
+
def self.exception_to_apply_rule5_rule7(paf_address:)
|
12
|
+
if contains_digit_as_first_and_last?(paf_address[SUB_BUILDING_NAME])
|
13
|
+
:exception_i
|
14
|
+
elsif contains_digit_as_first_and_penultimate_and_alpha_as_last?(paf_address[SUB_BUILDING_NAME])
|
15
|
+
:exception_ii
|
16
|
+
elsif contains_one_alpha_character?(paf_address[SUB_BUILDING_NAME])
|
17
|
+
:exception_iii
|
18
|
+
else
|
19
|
+
:no_exception
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|