postal_address 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
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: 2ab9634073fe121170936b837b4c697a01adcc00e05edc95b13fc0159aa3992b
4
+ data.tar.gz: 9d35fe02a26eb3de91a57042e5a2d82b9e93b831630bd464992e75893d216446
5
5
  SHA512:
6
- metadata.gz: a9584eae3902a14d69ee379d21c32daeae08c31d552ee50f9910aa14b2561652b03504cbf6c9b816d7d0f1dbd406612fa9ac7300040db332bd2ec579e73adb04
7
- data.tar.gz: ff909cfda68bf7bafa7486e3ce53b7bf74d8c0422073eaa39fe2da337d21c21f64d52a5d6b972674517cc0ed668855f82b66f49acac0efe97659d8aae5448bb5
6
+ metadata.gz: a03753a4063169367b6e655bc1acb0a5a95623f7b0ff076c1a1b178eb80cbf4e2db0a9a904641d34b96b86ae6934a5c21edcd2e688fc6ac37bd45c95dad4829c
7
+ data.tar.gz: 346d5f9abc08c87615a2a2fa046fd5be81fce5349ffc315861c180b2c644848c649c15afb0a76514264a2fc176fad8509c879cee7ff365d987cc8f3cd0dfb06c
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,30 +1,35 @@
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(**params)
29
+ AddressFormatter::Text.new(to_h).render(params)
25
30
  end
26
31
 
27
- def to_html(params={})
32
+ def to_html(**params)
28
33
  AddressFormatter::HTML.new(to_h).render(params)
29
34
  end
30
35
  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.4"
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
@@ -1,5 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require 'spec_helper'
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'minitest/autorun'
5
+ require 'minitest/spec'
6
+ require 'postal_address'
3
7
 
4
8
  describe Postal do
5
9
  it "should load the formats" do
@@ -81,24 +85,6 @@ describe Postal do
81
85
  address.to_s.must_equal "Tobias Füncke\n101 Broadway\n10002 New York City"
82
86
  end
83
87
 
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
88
  it "should return html" do
103
89
  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>"
104
90
  end
metadata CHANGED
@@ -1,29 +1,29 @@
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.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Melchert
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-20 00:00:00.000000000 Z
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -40,20 +40,6 @@ dependencies:
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  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
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - ">="
@@ -89,11 +75,11 @@ files:
89
75
  - postal_address.gemspec
90
76
  - spec/postal_address_spec.rb
91
77
  - spec/spec_helper.rb
92
- homepage: ''
78
+ homepage: https://github.com/max-power/postal_address
93
79
  licenses:
94
80
  - MIT
95
81
  metadata: {}
96
- post_install_message:
82
+ post_install_message:
97
83
  rdoc_options: []
98
84
  require_paths:
99
85
  - lib
@@ -108,11 +94,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
94
  - !ruby/object:Gem::Version
109
95
  version: '0'
110
96
  requirements: []
111
- rubyforge_project:
112
- rubygems_version: 2.2.0
113
- signing_key:
97
+ rubygems_version: 3.4.6
98
+ signing_key:
114
99
  specification_version: 4
115
- summary: International postal address formatting
100
+ summary: International postal address formatting with HTML Microformats
116
101
  test_files:
117
102
  - spec/postal_address_spec.rb
118
103
  - spec/spec_helper.rb