snail 2.2.2 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/assets/DE.yml +1 -1
- data/lib/snail/constants.rb +503 -507
- data/lib/snail/initializable.rb +3 -6
- data/lib/snail/version.rb +1 -1
- data/lib/snail.rb +47 -50
- metadata +3 -4
data/lib/snail/initializable.rb
CHANGED
@@ -1,13 +1,10 @@
|
|
1
1
|
class Snail
|
2
2
|
module Initializable
|
3
|
-
def initialize(attrs = {}
|
3
|
+
def initialize(attrs = {})
|
4
4
|
attrs.each do |k, v|
|
5
5
|
m = "#{k}="
|
6
|
-
|
7
|
-
|
8
|
-
else
|
9
|
-
raise UnknownAttribute.new(k)
|
10
|
-
end
|
6
|
+
raise UnknownAttribute, k unless respond_to? m
|
7
|
+
send(m, v)
|
11
8
|
end
|
12
9
|
yield self if block_given?
|
13
10
|
end
|
data/lib/snail/version.rb
CHANGED
data/lib/snail.rb
CHANGED
@@ -14,51 +14,17 @@ class Snail
|
|
14
14
|
# this will be raised whenever initialization doesn't recognize a key
|
15
15
|
class UnknownAttribute < ArgumentError; end
|
16
16
|
|
17
|
-
# My made-up standard fields.
|
18
|
-
attr_accessor :name
|
19
|
-
attr_accessor :line_1
|
20
|
-
attr_accessor :line_2
|
21
|
-
attr_accessor :city
|
22
|
-
attr_accessor :region
|
23
|
-
attr_accessor :postal_code
|
24
|
-
attr_reader :country
|
25
|
-
|
26
|
-
# Store country as ISO-3166 Alpha 2
|
27
|
-
def country=(val)
|
28
|
-
@country = Snail.lookup_country_iso(val)
|
29
|
-
end
|
30
|
-
|
31
|
-
# Aliases for easier assignment compatibility
|
32
|
-
{
|
33
|
-
:full_name => :name,
|
34
|
-
:street => :line_1,
|
35
|
-
:street_1 => :line_1,
|
36
|
-
:street1 => :line_1,
|
37
|
-
:street_2 => :line_2,
|
38
|
-
:street2 => :line_2,
|
39
|
-
:town => :city,
|
40
|
-
:locality => :city,
|
41
|
-
:state => :region,
|
42
|
-
:province => :region,
|
43
|
-
:zip => :postal_code,
|
44
|
-
:zip_code => :postal_code,
|
45
|
-
:postcode => :postal_code
|
46
|
-
}.each do |new, existing|
|
47
|
-
alias_method "#{new}=", "#{existing}="
|
48
|
-
end
|
49
|
-
|
50
17
|
# Load the SnailHelpers module into ActionView::Base. Previously this was done
|
51
18
|
# automatically, but going forward it must be included explicitly by calling
|
52
19
|
# Snail.load_helpers.
|
53
20
|
def self.load_helpers
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
21
|
+
return unless defined? ActionView
|
22
|
+
warn '[DEPRECATION] Snail::Helpers will be removed in a future release.'
|
23
|
+
ActionView::Base.class_eval { include Snail::Helpers }
|
58
24
|
end
|
59
25
|
|
60
26
|
def self.home_country
|
61
|
-
@home_country ||=
|
27
|
+
@home_country ||= 'US'
|
62
28
|
end
|
63
29
|
|
64
30
|
def self.home_country=(val)
|
@@ -66,19 +32,46 @@ class Snail
|
|
66
32
|
end
|
67
33
|
|
68
34
|
def self.lookup_country_iso(val)
|
69
|
-
return
|
35
|
+
return if val.nil? || val.empty?
|
70
36
|
val = val.upcase
|
71
37
|
if ::Snail::Iso3166::ALPHA2[val]
|
72
38
|
val
|
73
|
-
elsif iso = ::Snail::Iso3166::ALPHA2_EXCEPTIONS[val]
|
74
|
-
iso
|
75
|
-
elsif iso = ::Snail::Iso3166::ALPHA3_TO_ALPHA2[val]
|
76
|
-
iso
|
77
39
|
else
|
78
|
-
|
40
|
+
::Snail::Iso3166::ALPHA2_EXCEPTIONS[val] || ::Snail::Iso3166::ALPHA3_TO_ALPHA2[val]
|
79
41
|
end
|
80
42
|
end
|
81
43
|
|
44
|
+
# Snail's canonical fields
|
45
|
+
attr_accessor :name
|
46
|
+
alias full_name= name=
|
47
|
+
|
48
|
+
attr_accessor :line_1
|
49
|
+
alias street= line_1=
|
50
|
+
alias street_1= line_1=
|
51
|
+
alias street1= line_1=
|
52
|
+
|
53
|
+
attr_accessor :line_2
|
54
|
+
alias street_2= line_2=
|
55
|
+
alias street2= line_2=
|
56
|
+
|
57
|
+
attr_accessor :city
|
58
|
+
alias town= city=
|
59
|
+
alias locality= city=
|
60
|
+
|
61
|
+
attr_accessor :region
|
62
|
+
alias state= region=
|
63
|
+
alias province= region=
|
64
|
+
|
65
|
+
attr_accessor :postal_code
|
66
|
+
alias zip= postal_code=
|
67
|
+
alias zip_code= postal_code=
|
68
|
+
alias postcode= postal_code=
|
69
|
+
|
70
|
+
attr_reader :country
|
71
|
+
def country=(val)
|
72
|
+
@country = Snail.lookup_country_iso(val)
|
73
|
+
end
|
74
|
+
|
82
75
|
# Where the mail is coming from.
|
83
76
|
def origin=(val)
|
84
77
|
@origin = Snail.lookup_country_iso(val)
|
@@ -90,7 +83,7 @@ class Snail
|
|
90
83
|
end
|
91
84
|
|
92
85
|
def international?
|
93
|
-
country &&
|
86
|
+
country && origin != country
|
94
87
|
end
|
95
88
|
|
96
89
|
def to_s(with_country: nil)
|
@@ -102,7 +95,9 @@ class Snail
|
|
102
95
|
line_2,
|
103
96
|
city_line,
|
104
97
|
(country_line if with_country)
|
105
|
-
]
|
98
|
+
]
|
99
|
+
.reject { |line| line.nil? || line.empty? }
|
100
|
+
.join("\n")
|
106
101
|
end
|
107
102
|
|
108
103
|
def to_html(with_country: nil)
|
@@ -123,7 +118,7 @@ class Snail
|
|
123
118
|
"#{postal_code} #{city} #{region}"
|
124
119
|
when 'BY'
|
125
120
|
"#{postal_code} #{city}-#{region}"
|
126
|
-
when 'US', 'CA', 'AU', nil,
|
121
|
+
when 'US', 'CA', 'AU', nil, ''
|
127
122
|
"#{city} #{region} #{postal_code}"
|
128
123
|
when 'IL', 'DK', 'FI', 'FR', 'DE', 'GR', 'NO', 'ES', 'SE', 'TR', 'CY', 'PT', 'MK', 'BA'
|
129
124
|
"#{postal_code} #{city}"
|
@@ -137,7 +132,7 @@ class Snail
|
|
137
132
|
"#{city} #{postal_code}" # Locally these may be on separate lines. The USPS prefers the city line above the country line, though.
|
138
133
|
when 'EC'
|
139
134
|
"#{postal_code} #{city}"
|
140
|
-
when 'HK', 'IQ', 'YE', 'QA', 'AL'
|
135
|
+
when 'HK', 'IQ', 'YE', 'QA', 'AL', 'BS'
|
141
136
|
"#{city}"
|
142
137
|
when 'AE'
|
143
138
|
"#{postal_code}\n#{city}"
|
@@ -153,8 +148,10 @@ class Snail
|
|
153
148
|
"SI-#{postal_code} #{city}"
|
154
149
|
when 'CZ'
|
155
150
|
"#{postal_code} #{region}\n#{city}"
|
151
|
+
when 'SG'
|
152
|
+
"#{country_line.titleize} #{postal_code}"
|
156
153
|
else
|
157
|
-
if Kernel.const_defined?(
|
154
|
+
if Kernel.const_defined?('Rails')
|
158
155
|
Rails.logger.error "[Snail] Unknown Country: #{country}"
|
159
156
|
end
|
160
157
|
"#{city} #{region} #{postal_code}"
|
@@ -162,7 +159,7 @@ class Snail
|
|
162
159
|
end
|
163
160
|
|
164
161
|
def country_line
|
165
|
-
(translated_country(
|
162
|
+
(translated_country(origin, @country) || translated_country('US', @country))
|
166
163
|
end
|
167
164
|
|
168
165
|
def translated_country(origin, country)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snail
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lance Ivy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -310,8 +310,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
310
310
|
- !ruby/object:Gem::Version
|
311
311
|
version: '0'
|
312
312
|
requirements: []
|
313
|
-
|
314
|
-
rubygems_version: 2.5.1
|
313
|
+
rubygems_version: 3.0.3.1
|
315
314
|
signing_key:
|
316
315
|
specification_version: 4
|
317
316
|
summary: Easily format snail mail addresses for international delivery
|