Class: AtlasEngine::AddressValidation::StatsdEmitter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address:, result:, components: [:country, :province, :zip, :city, :street, :building_number, :phone]) ⇒ StatsdEmitter

Returns a new instance of StatsdEmitter.



11
12
13
14
15
16
# File 'app/models/atlas_engine/address_validation/statsd_emitter.rb', line 11

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

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



8
9
10
# File 'app/models/atlas_engine/address_validation/statsd_emitter.rb', line 8

def address
  @address
end

#componentsObject (readonly)

Returns the value of attribute components.



8
9
10
# File 'app/models/atlas_engine/address_validation/statsd_emitter.rb', line 8

def components
  @components
end

#resultObject (readonly)

Returns the value of attribute result.



8
9
10
# File 'app/models/atlas_engine/address_validation/statsd_emitter.rb', line 8

def result
  @result
end

Instance Method Details

#component_concerns(component) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/atlas_engine/address_validation/statsd_emitter.rb', line 61

def component_concerns(component)
  if component.equal?(:street)
    result.concerns.select do |c|
      c.attributes[:code] =~ /^(address1|address2|street|address_unknown).*/
    end
  elsif component.equal?(:building_number)
    result.concerns.select do |c|
      c.attributes[:code] =~ /^(missing_building_number).*/
    end
  else
    result.concerns.select { |c| c.attributes[:code] =~ /^#{component}.*/ }
  end
end

#emit(component) ⇒ Object



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 'app/models/atlas_engine/address_validation/statsd_emitter.rb', line 26

def emit(component)
  concerns = component_concerns(component)
  ending_breadcrumb = concerns.present? ? "invalid" : "valid"

  country_code = if address.country_code.blank? || !Worldwide.region(code: address.country_code).country?
    "no_country"
  else
    Worldwide.region(code: address.country_code).iso_code
  end

  I18n.with_locale("en") do
    tags = {
      country: country_code,
      component: component,
    }.compact

    if concerns.empty?
      StatsD.increment("AddressValidation.#{ending_breadcrumb}", tags: tags)
    else
      concerns.each do |concern|
        tags.merge!(concern.attributes.slice(:code, :type))

        if concern.attributes[:code] == :address_unknown
          StatsD.increment("AddressValidation.unknown", tags: tags.except(:component))
        else
          StatsD.increment("AddressValidation.#{ending_breadcrumb}", tags: tags)
        end
      end
    end
  end

  nil
end

#runObject



19
20
21
22
23
# File 'app/models/atlas_engine/address_validation/statsd_emitter.rb', line 19

def run
  components.each do |component|
    emit(component)
  end
end