Class: AtlasEngine::AddressValidation::Candidate

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

Defined Under Namespace

Classes: Component

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, source:, position: 0, index: nil) ⇒ Candidate

Returns a new instance of Candidate.



12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/atlas_engine/address_validation/candidate.rb', line 12

def initialize(id:, source:, position: 0, index: nil)
  components_hash = Hash.new { |hash, key| hash[key] = Component.new(key, nil) }

  @components = source.each_with_object(components_hash) do |(key, value), hash|
    hash[key.to_sym] = Component.new(key.to_sym, value)
  end

  @id = id
  @components[:id] = Component.new(:id, id)
  @index = index
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#indexObject (readonly)

Returns the value of attribute index.



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

def index
  @index
end

#positionObject

Returns the value of attribute position.



9
10
11
# File 'app/models/atlas_engine/address_validation/candidate.rb', line 9

def position
  @position
end

Class Method Details

.from(hit) ⇒ Object



58
59
60
61
62
63
64
# File 'app/models/atlas_engine/address_validation/candidate.rb', line 58

def from(hit)
  id = hit.dig("_id")
  source = hit.dig("_source")
  source["city"] = source["city_aliases"].map(&:values).flatten if source["city_aliases"]
  index = hit.dig("_index")
  new(id: id, source: source, index: index)
end

Instance Method Details

#component(name) ⇒ Object



25
26
27
# File 'app/models/atlas_engine/address_validation/candidate.rb', line 25

def component(name)
  @components[name]
end

#components(*names) ⇒ Object



30
31
32
33
34
35
36
# File 'app/models/atlas_engine/address_validation/candidate.rb', line 30

def components(*names)
  return @components.reject { |_, component| component.value.nil? } if names.empty?

  names.index_with do |name|
    component(name)
  end
end

#describes_general_delivery?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/atlas_engine/address_validation/candidate.rb', line 50

def describes_general_delivery?
  component(:street)&.value&.casecmp("general delivery") == 0
end

#describes_po_box?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/atlas_engine/address_validation/candidate.rb', line 45

def describes_po_box?
  component(:street)&.value&.casecmp("po box") == 0
end

#serializeObject



39
40
41
42
# File 'app/models/atlas_engine/address_validation/candidate.rb', line 39

def serialize
  components(:locale, :province_code, :region2, :region3, :region4, :zip, :city, :suburb, :street)
    .values.map(&:serialize).join(",")
end