Class: Addressing::AddressFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/addressing/address_format.rb

Overview

Provides address format information for countries.

Address formats define which fields are used, their order, requirements, and formatting rules for postal addresses in different countries.

Examples:

Get address format for Brazil

format = Addressing::AddressFormat.get('BR')
format.used_fields      # => ["given_name", "family_name", ...]
format.required_fields  # => ["address_line1", "locality", ...]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition = {}) ⇒ AddressFormat

Returns a new instance of AddressFormat.



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
# File 'lib/addressing/address_format.rb', line 74

def initialize(definition = {})
  # Validate the presence of required properties.
  [:country_code, :format].each do |required_property|
    if definition[required_property].nil?
      raise ArgumentError, "Missing required property #{required_property}."
    end
  end

  # Add defaults for properties that are allowed to be empty.
  definition = {
    locale: nil,
    local_format: nil,
    required_fields: [],
    uppercase_fields: [],
    default_values: {},
    postal_code_pattern: nil,
    postal_code_prefix: nil,
    subdivision_depth: 0
  }.merge(definition)

  AddressField.assert_all_exist(definition[:required_fields])
  AddressField.assert_all_exist(definition[:uppercase_fields])
  AddressField.assert_all_exist(definition[:default_values].keys)

  @country_code = definition[:country_code]
  @locale = definition[:locale]
  @format = definition[:format]
  @local_format = definition[:local_format]
  @required_fields = definition[:required_fields]
  @uppercase_fields = definition[:uppercase_fields]
  @default_values = definition[:default_values]
  @subdivision_depth = definition[:subdivision_depth]

  if used_fields.include?(AddressField::ADMINISTRATIVE_AREA)
    if definition[:administrative_area_type]
      AdministrativeAreaType.assert_exists(definition[:administrative_area_type])
      @administrative_area_type = definition[:administrative_area_type]
    end
  end

  if used_fields.include?(AddressField::LOCALITY)
    if definition[:locality_type]
      LocalityType.assert_exists(definition[:locality_type])
      @locality_type = definition[:locality_type]
    end
  end

  if used_fields.include?(AddressField::DEPENDENT_LOCALITY)
    if definition[:dependent_locality_type]
      DependentLocalityType.assert_exists(definition[:dependent_locality_type])
      @dependent_locality_type = definition[:dependent_locality_type]
    end
  end

  if used_fields.include?(AddressField::POSTAL_CODE)
    if definition[:postal_code_type]
      PostalCodeType.assert_exists(definition[:postal_code_type])
      @postal_code_type = definition[:postal_code_type]
    end

    @postal_code_pattern = definition[:postal_code_pattern]
    @postal_code_prefix = definition[:postal_code_prefix]
  end
end

Instance Attribute Details

#administrative_area_typeObject (readonly)

Returns the value of attribute administrative_area_type.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def administrative_area_type
  @administrative_area_type
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def country_code
  @country_code
end

#default_valuesObject (readonly)

Returns the value of attribute default_values.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def default_values
  @default_values
end

#dependent_locality_typeObject (readonly)

Returns the value of attribute dependent_locality_type.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def dependent_locality_type
  @dependent_locality_type
end

#formatObject (readonly)

Returns the value of attribute format.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def format
  @format
end

#local_formatObject (readonly)

Returns the value of attribute local_format.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def local_format
  @local_format
end

#localeObject (readonly)

Returns the value of attribute locale.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def locale
  @locale
end

#locality_typeObject (readonly)

Returns the value of attribute locality_type.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def locality_type
  @locality_type
end

#postal_code_patternObject (readonly)

Returns the value of attribute postal_code_pattern.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def postal_code_pattern
  @postal_code_pattern
end

#postal_code_prefixObject (readonly)

Returns the value of attribute postal_code_prefix.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def postal_code_prefix
  @postal_code_prefix
end

#postal_code_typeObject (readonly)

Returns the value of attribute postal_code_type.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def postal_code_type
  @postal_code_type
end

#required_fieldsObject (readonly)

Returns the value of attribute required_fields.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def required_fields
  @required_fields
end

#subdivision_depthObject (readonly)

Returns the value of attribute subdivision_depth.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def subdivision_depth
  @subdivision_depth
end

#uppercase_fieldsObject (readonly)

Returns the value of attribute uppercase_fields.



72
73
74
# File 'lib/addressing/address_format.rb', line 72

def uppercase_fields
  @uppercase_fields
end

Class Method Details

.allObject



31
32
33
34
35
36
# File 'lib/addressing/address_format.rb', line 31

def all
  definitions.map do |country_code, definition|
    definition = process_definition(definition)
    [country_code, new(definition)]
  end.to_h
end

.get(country_code) ⇒ AddressFormat

Gets the address format for the provided country code.

Parameters:

  • country_code (String)

    ISO 3166-1 alpha-2 country code

Returns:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/addressing/address_format.rb', line 19

def get(country_code)
  country_code = country_code.upcase
  @address_formats ||= {}

  unless @address_formats.key?(country_code)
    definition = process_definition(definitions[country_code] || {country_code: country_code})
    @address_formats[country_code] = new(definition)
  end

  @address_formats[country_code]
end

Instance Method Details

#used_fieldsObject

Gets the list of used fields.



140
141
142
143
144
# File 'lib/addressing/address_format.rb', line 140

def used_fields
  @used_fields ||= AddressField.all.filter_map do |key, value|
    value if @format.include?("%" + value)
  end
end

#used_subdivision_fieldsObject

Gets the list of used subdivision fields.



147
148
149
150
151
152
153
154
155
156
# File 'lib/addressing/address_format.rb', line 147

def used_subdivision_fields
  fields = [
    AddressField::ADMINISTRATIVE_AREA,
    AddressField::LOCALITY,
    AddressField::DEPENDENT_LOCALITY
  ]

  # Remove fields not used by the format.
  fields & used_fields
end