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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 709f977bc0f278516ed857f4180eea486b82f8a2
4
- data.tar.gz: bd3e09740ea53e45cfebbecec53dddea5b08a306
2
+ SHA256:
3
+ metadata.gz: 732395d6e01e69eb058f8040e1e4897b00d5362126261ce8d5519308cf6f83bf
4
+ data.tar.gz: abf24032951733529ab117b7b985655183a5fb2d127e84c4f4d9cd405b02afb6
5
5
  SHA512:
6
- metadata.gz: a9584eae3902a14d69ee379d21c32daeae08c31d552ee50f9910aa14b2561652b03504cbf6c9b816d7d0f1dbd406612fa9ac7300040db332bd2ec579e73adb04
7
- data.tar.gz: ff909cfda68bf7bafa7486e3ce53b7bf74d8c0422073eaa39fe2da337d21c21f64d52a5d6b972674517cc0ed668855f82b66f49acac0efe97659d8aae5448bb5
6
+ metadata.gz: 25f0707107ac25f08e63cda2a703b81e06230113eb3e5fb197f4dc6396c24c21efc0fc204aa6a6faf4f5837c865d11efeca059e2c3c77d3daa69835c49774ac7
7
+ data.tar.gz: 7727b70db57733a9edc17410324278c6457314831b85c445a5cf0377ac89b1d71c51f29dc22c6a759267415ccca4cbfff2a0ec5fc944d5467af74b0845bc122e
data/.travis.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
- - 1.9.3
3
+ - 2.2.0
4
+ - 2.4.2
data/README.md CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  International postal address formatting with HTML Microformats.
4
4
 
5
- [![Build Status](https://travis-ci.org/max-power/postal_address.png)](https://travis-ci.org/max-power/postal_address)
6
5
 
7
6
  ## Installation
8
7
 
@@ -1,31 +1,36 @@
1
1
  module Postal
2
2
  class Address
3
- Fields = [:recipient, :street, :zip, :state, :city, :country, :country_code]
3
+ Fields = %i(recipient street zip state city country country_code).freeze
4
4
 
5
- attr_accessor *Fields
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
- @country ||= Postal.country_names[country_code]
17
+ Postal.country_names[country_code]
13
18
  end
14
19
 
15
- def initialize(attrs={})
16
- attrs.each { |k,v| public_send(:"#{k}=", v) if respond_to?(:"#{k}=") } if attrs
20
+ def values
21
+ Fields.map(&method(:public_send))
17
22
  end
18
23
 
19
24
  def to_h
20
- Fields.each_with_object({}) { |field, hash| hash[field] = public_send(field) }
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(params={})
28
- AddressFormatter::HTML.new(to_h).render(params)
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
- Schema = 'http://schema.org/PostalAddress'
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
- content_tag((params.delete(:tag) || :p), super.gsub("\n", "<br>"), params.merge(itemscope: nil, itemtype: Schema))
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.each_with_object({}) do |(field, value), hash|
22
- hash[field] = value && content_tag(:span, value, itemprop: ItemProp[field])
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 content_tag(tag, content='', attrs={})
27
- "<#{tag}#{tag_attributes(attrs)}>#{content}</#{tag}>"
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}"] : k }.unshift('').join(' ')
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
- @address[:country] = nil if Postal.home_country == @address[:country_code]
17
- @address
17
+ address[:country] = nil if home_country?
18
+ address
18
19
  end
19
20
 
20
21
  def address_format
21
- Postal.address_formats[@address[:country_code]] || default_address_format
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[@address[:state] ? 'us' : 'de']
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
@@ -1,3 +1,3 @@
1
1
  module Postal
2
- VERSION = "0.9.3"
2
+ VERSION = "0.9.6"
3
3
  end
@@ -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
@@ -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", "~> 1.3"
21
+ spec.add_development_dependency "bundler"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "minitest", '~> 4.7.0'
24
- spec.add_development_dependency "turn"
23
+ spec.add_development_dependency "minitest"
25
24
  end
@@ -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 = 'de'
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 read/write home country and convert to string and lower case" do
21
- Postal.home_country = :DE
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
@@ -1,9 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require 'rubygems'
3
3
  require 'bundler/setup'
4
-
5
4
  require 'minitest/autorun'
6
5
  require 'minitest/spec'
7
- require 'turn/autorun'
8
-
9
- require 'postal_address'
6
+ require 'postal_address'
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.3
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: 2014-01-20 00:00:00.000000000 Z
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: '1.3'
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: '1.3'
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
- rubyforge_project:
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