class AddressLibrary

Attributes

store[R]

Public Class Methods

build(address_list) click to toggle source
# File lib/address_matcher/address_library.rb, line 2
def self.build(address_list)
  new.tap do |library|
    address_list.each { |address| library.add_address(address) }
  end
end
new() click to toggle source
# File lib/address_matcher/address_library.rb, line 8
def initialize
  @store = Hash.new do |outer, o_key|
    outer[o_key] = Hash.new do |inner, i_key|
      inner[i_key] = AddressGroup.new
    end
  end
end

Public Instance Methods

add_address(address_string) click to toggle source
# File lib/address_matcher/address_library.rb, line 16
def add_address(address_string)
  coords = geocode(address_string)
  if coords
    store[latitude_index(coords)][longitude_index(coords)][coords] = address_string
  end
  self
end
address_for_coords(lat_long) click to toggle source
# File lib/address_matcher/address_library.rb, line 35
def address_for_coords(lat_long)
  unless lat_long.to_a.empty?
    group = store[latitude_index(lat_long)][longitude_index(lat_long)]
    group.match(lat_long)
  end
end
match(address_string) click to toggle source
# File lib/address_matcher/address_library.rb, line 28
def match(address_string)
  coords = geocode(address_string)
  if coords
    address_for_coords(coords)
  end
end
near(coords) click to toggle source
# File lib/address_matcher/address_library.rb, line 24
def near(coords)
  store[latitude_index(coords)][longitude_index(coords)]
end

Private Instance Methods

geocode(address_string) click to toggle source
# File lib/address_matcher/address_library.rb, line 45
def geocode(address_string)
  response = Geocoder.search(address_string).first
  if response
    response.coordinates
  end
end
latitude_index(coords) click to toggle source
# File lib/address_matcher/address_library.rb, line 52
def latitude_index(coords)
  coords.first.round(3)
end
longitude_index(coords) click to toggle source
# File lib/address_matcher/address_library.rb, line 56
def longitude_index(coords)
  coords.last.round(3)
end