Sha256: 1b13db445c9252e819b200b3da7b336dc916963f1ee706e0702cfd587ec29f21

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

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_code
      attributes['Region']
    end

    alias_method :state, :state_code

    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

    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geocoder-1.3.0 lib/geocoder/results/esri.rb