Class: AtlasEngine::AddressValidation::LogEmitter

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
LogHelper
Defined in:
app/models/atlas_engine/address_validation/log_emitter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LogHelper

#log_error, #log_info, #log_warn

Constructor Details

#initialize(address:, result:) ⇒ LogEmitter

Returns a new instance of LogEmitter.



25
26
27
28
29
# File 'app/models/atlas_engine/address_validation/log_emitter.rb', line 25

def initialize(address:, result:)
  @address = address
  @result = result
  @fields = [:country, :province, :zip, :city, :street, :phone]
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



11
12
13
# File 'app/models/atlas_engine/address_validation/log_emitter.rb', line 11

def address
  @address
end

#fieldsObject (readonly)

Returns the value of attribute fields.



17
18
19
# File 'app/models/atlas_engine/address_validation/log_emitter.rb', line 17

def fields
  @fields
end

#resultObject (readonly)

Returns the value of attribute result.



14
15
16
# File 'app/models/atlas_engine/address_validation/log_emitter.rb', line 14

def result
  @result
end

Instance Method Details

#concern_codesObject



54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/atlas_engine/address_validation/log_emitter.rb', line 54

def concern_codes
  fields.flat_map do |field|
    if field.equal?(:street)
      result.concerns.select do |c|
        c.attributes[:code] =~ /^(missing_building_number|address1|address2|street).*/
      end
    else
      result.concerns.select { |c| c.attributes[:code] =~ /^#{field}.*/ }
    end
  end.map(&:code)
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/atlas_engine/address_validation/log_emitter.rb', line 32

def run
  formatted_address = I18n.with_locale(:en) do
    Worldwide.address(**address.to_h).single_line
  end
  data = {
    country_code: address.country_code,
    matching_strategy: result.matching_strategy,
    formatted_address: formatted_address,
    concerns: concern_codes,
    suggestions: result.suggestions.map(&:attributes),
    candidate: result.candidate,
    validation_id: result.id,
  }

  if concern_codes.any?
    log_info("[AddressValidation] Concern(s) found when validating address", data)
  else
    log_info("[AddressValidation] Address validated, no concerns returned", data)
  end
end