postal_address 0.9.3 → 0.9.6
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 +5 -5
- data/.travis.yml +2 -2
- data/README.md +0 -1
- data/lib/postal_address/address.rb +16 -11
- data/lib/postal_address/formatters/html.rb +17 -11
- data/lib/postal_address/formatters/text.rb +10 -5
- data/lib/postal_address/version.rb +1 -1
- data/lib/postal_address.rb +2 -2
- data/postal_address.gemspec +4 -5
- data/spec/postal_address_spec.rb +29 -40
- data/spec/spec_helper.rb +1 -4
- metadata +9 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 732395d6e01e69eb058f8040e1e4897b00d5362126261ce8d5519308cf6f83bf
|
4
|
+
data.tar.gz: abf24032951733529ab117b7b985655183a5fb2d127e84c4f4d9cd405b02afb6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25f0707107ac25f08e63cda2a703b81e06230113eb3e5fb197f4dc6396c24c21efc0fc204aa6a6faf4f5837c865d11efeca059e2c3c77d3daa69835c49774ac7
|
7
|
+
data.tar.gz: 7727b70db57733a9edc17410324278c6457314831b85c445a5cf0377ac89b1d71c51f29dc22c6a759267415ccca4cbfff2a0ec5fc944d5467af74b0845bc122e
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,31 +1,36 @@
|
|
1
1
|
module Postal
|
2
2
|
class Address
|
3
|
-
Fields =
|
3
|
+
Fields = %i(recipient street zip state city country country_code).freeze
|
4
4
|
|
5
|
-
attr_accessor
|
6
|
-
|
5
|
+
attr_accessor(*Fields)
|
6
|
+
|
7
|
+
def initialize(**attrs, &block)
|
8
|
+
attrs.each { |k,v| public_send("#{k}=", v) if respond_to?("#{k}=") }
|
9
|
+
yield self if block_given?
|
10
|
+
end
|
11
|
+
|
7
12
|
def country_code=(code)
|
8
13
|
@country_code = Postal.sanitize(code)
|
9
14
|
end
|
10
15
|
|
11
16
|
def country
|
12
|
-
|
17
|
+
Postal.country_names[country_code]
|
13
18
|
end
|
14
19
|
|
15
|
-
def
|
16
|
-
|
20
|
+
def values
|
21
|
+
Fields.map(&method(:public_send))
|
17
22
|
end
|
18
23
|
|
19
24
|
def to_h
|
20
|
-
Fields.
|
25
|
+
Fields.zip(values).to_h
|
21
26
|
end
|
22
27
|
|
23
|
-
def to_s
|
24
|
-
AddressFormatter::Text.new(to_h).render
|
28
|
+
def to_s(**)
|
29
|
+
AddressFormatter::Text.new(to_h).render(**)
|
25
30
|
end
|
26
31
|
|
27
|
-
def to_html(
|
28
|
-
AddressFormatter::HTML.new(to_h).render(
|
32
|
+
def to_html(**)
|
33
|
+
AddressFormatter::HTML.new(to_h).render(**)
|
29
34
|
end
|
30
35
|
end
|
31
36
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Postal
|
2
2
|
module AddressFormatter
|
3
3
|
class HTML < Text
|
4
|
-
|
4
|
+
ItemType = 'http://schema.org/PostalAddress'
|
5
5
|
ItemProp = {
|
6
6
|
recipient: 'name',
|
7
7
|
street: 'streetAddress',
|
@@ -11,25 +11,31 @@ module Postal
|
|
11
11
|
country: 'addressCountry'
|
12
12
|
}
|
13
13
|
|
14
|
-
def render(params
|
15
|
-
|
14
|
+
def render(**params)
|
15
|
+
tagname = params.delete(:tag) || :p
|
16
|
+
content = super.gsub("\n", "<br>")
|
17
|
+
tag tagname, content, **params.merge(itemscope: nil, itemtype: ItemType)
|
16
18
|
end
|
17
19
|
|
18
20
|
private
|
19
21
|
|
20
22
|
def address_data
|
21
|
-
super.
|
22
|
-
|
23
|
-
end
|
23
|
+
super.map do |key, value|
|
24
|
+
[key, tag_for(key, value)]
|
25
|
+
end.to_h
|
26
|
+
end
|
27
|
+
|
28
|
+
def tag_for(key, value)
|
29
|
+
tag :span, value, itemprop: ItemProp[key] if value
|
24
30
|
end
|
25
31
|
|
26
|
-
def
|
27
|
-
"<#{
|
32
|
+
def tag(name, content='', **attrs)
|
33
|
+
"<#{name}#{tag_attributes(attrs)}>#{content}</#{name}>"
|
28
34
|
end
|
29
|
-
|
35
|
+
|
30
36
|
def tag_attributes(attrs)
|
31
|
-
attrs.map { |k,v| v ? %[#{k}="#{v}"]
|
37
|
+
attrs.map { |k,v| v.nil? ? k : %[#{k}="#{v}"] }.unshift('').join(' ')
|
32
38
|
end
|
33
39
|
end
|
34
40
|
end
|
35
|
-
end
|
41
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module Postal
|
2
2
|
module AddressFormatter
|
3
3
|
class Text
|
4
|
+
attr_reader :address
|
4
5
|
# expects any hash with the 7 keys from Postal::Address::Fields
|
5
6
|
def initialize(address)
|
6
7
|
@address = address
|
@@ -13,17 +14,21 @@ module Postal
|
|
13
14
|
private
|
14
15
|
|
15
16
|
def address_data
|
16
|
-
|
17
|
-
|
17
|
+
address[:country] = nil if home_country?
|
18
|
+
address
|
18
19
|
end
|
19
20
|
|
20
21
|
def address_format
|
21
|
-
Postal.address_formats
|
22
|
+
Postal.address_formats.fetch(address[:country_code]) { default_address_format }
|
22
23
|
end
|
23
24
|
|
24
25
|
def default_address_format
|
25
|
-
Postal.address_formats[
|
26
|
+
Postal.address_formats[address[:state] ? 'us' : 'de']
|
27
|
+
end
|
28
|
+
|
29
|
+
def home_country?
|
30
|
+
Postal.home_country == address[:country_code]
|
26
31
|
end
|
27
32
|
end
|
28
33
|
end
|
29
|
-
end
|
34
|
+
end
|
data/lib/postal_address.rb
CHANGED
@@ -18,11 +18,11 @@ module Postal
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def address_formats
|
21
|
-
@address_formats ||= YAML.load_file(path_for(:address_formats))
|
21
|
+
@address_formats ||= YAML.load_file(path_for(:address_formats), aliases: true)
|
22
22
|
end
|
23
23
|
|
24
24
|
def country_names
|
25
|
-
@country_names ||= YAML.load_file(path_for(:country_names))
|
25
|
+
@country_names ||= YAML.load_file(path_for(:country_names), aliases: true)
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
data/postal_address.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Kevin Melchert"]
|
10
10
|
spec.email = ["kevin.melchert@gmail.com"]
|
11
11
|
spec.description = %q{International postal address formatting}
|
12
|
-
spec.summary = %q{International postal address formatting}
|
13
|
-
spec.homepage = ""
|
12
|
+
spec.summary = %q{International postal address formatting with HTML Microformats}
|
13
|
+
spec.homepage = "https://github.com/max-power/postal_address"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -18,8 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "bundler"
|
22
22
|
spec.add_development_dependency "rake"
|
23
|
-
spec.add_development_dependency "minitest"
|
24
|
-
spec.add_development_dependency "turn"
|
23
|
+
spec.add_development_dependency "minitest"
|
25
24
|
end
|
data/spec/postal_address_spec.rb
CHANGED
@@ -3,23 +3,23 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe Postal do
|
5
5
|
it "should load the formats" do
|
6
|
-
Postal.address_formats.wont_be :empty?
|
7
|
-
Postal.address_formats.must_be_kind_of Hash
|
6
|
+
_(Postal.address_formats).wont_be :empty?
|
7
|
+
_(Postal.address_formats).must_be_kind_of Hash
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should load the country_name" do
|
11
|
-
Postal.country_names.wont_be :empty?
|
12
|
-
Postal.country_names.must_be_kind_of Hash
|
11
|
+
_(Postal.country_names).wont_be :empty?
|
12
|
+
_(Postal.country_names).must_be_kind_of Hash
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should read/write home country" do
|
16
|
-
Postal.home_country =
|
17
|
-
Postal.home_country.must_equal 'de'
|
16
|
+
Postal.home_country = :de
|
17
|
+
_(Postal.home_country).must_equal 'de'
|
18
18
|
end
|
19
19
|
|
20
|
-
it "should
|
21
|
-
Postal.home_country =
|
22
|
-
Postal.home_country.must_equal 'de'
|
20
|
+
it "should sanititze home country" do
|
21
|
+
Postal.home_country = 'DE'
|
22
|
+
_(Postal.home_country).must_equal 'de'
|
23
23
|
end
|
24
24
|
|
25
25
|
describe "Instance" do
|
@@ -28,83 +28,72 @@ describe Postal do
|
|
28
28
|
}
|
29
29
|
|
30
30
|
let(:address) do
|
31
|
-
Postal::Address.new(
|
31
|
+
Postal::Address.new(
|
32
32
|
recipient: 'Tobias Füncke',
|
33
33
|
street: '101 Broadway',
|
34
34
|
city: 'New York City',
|
35
35
|
zip: '10002',
|
36
36
|
state: 'NY',
|
37
37
|
country_code: 'us'
|
38
|
-
|
38
|
+
)
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should respond to to_h" do
|
42
42
|
r = address.to_h
|
43
|
-
r.must_be_kind_of Hash
|
44
|
-
r.keys.length.must_equal 7
|
43
|
+
_(r).must_be_kind_of Hash
|
44
|
+
_(r.keys.length).must_equal 7
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should format to US format" do
|
48
|
-
address.to_s.must_equal "Tobias Füncke\n101 Broadway\nNew York City NY 10002\nUnited States of America"
|
48
|
+
_(address.to_s).must_equal "Tobias Füncke\n101 Broadway\nNew York City NY 10002\nUnited States of America"
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should format to FR format" do
|
52
52
|
address.country_code = 'fr'
|
53
53
|
address.city = 'Paris'
|
54
|
-
address.to_s.must_equal "Tobias Füncke\n101 Broadway\n10002 Paris\nFrance"
|
54
|
+
_(address.to_s).must_equal "Tobias Füncke\n101 Broadway\n10002 Paris\nFrance"
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should format to DE format" do
|
58
58
|
address.country_code = 'de'
|
59
59
|
address.city = 'Berlin'
|
60
|
-
address.to_s.must_equal "Tobias Füncke\n101 Broadway\n10002 Berlin\nGermany"
|
60
|
+
_(address.to_s).must_equal "Tobias Füncke\n101 Broadway\n10002 Berlin\nGermany"
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should not print the country if in home country" do
|
64
64
|
Postal.home_country = 'us'
|
65
|
-
address.to_s.must_equal "Tobias Füncke\n101 Broadway\nNew York City NY 10002"
|
65
|
+
_(address.to_s).must_equal "Tobias Füncke\n101 Broadway\nNew York City NY 10002"
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should print the country if not in home country" do
|
69
69
|
Postal.home_country = 'de'
|
70
|
-
address.to_s.must_equal "Tobias Füncke\n101 Broadway\nNew York City NY 10002\nUnited States of America"
|
70
|
+
_(address.to_s).must_equal "Tobias Füncke\n101 Broadway\nNew York City NY 10002\nUnited States of America"
|
71
71
|
end
|
72
72
|
|
73
73
|
it "should use default formatting with state if format for country code is not available" do
|
74
74
|
address.country_code = nil
|
75
|
-
address.to_s.must_equal "Tobias Füncke\n101 Broadway\nNew York City NY 10002"
|
75
|
+
_(address.to_s).must_equal "Tobias Füncke\n101 Broadway\nNew York City NY 10002"
|
76
76
|
end
|
77
77
|
|
78
78
|
it "should use default formatting without state if format for country code is not available" do
|
79
79
|
address.country_code = nil
|
80
80
|
address.state = nil
|
81
|
-
address.to_s.must_equal "Tobias Füncke\n101 Broadway\n10002 New York City"
|
81
|
+
_(address.to_s).must_equal "Tobias Füncke\n101 Broadway\n10002 New York City"
|
82
82
|
end
|
83
83
|
|
84
|
-
# it "should respond to state aliases" do
|
85
|
-
# [:region, :province, :territory, :administrative_area_level_1].each do |alias_name|
|
86
|
-
# address.must_respond_to :"#{alias_name}="
|
87
|
-
# end
|
88
|
-
# end
|
89
|
-
#
|
90
|
-
# it "should respond to city aliases" do
|
91
|
-
# [:locality].each do |alias_name|
|
92
|
-
# address.must_respond_to :"#{alias_name}="
|
93
|
-
# end
|
94
|
-
# end
|
95
|
-
#
|
96
|
-
# it "should respond to zip aliases" do
|
97
|
-
# [:zip_code, :postal_code, :postcode].each do |alias_name|
|
98
|
-
# address.must_respond_to :"#{alias_name}="
|
99
|
-
# end
|
100
|
-
# end
|
101
|
-
|
102
84
|
it "should return html" do
|
103
|
-
address.to_html.must_equal "<p itemscope itemtype=\"http://schema.org/PostalAddress\"><span itemprop=\"name\">Tobias Füncke</span><br><span itemprop=\"streetAddress\">101 Broadway</span><br><span itemprop=\"addressLocality\">New York City</span> <span itemprop=\"addressRegion\">NY</span> <span itemprop=\"postalCode\">10002</span><br><span itemprop=\"addressCountry\">United States of America</span></p>"
|
85
|
+
_(address.to_html).must_equal "<p itemscope itemtype=\"http://schema.org/PostalAddress\"><span itemprop=\"name\">Tobias Füncke</span><br><span itemprop=\"streetAddress\">101 Broadway</span><br><span itemprop=\"addressLocality\">New York City</span> <span itemprop=\"addressRegion\">NY</span> <span itemprop=\"postalCode\">10002</span><br><span itemprop=\"addressCountry\">United States of America</span></p>"
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should return html with default formatting" do
|
89
|
+
address.country_code = nil
|
90
|
+
address.state = nil
|
91
|
+
_(address.to_html).must_equal "<p itemscope itemtype=\"http://schema.org/PostalAddress\"><span itemprop=\"name\">Tobias Füncke</span><br><span itemprop=\"streetAddress\">101 Broadway</span><br><span itemprop=\"postalCode\">10002</span> <span itemprop=\"addressLocality\">New York City</span></p>"
|
104
92
|
end
|
105
93
|
|
106
94
|
it "should return custom html" do
|
107
|
-
address.to_html(tag: :section, itemprop: 'address').must_equal "<section itemprop=\"address\" itemscope itemtype=\"http://schema.org/PostalAddress\"><span itemprop=\"name\">Tobias Füncke</span><br><span itemprop=\"streetAddress\">101 Broadway</span><br><span itemprop=\"addressLocality\">New York City</span> <span itemprop=\"addressRegion\">NY</span> <span itemprop=\"postalCode\">10002</span><br><span itemprop=\"addressCountry\">United States of America</span></section>"
|
95
|
+
_(address.to_html(tag: :section, itemprop: 'address')).must_equal "<section itemprop=\"address\" itemscope itemtype=\"http://schema.org/PostalAddress\"><span itemprop=\"name\">Tobias Füncke</span><br><span itemprop=\"streetAddress\">101 Broadway</span><br><span itemprop=\"addressLocality\">New York City</span> <span itemprop=\"addressRegion\">NY</span> <span itemprop=\"postalCode\">10002</span><br><span itemprop=\"addressCountry\">United States of America</span></section>"
|
108
96
|
end
|
97
|
+
|
109
98
|
end
|
110
99
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,28 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postal_address
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Melchert
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-02-15 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: bundler
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
16
15
|
requirements:
|
17
|
-
- - "
|
16
|
+
- - ">="
|
18
17
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
18
|
+
version: '0'
|
20
19
|
type: :development
|
21
20
|
prerelease: false
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
23
22
|
requirements:
|
24
|
-
- - "
|
23
|
+
- - ">="
|
25
24
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
25
|
+
version: '0'
|
27
26
|
- !ruby/object:Gem::Dependency
|
28
27
|
name: rake
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
@@ -40,20 +39,6 @@ dependencies:
|
|
40
39
|
version: '0'
|
41
40
|
- !ruby/object:Gem::Dependency
|
42
41
|
name: minitest
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: 4.7.0
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: 4.7.0
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: turn
|
57
42
|
requirement: !ruby/object:Gem::Requirement
|
58
43
|
requirements:
|
59
44
|
- - ">="
|
@@ -89,11 +74,10 @@ files:
|
|
89
74
|
- postal_address.gemspec
|
90
75
|
- spec/postal_address_spec.rb
|
91
76
|
- spec/spec_helper.rb
|
92
|
-
homepage:
|
77
|
+
homepage: https://github.com/max-power/postal_address
|
93
78
|
licenses:
|
94
79
|
- MIT
|
95
80
|
metadata: {}
|
96
|
-
post_install_message:
|
97
81
|
rdoc_options: []
|
98
82
|
require_paths:
|
99
83
|
- lib
|
@@ -108,11 +92,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
92
|
- !ruby/object:Gem::Version
|
109
93
|
version: '0'
|
110
94
|
requirements: []
|
111
|
-
|
112
|
-
rubygems_version: 2.2.0
|
113
|
-
signing_key:
|
95
|
+
rubygems_version: 3.6.2
|
114
96
|
specification_version: 4
|
115
|
-
summary: International postal address formatting
|
97
|
+
summary: International postal address formatting with HTML Microformats
|
116
98
|
test_files:
|
117
99
|
- spec/postal_address_spec.rb
|
118
100
|
- spec/spec_helper.rb
|