1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
require 'geocoder/results/base'
module Geocoder::Result
class Esri < Base
def address
address_key = reverse_geocode? ? 'Address' : 'Match_addr'
attributes[address_key]
end
def city
if !reverse_geocode? && is_city?
place_name
else
attributes['City']
end
end
def state
attributes['Region']
end
def state_code
abbr = attributes['RegionAbbr']
abbr.to_s == "" ? state : abbr
end
def country
country_key = reverse_geocode? ? "CountryCode" : "Country"
attributes[country_key]
end
alias_method :country_code, :country
def postal_code
attributes['Postal']
end
def place_name
place_name_key = reverse_geocode? ? "Address" : "PlaceName"
attributes[place_name_key]
end
def place_type
reverse_geocode? ? "Address" : attributes['Type']
end
def coordinates
[geometry["y"], geometry["x"]]
end
def viewport
north = attributes['Ymax']
south = attributes['Ymin']
east = attributes['Xmax']
west = attributes['Xmin']
[south, west, north, east]
end
private
def attributes
reverse_geocode? ? @data['address'] : @data['locations'].first['feature']['attributes']
end
def geometry
reverse_geocode? ? @data["location"] : @data['locations'].first['feature']["geometry"]
end
def reverse_geocode?
@data['locations'].nil?
end
def is_city?
['City', 'State Capital', 'National Capital'].include?(place_type)
end
end
end
|