class EnrichmentDb::Geo::Locator

Attributes

id[R]
name[R]
postcode[R]
sa1[R]
sa2[R]
sa3[R]
sa4[R]
smallest_region[R]
state[R]
suburb[R]

Public Class Methods

all_region_names() click to toggle source
# File lib/enrichment_db/geo/locator.rb, line 76
def self.all_region_names 
  EnrichmentDb::Geo.constants - [:Region, :Locator]
end
all_regions() click to toggle source
# File lib/enrichment_db/geo/locator.rb, line 68
def self.all_regions
  all_region_names.collect do |region|
    EnrichmentDb::Geo.const_get(region)
  end.sort_by do |region|
    region.scale
  end
end
find_id(data, region_type) click to toggle source
# File lib/enrichment_db/geo/locator.rb, line 60
def self.find_id(data, region_type)
  id = "#{region_type}_id"
  data.each do |k, v|
    return v[id] if v.is_a?(Hash) && v.keys.include?(id)
  end
  nil
end
format_geo(geo) click to toggle source
# File lib/enrichment_db/geo/locator.rb, line 80
def self.format_geo(geo)
  return geo if geo.is_a?(String)

      fail EnrichmentDb::InvalidGeoPointFormat unless valid_geo_point?(geo)
  geo_format = geo.each_with_object({}) do |(k, v), h|
    h[k.to_sym] = v.to_f
  end
  GeoHash.encode(geo_format[:lat], geo_format[:lon])
end
geo_locate(geohash) click to toggle source
# File lib/enrichment_db/geo/locator.rb, line 40
def self.geo_locate(geohash)
  formatted_geohash = format_geo(geohash)
  values = [formatted_geohash]

  data = {}
  all_regions.each do |region|
    region_name = region.object_type
    region_id = find_id(data, region_name)

    result = if !region_id.nil?
      region.by_id(region_id)
    else
      region.by_boundary(values)
    end
    data['smallest_region'] ||= region_name if !result.nil?
    data[region_name] = result
  end 
  self.new(data)
end
new(data) click to toggle source
# File lib/enrichment_db/geo/locator.rb, line 25
def initialize(data)
  self.class.all_regions.each do |region|
    region_name = region.object_type
    region_data = data[region_name]
    region_instance = !!region_data ? region.new(region_data) : nil
    instance_variable_set("@#{region_name}", region_instance)

    if data['smallest_region'] == region_name
      @id = region_data['id']
      @name = region_data['name']
      @smallest_region = region_name
    end
  end
end
valid_geo_point?(geo_point) click to toggle source
# File lib/enrichment_db/geo/locator.rb, line 90
def self.valid_geo_point?(geo_point)
      geo_point.is_a?(Hash) && geo_point.keys.sort.collect(&:to_sym) == [:lat, :lon]
end

Private Class Methods

make_table_name(object_name) click to toggle source
# File lib/enrichment_db/geo/locator.rb, line 100
def self.make_table_name(object_name)
  "#{object_name}s"
end
object_type() click to toggle source
# File lib/enrichment_db/geo/locator.rb, line 96
def self.object_type
  to_s.split(':').last.downcase.to_sym
end