postal_address 0.9.2 → 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: f83668dafff4ede2c9d49c1c711cb5867167bd44
4
- data.tar.gz: 535bb43703e2f420c0f5177973ae95845d0270dc
2
+ SHA256:
3
+ metadata.gz: 2ab9634073fe121170936b837b4c697a01adcc00e05edc95b13fc0159aa3992b
4
+ data.tar.gz: 9d35fe02a26eb3de91a57042e5a2d82b9e93b831630bd464992e75893d216446
5
5
  SHA512:
6
- metadata.gz: dad66c7a372952a9ba32058e3a9bddbbe1f5c86704fd68d358ea304f3b6ce533025c5b06083f190ab523d6566f235a83f8ffacab5abea4421f2fd20f04ab0352
7
- data.tar.gz: dc0a4a98c5b98429e355bbc43c765544f62ddd304ff7d115d2e67fbba19365ae2bab261cc9e50b9227a9f7f4ac51bace01f1f61197d6f5003e12e37eab29d9c5
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,41 +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
-
7
- alias_method :locality=, :city=
8
- alias_method :zip_code=, :zip=
9
- alias_method :postal_code=, :zip=
10
- alias_method :postcode=, :zip=
11
- alias_method :region=, :state=
12
- alias_method :province=, :state=
13
- alias_method :territory=, :state=
14
- alias_method :administrative_area_level_1=, :state=
15
-
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
+
16
12
  def country_code=(code)
17
13
  @country_code = Postal.sanitize(code)
18
14
  end
19
15
 
20
16
  def country
21
- @country ||= Postal.country_names[country_code]
17
+ Postal.country_names[country_code]
22
18
  end
23
19
 
24
- def initialize(attrs={})
25
- attrs.each do |field, value|
26
- self.public_send(:"#{field}=", value) if self.respond_to?(:"#{field}=")
27
- end if attrs
20
+ def values
21
+ Fields.map(&method(:public_send))
28
22
  end
29
23
 
30
24
  def to_h
31
- Fields.each_with_object({}) { |field, hash| hash[field] = public_send(field) }
25
+ Fields.zip(values).to_h
32
26
  end
33
27
 
34
- def to_s
35
- AddressFormatter::Text.new(to_h).render
28
+ def to_s(**params)
29
+ AddressFormatter::Text.new(to_h).render(params)
36
30
  end
37
31
 
38
- def to_html(params={})
32
+ def to_html(**params)
39
33
  AddressFormatter::HTML.new(to_h).render(params)
40
34
  end
41
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,7 +1,8 @@
1
1
  module Postal
2
2
  module AddressFormatter
3
3
  class Text
4
- # expects any hash with the fields names of address
4
+ attr_reader :address
5
+ # expects any hash with the 7 keys from Postal::Address::Fields
5
6
  def initialize(address)
6
7
  @address = address
7
8
  end
@@ -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.2"
2
+ VERSION = "0.9.4"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require "yaml"
2
+ require "pathname"
2
3
  require "postal_address/version"
3
4
  require "postal_address/address"
4
5
  require "postal_address/formatters/text"
@@ -17,17 +18,17 @@ module Postal
17
18
  end
18
19
 
19
20
  def address_formats
20
- @address_formats ||= load_yaml('address_formats')
21
+ @address_formats ||= YAML.load_file(path_for(:address_formats), aliases: true)
21
22
  end
22
23
 
23
24
  def country_names
24
- @country_names ||= load_yaml('country_names')
25
+ @country_names ||= YAML.load_file(path_for(:country_names), aliases: true)
25
26
  end
26
27
 
27
28
  private
28
29
 
29
- def load_yaml(filename)
30
- YAML.load_file(File.expand_path("../data/#{filename}.yml", File.dirname(__FILE__)))
30
+ def path_for(filename)
31
+ Pathname.new(__FILE__).dirname.parent + "data" + "#{filename}.yml"
31
32
  end
32
33
  end
33
34
  end
@@ -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.2
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-19 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