postal_address 0.0.5 → 0.1.1

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
2
  SHA1:
3
- metadata.gz: 2499c8eb67851e9f520f4617be1237950cd8d1f1
4
- data.tar.gz: 9ad9cb07b425aa9bb65953658811e292b8283ec9
3
+ metadata.gz: d917f51e33a1a55dcb4b1223df2626b3df0b5ec5
4
+ data.tar.gz: 19521b23dd055ac878ed78e1e719c3bc927ca911
5
5
  SHA512:
6
- metadata.gz: 1cc7ca58f44d13de08c8965b43b276a5c01ed95ab1b9e5838132b359bff4eb432128fa06a08e43863e78b0a2963276f6f133a0f477a68f4a517f898ddcb8a173
7
- data.tar.gz: fccf1064bc4ad2348a1ae747d56d22c1872fe4e06201343055b02f856d4d8d2da976d4d1c6c955f1a6a797099364efeaf75d89fba1734217f8588a154dc29f93
6
+ metadata.gz: 7fb11ea6efacab7cd36459679d643a52ed350ff34f3d3da9dde0cd432cfee89278c9592eed0127aab2d23a2d6c0a3c257080929a60cb5f98ea92e6fdc1235bee
7
+ data.tar.gz: 835d1c41d90324bd5b167f1bda981561b3f87048dc73aa6d8482251390c5c74a235a0f85a0bcac3b897d18a907c380fb8022bf599dd0a3eaf0a329ae2dd30baa
data/README.md CHANGED
@@ -29,14 +29,14 @@ Or install it yourself as:
29
29
  country_code: 'us'
30
30
  }
31
31
 
32
- PostalAddress.new(address).to_s
32
+ Postal::Address.new(address).to_s
33
33
 
34
34
  Tobias Füncke
35
35
  101 Broadway
36
36
  New York City NY 10002
37
37
  United States of America
38
38
 
39
- PostalAddress.new(address).to_html
39
+ Postal::Address.new(address).to_html
40
40
 
41
41
  <p itemscope itemtype="http://schema.org/PostalAddress">
42
42
  <span itemprop="name">Tobias Füncke</span>
@@ -53,9 +53,9 @@ Or install it yourself as:
53
53
 
54
54
  Set a home country (country names are not display for those addresses)
55
55
 
56
- PostalAddress.home_country = 'us'
56
+ Postal.home_country = 'us'
57
57
 
58
- p = PostalAddress.new(address)
58
+ p = Postal::Address.new(address)
59
59
  p.to_html(tag: 'section', itemprop: 'address')
60
60
 
61
61
  <section itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
@@ -67,6 +67,14 @@ Set a home country (country names are not display for those addresses)
67
67
  <span itemprop="addressRegion">NY</span>
68
68
  <span itemprop="postalCode">10002</span>
69
69
  </section>
70
+
71
+
72
+ Standalone AddressFormatter usage:
73
+
74
+ Postal::AddressFormatter::Text.new(address).render
75
+
76
+ Postal::AddressFormatter::HTML.new(address).render(tag: 'section', itemprop: 'address')
77
+
70
78
 
71
79
  ## Contributing
72
80
 
@@ -1,3 +1,6 @@
1
+ # http://www.addressdoctor.com/en/countries-data/address-formats.html
2
+
3
+
1
4
  ---
2
5
  ae: "%{recipient}\n%{street}\n%{zip} %{city}\n%{country}"
3
6
  ar: "%{recipient}\n%{street}\n%{zip} %{city}\n%{state}\n%{country}"
@@ -1,17 +1,37 @@
1
+ require "yaml"
1
2
  require "postal_address/version"
2
- require "postal_address/class_methods"
3
- require "postal_address/instance_methods"
3
+ require "postal_address/address"
4
+ require "postal_address/formatters/text"
5
+ require "postal_address/formatters/html"
4
6
 
5
- class PostalAddress
6
- extend ClassMethods
7
- include InstanceMethods
7
+ module Postal
8
+ class << self
9
+ attr_reader :home_country
10
+
11
+ def home_country=(code)
12
+ @home_country = sanitize(code)
13
+ end
14
+
15
+ def home_country?(code)
16
+ home_country == code
17
+ end
18
+
19
+ def sanitize(code)
20
+ code && code.to_s.downcase
21
+ end
22
+
23
+ def address_formats
24
+ @address_formats ||= load_yaml('address_formats')
25
+ end
8
26
 
9
- # define the postal address attributes and aliases for easier assignment
10
- attribute :recipient, itemprop: 'name'
11
- attribute :street, itemprop: 'streetAddress', alias: [:street_address]
12
- attribute :zip, itemprop: 'postalCode', alias: [:zip_code, :postal_code, :postcode]
13
- attribute :state, itemprop: 'addressRegion', alias: [:region, :province, :territory, :administrative_area_level_1]
14
- attribute :city, itemprop: 'addressLocality', alias: [:locality]
15
- attribute :country_code, itemprop: 'addressCountry', alias: [:country_id], writer: false
16
- attribute :country, itemprop: 'addressCountry', writer: false, reader: false
27
+ def country_names
28
+ @country_names ||= load_yaml('country_names')
29
+ end
30
+
31
+ private
32
+
33
+ def load_yaml(filename)
34
+ YAML.load_file(File.expand_path("../data/#{filename}.yml", File.dirname(__FILE__)))
35
+ end
36
+ end
17
37
  end
@@ -0,0 +1,41 @@
1
+ module Postal
2
+ class Address
3
+ Fields = [:recipient, :street, :zip, :state, :city, :country, :country_code]
4
+
5
+ attr_accessor *Fields
6
+
7
+ def initialize(attrs={})
8
+ attrs.each do |attr, value|
9
+ self.public_send("#{attr}=", value) if self.respond_to?("#{attr}=")
10
+ end if attrs
11
+ end
12
+
13
+ def country_code=(code)
14
+ @country_code = Postal.sanitize(code)
15
+ end
16
+
17
+ def country
18
+ @country ||= Postal.country_names[country_code]
19
+ end
20
+
21
+ def to_h
22
+ attributes
23
+ end
24
+
25
+ def to_s
26
+ AddressFormatter::Text.new(attributes).render
27
+ end
28
+
29
+ def to_html(params={})
30
+ AddressFormatter::HTML.new(attributes).render(params)
31
+ end
32
+
33
+ private
34
+
35
+ def attributes
36
+ Fields.each_with_object({}) do |key, hash|
37
+ hash[key] = public_send(key)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,32 @@
1
+ module Postal
2
+ module AddressFormatter
3
+ class HTML < Text
4
+ Schema = 'http://schema.org/PostalAddress'
5
+ ItemProp = {
6
+ recipient: 'name',
7
+ street: 'streetAddress',
8
+ zip: 'postalCode',
9
+ state: 'addressRegion',
10
+ city: 'addressLocality',
11
+ country: 'addressCountry'
12
+ }
13
+
14
+ def render(params={})
15
+ content_tag((params.delete(:tag) || :p), super.gsub("\n", "<br>"), params.merge(itemscope: nil, itemtype: Schema))
16
+ end
17
+
18
+ private
19
+
20
+ def address_data
21
+ super.each_with_object({}) do |(key, value), hash|
22
+ hash[key] = content_tag(:span, value, itemprop: ItemProp[key])
23
+ end
24
+ end
25
+
26
+ def content_tag(tag, content='', attributes={})
27
+ attrs = attributes.map {|k,v| k.to_s + (v ? "=\"#{v}\"" : '') }.unshift('').join(' ')
28
+ "<#{tag}#{attrs}>#{content}</#{tag}>"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ module Postal
2
+ module AddressFormatter
3
+ class Text
4
+ # expects any hash with the fields names of address
5
+ def initialize(address)
6
+ @address = address
7
+ end
8
+
9
+ def render(params={})
10
+ (address_format % address_data).strip
11
+ end
12
+
13
+ private
14
+
15
+ def address_data
16
+ @address[:country] = nil if Postal.home_country?(@address[:country_code])
17
+ @address
18
+ end
19
+
20
+ def address_format
21
+ Postal.address_formats[@address[:country_code]] || Postal.address_formats[@address[:state] ? 'us' : 'de']
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  class PostalAddress
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "minitest"
23
+ spec.add_development_dependency "minitest", '~> 4.7.0'
24
+ spec.add_development_dependency "turn"
24
25
  spec.add_development_dependency "virtus"
25
26
  end
@@ -3,32 +3,32 @@ require 'spec_helper'
3
3
 
4
4
  describe PostalAddress do
5
5
  it "should load the formats" do
6
- PostalAddress.formats.wont_be :empty?
7
- PostalAddress.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
- PostalAddress.country_names.wont_be :empty?
12
- PostalAddress.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
- PostalAddress.home_country = 'de'
17
- PostalAddress.home_country.must_equal 'de'
16
+ Postal.home_country = 'de'
17
+ Postal.home_country.must_equal 'de'
18
18
  end
19
19
 
20
20
  it "should read/write home country and convert to string and lower case" do
21
- PostalAddress.home_country = :DE
22
- PostalAddress.home_country.must_equal 'de'
21
+ Postal.home_country = :DE
22
+ Postal.home_country.must_equal 'de'
23
23
  end
24
24
 
25
25
  describe "Instance" do
26
26
  before {
27
- PostalAddress.home_country = nil
27
+ Postal.home_country = nil
28
28
  }
29
29
 
30
30
  let(:address) do
31
- PostalAddress.new({
31
+ Postal::Address.new({
32
32
  recipient: 'Tobias Füncke',
33
33
  street: '101 Broadway',
34
34
  city: 'New York City',
@@ -61,12 +61,12 @@ describe PostalAddress do
61
61
  end
62
62
 
63
63
  it "should not print the country if in home country" do
64
- PostalAddress.home_country = 'us'
64
+ Postal.home_country = 'us'
65
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
- PostalAddress.home_country = 'de'
69
+ Postal.home_country = 'de'
70
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
 
@@ -81,29 +81,29 @@ describe PostalAddress do
81
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
- address.must_respond_to :"#{alias_name}="
88
- #address.send(alias_name).must_equal address.state
89
- end
90
- end
91
-
92
- it "should respond to city aliases" do
93
- [:locality].each do |alias_name|
94
- #address.must_respond_to alias_name
95
- address.must_respond_to :"#{alias_name}="
96
- #address.send(alias_name).must_equal address.city
97
- end
98
- end
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
+ # address.must_respond_to :"#{alias_name}="
88
+ # #address.send(alias_name).must_equal address.state
89
+ # end
90
+ # end
99
91
 
100
- it "should respond to zip aliases" do
101
- [:zip_code, :postal_code, :postcode].each do |alias_name|
102
- #address.must_respond_to alias_name
103
- address.must_respond_to :"#{alias_name}="
104
- #address.send(alias_name).must_equal address.zip
105
- end
106
- end
92
+ # it "should respond to city aliases" do
93
+ # [:locality].each do |alias_name|
94
+ # #address.must_respond_to alias_name
95
+ # address.must_respond_to :"#{alias_name}="
96
+ # #address.send(alias_name).must_equal address.city
97
+ # end
98
+ # end
99
+ #
100
+ # it "should respond to zip aliases" do
101
+ # [:zip_code, :postal_code, :postcode].each do |alias_name|
102
+ # #address.must_respond_to alias_name
103
+ # address.must_respond_to :"#{alias_name}="
104
+ # #address.send(alias_name).must_equal address.zip
105
+ # end
106
+ # end
107
107
 
108
108
  it "should return html" do
109
109
  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>"
data/spec/spec_helper.rb CHANGED
@@ -4,4 +4,6 @@ require 'bundler/setup'
4
4
 
5
5
  require 'minitest/autorun'
6
6
  require 'minitest/spec'
7
+ require 'turn/autorun'
8
+
7
9
  require 'postal_address'
@@ -14,7 +14,7 @@ class Address
14
14
  attribute :geolocation, Array
15
15
 
16
16
  def postal_address
17
- PostalAddress.new(self.attributes).to_s
17
+ Postal::Address.new(self.attributes).to_s
18
18
  end
19
19
  end
20
20
 
@@ -33,7 +33,7 @@ describe Address do
33
33
  }
34
34
 
35
35
  it "should work" do
36
- PostalAddress.home_country = 'us'
36
+ Postal.home_country = 'us'
37
37
  address.postal_address.must_equal "Tobias Füncke\n101 Broadway\nNew York City NY 10002"
38
38
  end
39
39
  end
metadata CHANGED
@@ -1,69 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postal_address
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Melchert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-25 00:00:00.000000000 Z
11
+ date: 2014-01-18 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
19
  version: '1.3'
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
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
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
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - '>='
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: virtus
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - '>='
73
+ - - ">="
60
74
  - !ruby/object:Gem::Version
61
75
  version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - '>='
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  description: International postal address formatting
@@ -73,8 +87,8 @@ executables: []
73
87
  extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
- - .gitignore
77
- - .travis.yml
90
+ - ".gitignore"
91
+ - ".travis.yml"
78
92
  - Gemfile
79
93
  - LICENSE.txt
80
94
  - README.md
@@ -82,8 +96,9 @@ files:
82
96
  - data/address_formats.yml
83
97
  - data/country_names.yml
84
98
  - lib/postal_address.rb
85
- - lib/postal_address/class_methods.rb
86
- - lib/postal_address/instance_methods.rb
99
+ - lib/postal_address/address.rb
100
+ - lib/postal_address/formatters/html.rb
101
+ - lib/postal_address/formatters/text.rb
87
102
  - lib/postal_address/version.rb
88
103
  - postal_address.gemspec
89
104
  - spec/postal_address_spec.rb
@@ -99,17 +114,17 @@ require_paths:
99
114
  - lib
100
115
  required_ruby_version: !ruby/object:Gem::Requirement
101
116
  requirements:
102
- - - '>='
117
+ - - ">="
103
118
  - !ruby/object:Gem::Version
104
119
  version: '0'
105
120
  required_rubygems_version: !ruby/object:Gem::Requirement
106
121
  requirements:
107
- - - '>='
122
+ - - ">="
108
123
  - !ruby/object:Gem::Version
109
124
  version: '0'
110
125
  requirements: []
111
126
  rubyforge_project:
112
- rubygems_version: 2.0.3
127
+ rubygems_version: 2.2.0
113
128
  signing_key:
114
129
  specification_version: 4
115
130
  summary: International postal address formatting
@@ -117,4 +132,3 @@ test_files:
117
132
  - spec/postal_address_spec.rb
118
133
  - spec/spec_helper.rb
119
134
  - spec/virtus_integration_spec.rb
120
- has_rdoc:
@@ -1,40 +0,0 @@
1
- require "yaml"
2
-
3
- class PostalAddress
4
- module ClassMethods
5
- attr_reader :home_country
6
-
7
- def home_country=(code)
8
- @home_country = code && code.to_s.downcase
9
- end
10
-
11
- def formats
12
- @formats ||= load_yaml('address_formats')
13
- end
14
-
15
- def country_names
16
- @country_names ||= load_yaml('country_names')
17
- end
18
-
19
- def attributes
20
- @attributes ||= {}
21
- end
22
-
23
- private
24
-
25
- def attribute(name, params={})
26
- attributes[name] = params
27
- attr_reader name unless params[:reader] === false
28
- attr_writer name unless params[:writer] === false
29
- alias_attribute_writer(name, Array(params[:alias])) if params[:alias]
30
- end
31
-
32
- def alias_attribute_writer(original, aliases)
33
- aliases.each { |name| alias_method :"#{name}=", :"#{original}=" }
34
- end
35
-
36
- def load_yaml(filename)
37
- YAML.load_file(File.join(File.dirname(__FILE__), "..", "..", "data", "#{filename}.yml"))
38
- end
39
- end
40
- end
@@ -1,58 +0,0 @@
1
- class PostalAddress
2
- module InstanceMethods
3
- def initialize(params={})
4
- params.each do |attr, value|
5
- self.public_send("#{attr}=", value) if self.respond_to?("#{attr}=")
6
- end if params
7
- end
8
-
9
- def to_h
10
- attributes
11
- end
12
-
13
- def to_s
14
- format(attributes)
15
- end
16
-
17
- def to_html(params={})
18
- address = format(attributes(:html)).gsub("\n", "<br>")
19
- content_tag((params.delete(:tag) || :p), address, params.merge(itemscope: nil, itemtype: 'http://schema.org/PostalAddress'))
20
- end
21
-
22
- def country_code=(code)
23
- @country_code = code && code.to_s.downcase
24
- end
25
-
26
- def country
27
- @country = self.class.country_names[country_code] unless in_home_country?
28
- end
29
-
30
- def in_home_country?
31
- self.class.home_country == country_code
32
- end
33
-
34
- private
35
-
36
- def format(hash)
37
- (address_format % hash).strip
38
- end
39
-
40
- def address_format
41
- self.class.formats[country_code] || self.class.formats[state ? 'us' : 'de']
42
- end
43
-
44
- def attributes(html=false)
45
- self.class.attributes.inject({}) do |hash, (key, attribute)|
46
- value = public_send(key)
47
- value = value && content_tag(:span, value, itemprop: attribute[:itemprop]) if html
48
- hash[key] = value
49
- hash
50
- end
51
- end
52
-
53
- def content_tag(tag, content='', attributes={})
54
- attrs = attributes.map {|k,v| k.to_s + (v ? "=\"#{v}\"" : '') }.unshift('').join(' ')
55
- "<#{tag}#{attrs}>#{content}</#{tag}>"
56
- end
57
- end
58
- end