This repository has been archived by the owner on Jun 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 545
/
location.rb
166 lines (140 loc) · 4.49 KB
/
location.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module ActiveShipping #:nodoc:
class Location
ADDRESS_TYPES = %w(residential commercial po_box)
ATTRIBUTE_ALIASES = {
name: [:name],
country: [:country_code, :country],
postal_code: [:postal_code, :zip, :postal],
province: [:province_code, :state_code, :territory_code, :region_code, :province, :state, :territory, :region],
city: [:city, :town],
address1: [:address1, :address, :street],
address2: [:address2],
address3: [:address3],
phone: [:phone, :phone_number],
fax: [:fax, :fax_number],
email: [:email],
address_type: [:address_type],
company_name: [:company, :company_name],
}.freeze
attr_reader :options,
:country,
:postal_code,
:province,
:city,
:name,
:address1,
:address2,
:address3,
:phone,
:fax,
:email,
:address_type,
:company_name
alias_method :zip, :postal_code
alias_method :postal, :postal_code
alias_method :state, :province
alias_method :territory, :province
alias_method :region, :province
alias_method :company, :company_name
def initialize(options = {})
@country = if options[:country].nil? || options[:country].is_a?(ActiveUtils::Country)
options[:country]
else
ActiveUtils::Country.find(options[:country])
end
@postal_code = options[:postal_code] || options[:postal] || options[:zip]
@province = options[:province] || options[:state] || options[:territory] || options[:region]
@city = options[:city]
@name = options[:name]
@address1 = options[:address1]
@address2 = options[:address2]
@address3 = options[:address3]
@phone = options[:phone]
@fax = options[:fax]
@email = options[:email]
@company_name = options[:company_name] || options[:company]
self.address_type = options[:address_type]
end
def self.from(object, options = {})
return object if object.is_a?(ActiveShipping::Location)
attributes = {}
hash_access = object.respond_to?(:[])
ATTRIBUTE_ALIASES.each do |attribute, aliases|
aliases.detect do |sym|
value = object[sym] if hash_access
if !value &&
object.respond_to?(sym) &&
(!hash_access || !Hash.public_instance_methods.include?(sym))
value = object.send(sym)
end
attributes[attribute] = value if value
end
end
attributes.delete(:address_type) unless ADDRESS_TYPES.include?(attributes[:address_type].to_s)
new(attributes.update(options))
end
def country_code(format = :alpha2)
@country.nil? ? nil : @country.code(format).value
end
def residential?
@address_type == 'residential'
end
def commercial?
@address_type == 'commercial'
end
def po_box?
@address_type == 'po_box'
end
def unknown?
country_code == 'ZZ'
end
def address_type=(value)
return unless value.present?
raise ArgumentError.new("address_type must be one of #{ADDRESS_TYPES.join(', ')}") unless ADDRESS_TYPES.include?(value.to_s)
@address_type = value.to_s
end
def to_hash
{
country: country_code,
postal_code: postal_code,
province: province,
city: city,
name: name,
address1: address1,
address2: address2,
address3: address3,
phone: phone,
fax: fax,
email: email,
address_type: address_type,
company_name: company_name
}
end
def to_s
prettyprint.gsub(/\n/, ' ')
end
def prettyprint
chunks = [@name, @address1, @address2, @address3]
chunks << [@city, @province, @postal_code].reject(&:blank?).join(', ')
chunks << @country
chunks.reject(&:blank?).join("\n")
end
def inspect
string = prettyprint
string << "\nPhone: #{@phone}" unless @phone.blank?
string << "\nFax: #{@fax}" unless @fax.blank?
string << "\nEmail: #{@email}" unless @email.blank?
string
end
# Returns the postal code as a properly formatted Zip+4 code, e.g. "77095-2233"
def zip_plus_4
"#{$1}-#{$2}" if /(\d{5})-?(\d{4})/ =~ @postal_code
end
def address2_and_3
[address2, address3].reject(&:blank?).join(", ")
end
def ==(other)
to_hash == other.to_hash
end
end
end