Class: Worldwide::Address
- Inherits:
-
Object
- Object
- Worldwide::Address
- Defined in:
- lib/worldwide/address.rb
Constant Summary collapse
- RESERVED_DELIMITER =
"\u2060"
- COUNTRIES_USING_REVERSE_ADDRESS_ORDER =
["JP"]
Instance Attribute Summary collapse
-
#address1 ⇒ Object
readonly
Returns the value of attribute address1.
-
#address2 ⇒ Object
readonly
Returns the value of attribute address2.
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#company ⇒ Object
readonly
Returns the value of attribute company.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#first_name ⇒ Object
readonly
Returns the value of attribute first_name.
-
#last_name ⇒ Object
readonly
Returns the value of attribute last_name.
-
#line2 ⇒ Object
readonly
Returns the value of attribute line2.
-
#neighborhood ⇒ Object
readonly
Returns the value of attribute neighborhood.
-
#phone ⇒ Object
readonly
Returns the value of attribute phone.
-
#province_code ⇒ Object
readonly
Returns the value of attribute province_code.
-
#street_name ⇒ Object
readonly
Returns the value of attribute street_name.
-
#street_number ⇒ Object
readonly
Returns the value of attribute street_number.
-
#zip ⇒ Object
readonly
Returns the value of attribute zip.
Instance Method Summary collapse
- #concatenate_address1 ⇒ Object
- #concatenate_address2 ⇒ Object
- #errors ⇒ Object
- #format(additional_lines: [], excluded_fields: []) ⇒ Object
-
#initialize(first_name: nil, last_name: nil, company: nil, address1: nil, address2: nil, zip: nil, city: nil, province_code: nil, country_code: "ZZ", phone: nil, street_name: nil, street_number: nil, line2: nil, neighborhood: nil) ⇒ Address
constructor
A new instance of Address.
- #normalize(autocorrect_level: 1) ⇒ Object
- #normalize!(autocorrect_level: 1) ⇒ Object
- #single_line(additional_lines: [], excluded_fields: []) ⇒ Object
- #split_address1 ⇒ Object
- #split_address2 ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(first_name: nil, last_name: nil, company: nil, address1: nil, address2: nil, zip: nil, city: nil, province_code: nil, country_code: "ZZ", phone: nil, street_name: nil, street_number: nil, line2: nil, neighborhood: nil) ⇒ Address
Returns a new instance of Address.
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 |
# File 'lib/worldwide/address.rb', line 22 def initialize( first_name: nil, last_name: nil, company: nil, address1: nil, address2: nil, zip: nil, city: nil, province_code: nil, country_code: "ZZ", # Unknown region phone: nil, street_name: nil, street_number: nil, line2: nil, neighborhood: nil ) @first_name = first_name @last_name = last_name @company = company @address1 = address1 @address2 = address2 @zip = zip @city = city @province_code = province_code&.to_s&.upcase @country_code = country_code.to_s.upcase @phone = phone @street_name = street_name @street_number = street_number @line2 = line2 @neighborhood = neighborhood end |
Instance Attribute Details
#address1 ⇒ Object (readonly)
Returns the value of attribute address1.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def address1 @address1 end |
#address2 ⇒ Object (readonly)
Returns the value of attribute address2.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def address2 @address2 end |
#city ⇒ Object (readonly)
Returns the value of attribute city.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def city @city end |
#company ⇒ Object (readonly)
Returns the value of attribute company.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def company @company end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def country_code @country_code end |
#first_name ⇒ Object (readonly)
Returns the value of attribute first_name.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def first_name @first_name end |
#last_name ⇒ Object (readonly)
Returns the value of attribute last_name.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def last_name @last_name end |
#line2 ⇒ Object (readonly)
Returns the value of attribute line2.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def line2 @line2 end |
#neighborhood ⇒ Object (readonly)
Returns the value of attribute neighborhood.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def neighborhood @neighborhood end |
#phone ⇒ Object (readonly)
Returns the value of attribute phone.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def phone @phone end |
#province_code ⇒ Object (readonly)
Returns the value of attribute province_code.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def province_code @province_code end |
#street_name ⇒ Object (readonly)
Returns the value of attribute street_name.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def street_name @street_name end |
#street_number ⇒ Object (readonly)
Returns the value of attribute street_number.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def street_number @street_number end |
#zip ⇒ Object (readonly)
Returns the value of attribute zip.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def zip @zip end |
Instance Method Details
#concatenate_address1 ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/worldwide/address.rb', line 150 def concatenate_address1 additional_fields = region.combined_address_format.dig(script_from_field("address1"), "address1") || [] additional_field_keys = additional_fields.map { |field| field["key"] } return address1 if field_values(additional_field_keys).empty? concatenate_fields(additional_fields) end |
#concatenate_address2 ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/worldwide/address.rb', line 159 def concatenate_address2 additional_fields = region.combined_address_format.dig(script_from_field("address2"), "address2") || [] additional_field_keys = additional_fields.map { |field| field["key"] } return address2 if field_values(additional_field_keys).empty? concatenate_fields(additional_fields) end |
#errors ⇒ Object
54 55 56 |
# File 'lib/worldwide/address.rb', line 54 def errors AddressValidator.errors(self) end |
#format(additional_lines: [], excluded_fields: []) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/worldwide/address.rb', line 58 def format(additional_lines: [], excluded_fields: []) normalized_excluded_fields = normalize_field_names(excluded_fields) lines = build_filled_address_data( additional_lines: additional_lines, excluded_fields: normalized_excluded_fields, ) strip_extra_chars(lines: lines, excluded_fields: normalized_excluded_fields).map do |line| line.join(" ") end end |
#normalize(autocorrect_level: 1) ⇒ Object
71 72 73 |
# File 'lib/worldwide/address.rb', line 71 def normalize(autocorrect_level: 1) dup.normalize!(autocorrect_level: autocorrect_level) end |
#normalize!(autocorrect_level: 1) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/worldwide/address.rb', line 75 def normalize!(autocorrect_level: 1) @country_code = normalize_country_code(autocorrect_level: autocorrect_level) @city = normalize_city @zip = normalize_zip @province_code = normalize_province_code || @province_code @phone = normalize_phone self end |
#single_line(additional_lines: [], excluded_fields: []) ⇒ Object
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 |
# File 'lib/worldwide/address.rb', line 86 def single_line(additional_lines: [], excluded_fields: []) normalized_excluded_fields = normalize_field_names(excluded_fields) lines = build_address_format_array( additional_lines: additional_lines, excluded_fields: normalized_excluded_fields, ) if "BR" == country_code lines = adjust_single_line_format_for_brazil(lines: lines, excluded_fields: normalized_excluded_fields) end filled = fill_in_lines(lines: lines, address_data: build_formatted_address_data) stripped = strip_extra_chars(lines: filled, excluded_fields: normalized_excluded_fields) # Fallback to showing the country name by itself, in case no other information ended up being displayed if stripped.empty? stripped = [[Worldwide.region(code: country_code).full_name]] end fields = stripped.flatten.filter_map do |field| field unless blank?(field) end locale_str = I18n.locale.to_s.downcase # A handful of countries show their address in reverse order. # When formatting a mailing address for that country, we display in the country's order. # But, when constructing a single-line address like "Tokyo, Japan", we'd rather have things # in the "natural order" for the active locale. # So, in those cases where format_address returns the fields in reverse order, let's # unflip them before we get to producing our output. That way, we'll show "Tokyo, Japan" # for :en, instead of showing "Japan, Tokyo". if COUNTRIES_USING_REVERSE_ADDRESS_ORDER.include?(country_code) && locale_str.start_with?("ja", "zh") fields.reverse! end line = if locale_str.downcase.start_with?("ja") result = if fields.size < 2 || !lines.any? { |line| line.include?("{country}") } fields.reverse.join("") else fields.reverse.insert(1, ":").join("") end if Worldwide::Scripts.identify(text: result).include?(:Latin) result else result.delete(" \u{3000}") end elsif locale_str.start_with?("zh") fields.reverse.join("") else Worldwide::Lists.format(fields, join: :narrow) end line.strip end |
#split_address1 ⇒ Object
168 169 170 171 172 173 |
# File 'lib/worldwide/address.rb', line 168 def split_address1 additional_fields = region.combined_address_format.dig(script_from_string(address1), "address1") || [] number_of_fields = additional_fields.size split_fields_arr = address1&.split(RESERVED_DELIMITER, number_of_fields) || [] split_fields(additional_fields, split_fields_arr) end |
#split_address2 ⇒ Object
175 176 177 178 179 180 |
# File 'lib/worldwide/address.rb', line 175 def split_address2 additional_fields = region.combined_address_format.dig(script_from_string(address2), "address2") || [] number_of_fields = additional_fields.size split_fields_arr = address2&.split(RESERVED_DELIMITER, number_of_fields) || [] split_fields(additional_fields, split_fields_arr) end |