going_postal 0.1.4 → 0.1.5

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.
Files changed (3) hide show
  1. data/lib/going_postal.rb +106 -7
  2. metadata +9 -7
  3. checksums.yaml +0 -7
data/lib/going_postal.rb CHANGED
@@ -34,13 +34,106 @@
34
34
  # Canada (CA), Australia (AU), New Zeland (NZ), South Africa (ZA), and
35
35
  # The Netherlands (NL).
36
36
  #
37
- # Ireland (IE) is supported insomuch as, Ireland doesn't use postcodes, so "" or
38
- # nil are considered valid.
37
+ # Ireland (IE) as well as 60+ other countries that do not use postcodes
38
+ # - see GoingPostal::COUNTRIES_WITHOUT_POSTCODES - are supported insomuch as,
39
+ # these countires don't use postcodes, so "" or nil are considered valid.
39
40
  #
40
41
  # Currently unsupported countries will be formatted by simply stripping leading
41
42
  # and trailing whitespace, and any input will be considered valid.
42
43
  #
44
+
45
+ require "set"
46
+
43
47
  module GoingPostal
48
+ COUNTRIES_WITHOUT_POSTCODES = Set[
49
+ "AO", # Angola
50
+ "AG", # Antigua and Barbuda
51
+ "AN", # Netherlands Antilles
52
+ "AW", # Aruba
53
+ "BS", # Bahamas
54
+ "BZ", # Belize
55
+ "BJ", # Benin
56
+ "BW", # Botswana
57
+ "BF", # Burkina Faso
58
+ "BI", # Burundi
59
+ "CI", # Côte d’Ivoire
60
+ "CD", # Congo, the Democratic Republic of the
61
+ "CG", # Congo (Brazzaville)
62
+ "CM", # Cameroon
63
+ "CF", # Central African Republic
64
+ "CW", # Curaçao
65
+ "KM", # Comoros
66
+ "CK", # Cook Islands
67
+ "DJ", # Djibouti
68
+ "DM", # Dominica
69
+ "GQ", # Equatorial Guinea
70
+ "ER", # Eritrea
71
+ "FJ", # Fiji
72
+ "GM", # Gambia
73
+ "GH", # Ghana
74
+ "GD", # Grenada
75
+ "GN", # Guinea
76
+ "GY", # Guyana
77
+ "HK", # Hong Kong
78
+ "IE", # Ireland
79
+ "KI", # Kiribati
80
+ "KP", # North Korea
81
+ "MO", # Macau
82
+ "MW", # Malawi
83
+ "ML", # Mali
84
+ "MR", # Mauritania
85
+ "MU", # Mauritius
86
+ "MS", # Montserrat
87
+ "NA", # Namibia
88
+ "NR", # Nauru
89
+ "NU", # Niue
90
+ "PA", # Panama
91
+ "QA", # Qatar
92
+ "RW", # Rwanda
93
+ "KN", # Saint Kitts and Nevis
94
+ "LC", # Saint Lucia
95
+ "ST", # Sao Tome and Principe
96
+ "SC", # Seychelles
97
+ "SL", # Sierra Leone
98
+ "SB", # Solomon Islands
99
+ "SO", # Somalia
100
+ "SR", # Suriname
101
+ "SX", # Sint Maarten
102
+ "SY", # Syria
103
+ "TF", # French Southern and Antarctic Territories
104
+ "TK", # Tokelau
105
+ "TL", # East Timor
106
+ "TO", # Tonga
107
+ "TT", # Trinidad and Tobago
108
+ "TV", # Tuvalu
109
+ "TZ", # Tanzania
110
+ "UG", # Uganda
111
+ "AE", # United Arab Emirates
112
+ "VU", # Vanuatu
113
+ "YE", # Yemen
114
+ "ZW" # Zimbabwe
115
+ ]
116
+
117
+ # :section: Checking Postcode Requirements
118
+
119
+ # :call-seq: GoingPostal.not_required?(country_code) -> bool
120
+ #
121
+ # Returns true if the country spcified by the two letter country code
122
+ # country_code argument does not require postcodes, false otherwise.
123
+ #
124
+ def self.not_required?(country_code)
125
+ COUNTRIES_WITHOUT_POSTCODES.include?(country_code.to_s.upcase)
126
+ end
127
+
128
+ # :call-seq: GoingPostal.not_required?(country_code) -> bool
129
+ #
130
+ # Returns true if the country spcified by the two letter country code
131
+ # country_code argument requires postcodes, false otherwise.
132
+ #
133
+ def self.required?(country_code)
134
+ !not_required?(country_code)
135
+ end
136
+
44
137
  extend self
45
138
 
46
139
  # :section: Validation
@@ -62,12 +155,12 @@ module GoingPostal
62
155
  # Postcodes for unknown countries will always be considered valid, the return
63
156
  # value will consist of the input stripped of leading and trailing whitespace.
64
157
  #
65
- # Ireland (IE) has no postcodes, "" will be returned from in input of "" or
158
+ # For countries without postcodes, "" will be returned from in input of "" or
66
159
  # nil, false otherwise.
67
160
  #
68
161
  def postcode?(*args)
69
162
  string, country_code = get_args_for_format_postcode(args)
70
- if country_code.to_s.upcase == "IE"
163
+ if GoingPostal.not_required?(country_code)
71
164
  string.nil? || string.to_s.empty? ? "" : false
72
165
  else
73
166
  format_postcode(string, country_code) || false
@@ -120,7 +213,7 @@ module GoingPostal
120
213
  # Postcodes for unknown countries will simply be stripped of leading and
121
214
  # trailing whitespace.
122
215
  #
123
- # Ireland (IE) has no postcodes, so nil will always be returned.
216
+ # Countries without postcodes will always return nil.
124
217
  #--
125
218
  # The magic is done calling a formatting method for each country. If no such
126
219
  # method exists a default method is called stripping the leading and trailing
@@ -128,7 +221,11 @@ module GoingPostal
128
221
  #++
129
222
  def format_postcode(*args)
130
223
  string, country_code = get_args_for_format_postcode(args)
131
- method = :"format_#{country_code.to_s.downcase}_postcode"
224
+ method = if GoingPostal.not_required?(country_code)
225
+ :format_non_postcode
226
+ else
227
+ :"format_#{country_code.to_s.downcase}_postcode"
228
+ end
132
229
  respond_to?(method) ? __send__(method, string) : string.to_s.strip
133
230
  end
134
231
  alias format_post_code format_postcode
@@ -138,9 +235,11 @@ module GoingPostal
138
235
 
139
236
  # :stopdoc:
140
237
 
141
- def format_ie_postcode(string)
238
+ def format_non_postcode(string)
142
239
  nil
143
240
  end
241
+ # backwards compatibility, no need to alias other non-postcode countries
242
+ alias format_ie_postcode format_non_postcode
144
243
 
145
244
  def format_gb_postcode(string)
146
245
  out_code = string.to_s.upcase.delete(" \t\r\n")
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: going_postal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Matthew Sadler
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-03-12 00:00:00.000000000 Z
12
+ date: 2014-11-11 00:00:00.000000000 Z
12
13
  dependencies: []
13
14
  description: Post/zip code formatting and validation for the UK, US, CA and more.
14
15
  email: mat@sourcetagsandcodes.com
@@ -21,7 +22,6 @@ files:
21
22
  - README.rdoc
22
23
  homepage: http://github.com/globaldev/going_postal
23
24
  licenses: []
24
- metadata: {}
25
25
  post_install_message:
26
26
  rdoc_options:
27
27
  - --main
@@ -31,19 +31,21 @@ rdoc_options:
31
31
  require_paths:
32
32
  - lib
33
33
  required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
34
35
  requirements:
35
- - - '>='
36
+ - - ! '>='
36
37
  - !ruby/object:Gem::Version
37
38
  version: '0'
38
39
  required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
39
41
  requirements:
40
- - - '>='
42
+ - - ! '>='
41
43
  - !ruby/object:Gem::Version
42
44
  version: '0'
43
45
  requirements: []
44
46
  rubyforge_project:
45
- rubygems_version: 2.0.3
47
+ rubygems_version: 1.8.23.2
46
48
  signing_key:
47
- specification_version: 4
49
+ specification_version: 3
48
50
  summary: Global post/zip code formatting and validation mixin
49
51
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: f3b76826ddbfd629cb8ec49edd71eed7c90f6f26
4
- data.tar.gz: 7435b969946dfa00c0bec50d586ee97a703c3ba2
5
- SHA512:
6
- metadata.gz: 33640d1079991cb40dc376bb96f619dd865c6ebd28fa68438b3412a0eef556218101f37ea23b4d3ae91003e5ac820b2e228441ecee1180a26da80716f585c196
7
- data.tar.gz: 959f26dc83f106d0ef5f3aea0512c7bc6380163c2b4da217f3de9c59361066e794d7c2e3ed1d40b2e3da4b9b0550b3cbc7b3da31bd50e70b9e2464ca2e0c4973