Class: Worldwide::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/worldwide/address.rb

Constant Summary collapse

RESERVED_DELIMITER =
"\u2060"
COUNTRIES_USING_REVERSE_ADDRESS_ORDER =
["JP"]

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#address1Object (readonly)

Returns the value of attribute address1.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def address1
  @address1
end

#address2Object (readonly)

Returns the value of attribute address2.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def address2
  @address2
end

#cityObject (readonly)

Returns the value of attribute city.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def city
  @city
end

#companyObject (readonly)

Returns the value of attribute company.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def company
  @company
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def country_code
  @country_code
end

#districtObject (readonly)

Returns the value of attribute district.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def district
  @district
end

#first_nameObject (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_nameObject (readonly)

Returns the value of attribute last_name.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def last_name
  @last_name
end

#line2Object (readonly)

Returns the value of attribute line2.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def line2
  @line2
end

#neighborhoodObject (readonly)

Returns the value of attribute neighborhood.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def neighborhood
  @neighborhood
end

#phoneObject (readonly)

Returns the value of attribute phone.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def phone
  @phone
end

#province_codeObject (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_nameObject (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_numberObject (readonly)

Returns the value of attribute street_number.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def street_number
  @street_number
end

#subdistrictObject (readonly)

Returns the value of attribute subdistrict.



7
8
9
# File 'lib/worldwide/address.rb', line 7

def subdistrict
  @subdistrict
end

#zipObject (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_address1Object



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_address2Object



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

#errorsObject



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_address1Object



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_address2Object



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

#valid?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/worldwide/address.rb', line 152

def valid?
  Util.blank?(errors)
end