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.
-
#district ⇒ Object
readonly
Returns the value of attribute district.
-
#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.
-
#subdistrict ⇒ Object
readonly
Returns the value of attribute subdistrict.
-
#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, district: nil, subdistrict: 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, district: nil, subdistrict: nil) ⇒ Address
Returns a new instance of Address.
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 |
# File 'lib/worldwide/address.rb', line 24 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, district: nil, subdistrict: 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 @district = district @subdistrict = subdistrict 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 |
#district ⇒ Object (readonly)
Returns the value of attribute district.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def district @district 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 |
#subdistrict ⇒ Object (readonly)
Returns the value of attribute subdistrict.
7 8 9 |
# File 'lib/worldwide/address.rb', line 7 def subdistrict @subdistrict 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
156 157 158 159 160 161 162 163 |
# File 'lib/worldwide/address.rb', line 156 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
165 166 167 168 169 170 171 172 |
# File 'lib/worldwide/address.rb', line 165 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
60 61 62 |
# File 'lib/worldwide/address.rb', line 60 def errors AddressValidator.errors(self) end |
#format(additional_lines: [], excluded_fields: []) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/worldwide/address.rb', line 64 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
77 78 79 |
# File 'lib/worldwide/address.rb', line 77 def normalize(autocorrect_level: 1) dup.normalize!(autocorrect_level: autocorrect_level) end |
#normalize!(autocorrect_level: 1) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/worldwide/address.rb', line 81 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
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 |
# File 'lib/worldwide/address.rb', line 92 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
174 175 176 177 178 179 |
# File 'lib/worldwide/address.rb', line 174 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
181 182 183 184 185 186 |
# File 'lib/worldwide/address.rb', line 181 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 |