Class: EnrichmentDb::Geo::Locator
- Inherits:
-
DatumModel
- Object
- DatumModel
- EnrichmentDb::Geo::Locator
- Defined in:
- lib/enrichment_db/geo/locator.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#postcode ⇒ Object
readonly
Returns the value of attribute postcode.
-
#sa1 ⇒ Object
readonly
Returns the value of attribute sa1.
-
#sa2 ⇒ Object
readonly
Returns the value of attribute sa2.
-
#sa3 ⇒ Object
readonly
Returns the value of attribute sa3.
-
#sa4 ⇒ Object
readonly
Returns the value of attribute sa4.
-
#smallest_region ⇒ Object
readonly
Returns the value of attribute smallest_region.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#suburb ⇒ Object
readonly
Returns the value of attribute suburb.
Attributes inherited from DatumModel
Class Method Summary collapse
- .all_region_names ⇒ Object
- .all_regions ⇒ Object
- .find_id(data, region_type) ⇒ Object
- .format_geo(geo) ⇒ Object
- .geo_locate(geohash) ⇒ Object
- .valid_geo_point?(geo_point) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(data) ⇒ Locator
constructor
A new instance of Locator.
Methods inherited from DatumModel
all, by_id, by_lambda, format_result, lazy_attr_reader
Constructor Details
#initialize(data) ⇒ Locator
Returns a new instance of Locator.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/enrichment_db/geo/locator.rb', line 14 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/enrichment_db/geo/locator.rb', line 15 def name @name end |
#postcode ⇒ Object (readonly)
Returns the value of attribute postcode.
21 22 23 |
# File 'lib/enrichment_db/geo/locator.rb', line 21 def postcode @postcode end |
#sa1 ⇒ Object (readonly)
Returns the value of attribute sa1.
20 21 22 |
# File 'lib/enrichment_db/geo/locator.rb', line 20 def sa1 @sa1 end |
#sa2 ⇒ Object (readonly)
Returns the value of attribute sa2.
19 20 21 |
# File 'lib/enrichment_db/geo/locator.rb', line 19 def sa2 @sa2 end |
#sa3 ⇒ Object (readonly)
Returns the value of attribute sa3.
18 19 20 |
# File 'lib/enrichment_db/geo/locator.rb', line 18 def sa3 @sa3 end |
#sa4 ⇒ Object (readonly)
Returns the value of attribute sa4.
17 18 19 |
# File 'lib/enrichment_db/geo/locator.rb', line 17 def sa4 @sa4 end |
#smallest_region ⇒ Object (readonly)
Returns the value of attribute smallest_region.
23 24 25 |
# File 'lib/enrichment_db/geo/locator.rb', line 23 def smallest_region @smallest_region end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
16 17 18 |
# File 'lib/enrichment_db/geo/locator.rb', line 16 def state @state end |
#suburb ⇒ Object (readonly)
Returns the value of attribute suburb.
22 23 24 |
# File 'lib/enrichment_db/geo/locator.rb', line 22 def suburb @suburb end |
Class Method Details
.all_region_names ⇒ Object
76 77 78 |
# File 'lib/enrichment_db/geo/locator.rb', line 76 def self.all_region_names EnrichmentDb::Geo.constants - [:Region, :Locator] end |
.all_regions ⇒ Object
68 69 70 71 72 73 74 |
# 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) ⇒ Object
60 61 62 63 64 65 66 |
# 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) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# 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) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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 |
.valid_geo_point?(geo_point) ⇒ Boolean
90 91 92 |
# 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 |