postnumer 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 63de5069ed2d148ad5d63a4f05e1fb5da73f7d9db176c4d8660d7c24ab0a619b
4
+ data.tar.gz: a19d72b474f22a820d6f5a09a1953ccf737a0b472046744200d946d18d846b57
5
+ SHA512:
6
+ metadata.gz: b51c140568cee7270568903d92f412784c26e29e69910a5b5e09a123072876393bc3af082cb9c0f3ffd6ce75ce2ef6f07b914158df42aabcedf95cd5a794a1cb
7
+ data.tar.gz: 988a23557453b0fd65683c624a826df5332cabf1230799b4bdfeec7ae87c7452d6b6da25fde7b8e2f8bed8828168b9495f5605ed4ae4df9f9b5be2a037b5903f
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ Metrics/BlockLength:
5
+ Max: 30
6
+ Exclude:
7
+ - 'spec/*'
8
+ Metrics/ClassLength:
9
+ Enabled: false
10
+ Style/OptionalBooleanParameter:
11
+ Enabled: false
12
+ Metrics/AbcSize:
13
+ Max: 20
14
+ Metrics/MethodLength:
15
+ Max: 20
16
+ Layout/HashAlignment:
17
+ Exclude:
18
+ - 'lib/postnumer.rb'
19
+ Style/CaseEquality:
20
+ Exclude:
21
+ - 'lib/postnumer.rb'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
@@ -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 aldavigdis@aldavigdis.is. 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
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in postnumer.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,93 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ postnumer (0.2.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionview (7.0.4.1)
10
+ activesupport (= 7.0.4.1)
11
+ builder (~> 3.1)
12
+ erubi (~> 1.4)
13
+ rails-dom-testing (~> 2.0)
14
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
15
+ activesupport (7.0.4.1)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 1.6, < 2)
18
+ minitest (>= 5.1)
19
+ tzinfo (~> 2.0)
20
+ ast (2.4.2)
21
+ builder (3.2.4)
22
+ concurrent-ruby (1.2.0)
23
+ crass (1.0.6)
24
+ diff-lcs (1.5.0)
25
+ erubi (1.12.0)
26
+ i18n (1.12.0)
27
+ concurrent-ruby (~> 1.0)
28
+ json (2.6.3)
29
+ loofah (2.19.1)
30
+ crass (~> 1.0.2)
31
+ nokogiri (>= 1.5.9)
32
+ minitest (5.17.0)
33
+ nokogiri (1.13.10-x86_64-linux)
34
+ racc (~> 1.4)
35
+ parallel (1.22.1)
36
+ parser (3.2.0.0)
37
+ ast (~> 2.4.1)
38
+ racc (1.6.2)
39
+ rails-dom-testing (2.0.3)
40
+ activesupport (>= 4.2.0)
41
+ nokogiri (>= 1.6)
42
+ rails-html-sanitizer (1.4.4)
43
+ loofah (~> 2.19, >= 2.19.1)
44
+ rainbow (3.1.1)
45
+ rake (13.0.6)
46
+ regexp_parser (2.6.2)
47
+ rexml (3.2.5)
48
+ rspec (3.12.0)
49
+ rspec-core (~> 3.12.0)
50
+ rspec-expectations (~> 3.12.0)
51
+ rspec-mocks (~> 3.12.0)
52
+ rspec-core (3.12.0)
53
+ rspec-support (~> 3.12.0)
54
+ rspec-expectations (3.12.2)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.12.0)
57
+ rspec-mocks (3.12.3)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.12.0)
60
+ rspec-support (3.12.0)
61
+ rubocop (1.44.1)
62
+ json (~> 2.3)
63
+ parallel (~> 1.10)
64
+ parser (>= 3.2.0.0)
65
+ rainbow (>= 2.2.2, < 4.0)
66
+ regexp_parser (>= 1.8, < 3.0)
67
+ rexml (>= 3.2.5, < 4.0)
68
+ rubocop-ast (>= 1.24.1, < 2.0)
69
+ ruby-progressbar (~> 1.7)
70
+ unicode-display_width (>= 2.4.0, < 3.0)
71
+ rubocop-ast (1.24.1)
72
+ parser (>= 3.1.1.0)
73
+ ruby-progressbar (1.11.0)
74
+ tzinfo (2.0.6)
75
+ concurrent-ruby (~> 1.0)
76
+ unicode-display_width (2.4.2)
77
+ webrick (1.7.0)
78
+ yard (0.9.28)
79
+ webrick (~> 1.7.0)
80
+
81
+ PLATFORMS
82
+ x86_64-linux
83
+
84
+ DEPENDENCIES
85
+ actionview (~> 7.0)
86
+ postnumer!
87
+ rake (~> 13.0)
88
+ rspec (~> 3.0)
89
+ rubocop (~> 1.21)
90
+ yard (~> 0.9)
91
+
92
+ BUNDLED WITH
93
+ 2.3.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Alda Vigdís Skarphéðinsdóttir
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,129 @@
1
+ # Postnumer
2
+
3
+ The Postnumer class validates, looks up and associates an Icelandic postal code
4
+ with its postal locality.
5
+
6
+ Icelandic postal codes are three numeric digits and may be handled as integers
7
+ (as this gem does internally) or as strings (as you should do in your database).
8
+
9
+ ## What this gem does not and will not do
10
+
11
+ As the role of the Icelandic postal code system is to facilitate mail sorting
12
+ and distrubution. This gem will therefore mainly facilitate the validation or
13
+ autocompletion of a pair of postal codes and their assigned localities.
14
+
15
+ What this gem will *not* do is to facilitate the assumtion of other attributes
16
+ often associated with Icelandic postal codes. This means that if you plan to use
17
+ postal codes to assume things such as wether an Icelandic postal code is rural,
18
+ is for a P.O. box, if it is within the Reykjavik Metropolitan Area or which
19
+ administrative region it belongs to, you should roll your own solution for your
20
+ own application and I wish you good luck in all your future endevours.
21
+
22
+ The [author of this gem](https://aldavigdis.is/) will gladly provide a paid
23
+ lecture on the pitfalls of postal addressing and postal code systems in Iceland
24
+ and around the world.
25
+
26
+ ## Installation
27
+
28
+ Install the gem and add to the application's Gemfile by executing:
29
+
30
+ $ bundle add postnumer
31
+
32
+ If bundler is not being used to manage dependencies:
33
+
34
+ $ gem install postnumer-gem
35
+
36
+ ## Usage
37
+
38
+ ### Using class methods
39
+
40
+ ```ruby
41
+ # For getting a hash of every Icelandic postal code and its locality,
42
+ # you can use `Postnumer::all` to get a list of every valid postal code:
43
+ Postnumer.all
44
+ =>
45
+ {101=>"Reykjavík",
46
+ 102=>"Reykjavík",
47
+ 103=>"Reykjavík",
48
+ 104=>"Reykjavík"}
49
+
50
+ # `Postnumer.all_options` provides every available postal code in Iceland as an
51
+ # array of arrays:
52
+ Postnumer.all_options
53
+ =>
54
+ [["101 Reykjavík", 101],
55
+ ["102 Reykjavík", 102],
56
+ ["103 Reykjavík", 103],
57
+ ["104 Reykjavík", 104]]
58
+
59
+ # In Rails, you can use features from the `FormOptionsHelper` such as `select`
60
+ # or `options_for_select` to build a form field from `Postnumer.all_options`.
61
+
62
+ # In Rails:
63
+ select('customer', 'postal_code', Postnumer.all_options)
64
+
65
+ # In Formtastic or ActiveAdmin:
66
+ f.input(:author, :as => :select,
67
+ :collection => options_for_select(Postnumer::all_options)
68
+ )
69
+
70
+ # If you just want to validate a postal code, you can do it like so:
71
+ Postnumer.valid?(310)
72
+ => true
73
+
74
+ # Numeric strings can also be passed to any method and will be cast internally using `String::to_i`:
75
+ # (You should always store postal codes as strings in your database.)
76
+ Postnumer.valid?("101")
77
+ => true
78
+
79
+ # The `Postnumer.valid?` method will return `false` if the provided code is invalid:
80
+ Postnumer.valid?(90210)
81
+ => false
82
+
83
+ # The `Postnumer.locality` method will provide the postal locality associated with the postal code:
84
+ Postnumer.locality(310)
85
+ => "Borgarnes"
86
+
87
+ # The `Postnumer.locality` method will simply return `nil` if the postal code is invalid.
88
+ Postnumer.locality(90210)
89
+ => nil
90
+ ```
91
+
92
+ ### Using an instance of the Postnumer class
93
+
94
+ ```ruby
95
+ # You can initialize an instance of Postnumer by initializing it.
96
+ # Note: Postnumer.new will throw an `ArgumentError` if the postal code is invalid.
97
+ pnr = Postnumer.new(310)
98
+ => #<Postnumer:0x00007fe5f2401958 @code=310, @locality="Borgarnes">
99
+
100
+ # Once initialized, the locality can be read by using the `locality` accessor:
101
+ pnr.locality
102
+ => "Borgarnes"
103
+
104
+ # The postal code is also stored in the instance and can be read using the `code` accessor:
105
+ pnr.code
106
+ => 310
107
+ ```
108
+
109
+ ## Development
110
+
111
+ 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.
112
+
113
+ ## Contributing
114
+
115
+ Bug reports and pull requests are welcome on GitHub at https://github.com/aldavigdis/postnumer-gem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/aldavigdis/postnumer-gem/blob/master/CODE_OF_CONDUCT.md).
116
+
117
+ Don't be afraid of using Rubocop for correcting your code and to make it conform to the standards defined in `.rubocop.yml`. If you need to add an exception, the please do so in your pull request and it will be looked into.
118
+
119
+ You will want to run `bundle exec rubocop -A` to see if your code has any errors and correct them on the fly.
120
+
121
+ Any pull request is welcome. Please write rspec tests for any contributed code if you have the capacity to.
122
+
123
+ ## License
124
+
125
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
126
+
127
+ ## Code of Conduct
128
+
129
+ Everyone interacting in the Postnumer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/aldavigdis/postnumer-gem/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/lib/postnumer.rb ADDED
@@ -0,0 +1,197 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The main Postnumer classs
4
+ class Postnumer
5
+ # The current version of Postnumer
6
+ VERSION = '0.2.1'
7
+
8
+ # The postal code registry
9
+ #
10
+ # This is based on the official list of Icelandic postal codes.
11
+ #
12
+ # @see https://www.byggdastofnun.is/is/postthjonusta/postnumer Póstnúmer, Byggðastofnun
13
+ POSTNUMERASKRA = {
14
+ 101..113 => 'Reykjavík',
15
+ 116 => 'Reykjavík',
16
+ 121..125 => 'Reykjavík',
17
+ 127..130 => 'Reykjavík',
18
+ 132 => 'Reykjavík',
19
+ 161..162 => 'Reykjavík',
20
+ 170..172 => 'Seltjarnarnes',
21
+ 190..192 => 'Vogar',
22
+ 200..203 => 'Kópavogur',
23
+ 206 => 'Kópavogur',
24
+ 210 => 'Garðabær',
25
+ 212 => 'Garðabær',
26
+ 220..222 => 'Hafnarfjörður',
27
+ 225 => 'Garðabær (Álftanes)',
28
+ 230 => 'Reykjanesbær',
29
+ 232..233 => 'Reykjanesbær',
30
+ 235 => 'Keflavíkurflugvöllur',
31
+ 240..241 => 'Grindavík',
32
+ 245..246 => 'Sandgerði',
33
+ 250..251 => 'Garður',
34
+ 260 => 'Reykjanesbær',
35
+ 262 => 'Reykjanesbær',
36
+ 270..271 => 'Mosfellsbær',
37
+ 276 => 'Mosfellsbær',
38
+ 300..302 => 'Akranes',
39
+ 310..311 => 'Borgarnes',
40
+ 320 => 'Reykholt í Borgarfirði',
41
+ 340..342 => 'Stykkishólmur',
42
+ 345 => 'Flatey á Breiðafirði',
43
+ 350..351 => 'Grundafjörður',
44
+ 355 => 'Ólafsvík',
45
+ 356 => 'Snæfellsbær',
46
+ 360 => 'Hellissandur',
47
+ 370..371 => 'Búðardalur',
48
+ 380..381 => 'Reykhólahreppur',
49
+ 400..401 => 'Ísafjörður',
50
+ 410 => 'Hnífsdalur',
51
+ 415..416 => 'Bolungarvík',
52
+ 420..421 => 'Súðavík',
53
+ 425..426 => 'Flateyri',
54
+ 430..431 => 'Suðureyri',
55
+ 450..451 => 'Patreksfjörður',
56
+ 460..461 => 'Tálknafjörður',
57
+ 465..466 => 'Bíldudalur',
58
+ 470..471 => 'Þingeyri',
59
+ 500 => 'Staður',
60
+ 510..512 => 'Hólmavík',
61
+ 520 => 'Drangsnes',
62
+ 524 => 'Árneshreppur',
63
+ 530..531 => 'Hvammstangi',
64
+ 540..541 => 'Blönduós',
65
+ 545..546 => 'Skagaströnd',
66
+ 550..551 => 'Sauðárkrókur',
67
+ 560..561 => 'Varmahlíð',
68
+ 565..566 => 'Hofsós',
69
+ 570 => 'Fljót',
70
+ 580..581 => 'Siglufjörður',
71
+ 600..607 => 'Akureyri',
72
+ 610 => 'Grenivík',
73
+ 611 => 'Grímsey',
74
+ 616 => 'Grenivík',
75
+ 620..621 => 'Dalvík',
76
+ 525..526 => 'Ólafsfjörður',
77
+ 630 => 'Hrísey',
78
+ 640..641 => 'Húsavík',
79
+ 645 => 'Fosshóll',
80
+ 650 => 'Laugar',
81
+ 660 => 'Mývatn',
82
+ 670..671 => 'Kópasker',
83
+ 675..676 => 'Raufarhöfn',
84
+ 680..681 => 'Þórshöfn',
85
+ 685..686 => 'Bakkafjörður',
86
+ 690..691 => 'Vopnafjörður',
87
+ 700..701 => 'Egilsstaðir',
88
+ 710..711 => 'Seyðisfjörður',
89
+ 715 => 'Mjóifjörður',
90
+ 720..721 => 'Borgarfjörður (eystri)',
91
+ 730..731 => 'Reyðarfjörður',
92
+ 735..736 => 'Eskifjörður',
93
+ 740..741 => 'Neskaupsstaður',
94
+ 750..751 => 'Fáskrúðsfjörður',
95
+ 755..756 => 'Stöðvarfjörður',
96
+ 760..761 => 'Breiðdalsvík',
97
+ 765..766 => 'Djúpivogur',
98
+ 780..781 => 'Höfn í Hornafirði',
99
+ 785 => 'Öræfi',
100
+ 800..806 => 'Selfoss',
101
+ 810 => 'Hveragerði',
102
+ 815 => 'Þorlákshöfn',
103
+ 816 => 'Ölfus',
104
+ 820 => 'Eyrarbakki',
105
+ 825 => 'Stokkseyri',
106
+ 840 => 'Laugarvatn',
107
+ 845..846 => 'Flúðir',
108
+ 850..851 => 'Hella',
109
+ 860..861 => 'Hvolsvöllur',
110
+ 870..871 => 'Vík',
111
+ 880..881 => 'Kirkjubæjarklaustur',
112
+ 900 => 'Vestmannaeyjar',
113
+ 902 => 'Vestmannaeyjar'
114
+ }.freeze
115
+
116
+ attr_reader :locality, :code
117
+
118
+ #
119
+ # Get every valid postal code as a hash
120
+ #
121
+ # @return [Hash]
122
+ #
123
+ def self.all
124
+ results = {}
125
+ POSTNUMERASKRA.each do |pnr_range, locality|
126
+ if pnr_range.is_a?(Integer)
127
+ results[pnr_range] = locality
128
+ next
129
+ end
130
+ pnr_range.to_a.each do |pnr|
131
+ results[pnr] = locality
132
+ end
133
+ end
134
+ results
135
+ end
136
+
137
+ #
138
+ # Get every postal code as an array of arrays
139
+ #
140
+ # This is useful for bulding select elements in ActionView and Formtastic.
141
+ #
142
+ # @see https://api.rubyonrails.org/v7.0.4.2/classes/ActionView/Helpers/FormOptionsHelper.html
143
+ #
144
+ # @return [Array]
145
+ #
146
+ def self.all_options
147
+ results = []
148
+ all.each do |pnr, locality|
149
+ results << ["#{pnr} #{locality}", pnr]
150
+ end
151
+ results
152
+ end
153
+
154
+ #
155
+ # Get the locality name of a specific postal code
156
+ #
157
+ # @param [Integer|String] postal_code The 3-digit Icelandic postal code
158
+ #
159
+ # @return [NilClass|String] Returns nil if invalid and the locality name as a string if valid
160
+ #
161
+ def self.locality(postal_code)
162
+ result = POSTNUMERASKRA.select { |p| p === postal_code.to_i }
163
+
164
+ return nil if result.empty?
165
+
166
+ result.first.last
167
+ end
168
+
169
+ #
170
+ # Check the validity of a postal code
171
+ #
172
+ # If the postal code provided is a string and can be cast to an integer,
173
+ # the resulting integer value will be used to test for validity.
174
+ #
175
+ # @param [Integer|String] postal_code The 3-digit Icelandic postal code
176
+ # @return [Boolean] True if valid, false if invalid
177
+ #
178
+ def self.valid?(postal_code)
179
+ return true if locality(postal_code.to_i)
180
+
181
+ false
182
+ end
183
+
184
+ #
185
+ # Initialize a Postnumer object
186
+ #
187
+ # @param [Integer|String] postal_code The 3-digit Icelandic postal code
188
+ # @raise [ArgumentError] if the postal code is invalid
189
+ #
190
+ def initialize(postal_code)
191
+ result = POSTNUMERASKRA.select { |p| p === postal_code.to_i }
192
+ raise ArgumentError, 'Postal code is invalid' if result.empty?
193
+
194
+ @code = postal_code.to_i
195
+ @locality = result.first.last
196
+ end
197
+ end
data/sig/postnumer.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Postnumer
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postnumer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Alda Vigdís Skarphéðinsdóttir
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.21'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.21'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: actionview
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '7.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '7.0'
83
+ description:
84
+ email:
85
+ - aldavigdis@aldavigdis.is
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".rspec"
91
+ - ".rubocop.yml"
92
+ - ".ruby-version"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - lib/postnumer.rb
100
+ - sig/postnumer.rbs
101
+ homepage: https://github.com/aldavigdis/postnumer-gem
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ homepage_uri: https://github.com/aldavigdis/postnumer-gem
106
+ source_code_uri: https://github.com/aldavigdis/postnumer-gem
107
+ changelog_uri: https://github.com/aldavigdis/postnumer-gem
108
+ rubygems_mfa_required: 'true'
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.6.0
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.3.7
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Validate and use Icelandic postal codes.
128
+ test_files: []