Sha256: 5eed85c1502ae2ff560d5bb50d6717a73c6f26be71058566ba7b414b3d2a8944

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

require 'geocoder/results/base'

module Geocoder::Result
  class Google < Base

    def coordinates
      ['lat', 'lng'].map{ |i| geometry['location'][i] }
    end

    def address(format = :full)
      formatted_address
    end

    def city
      fields = [:locality, :sublocality, :administrative_area_level_3,
        :administrative_area_level_2, :administrative_area_level_1]
      fields.each do |f|
        if entity = address_components_of_type(f).first
          return entity['long_name']
        end
      end
    end

    def country
      if country = address_components_of_type(:country).first
        country['long_name']
      end
    end

    def country_code
      if country = address_components_of_type(:country).first
        country['short_name']
      end
    end

    def postal_code
      if postal = address_components_of_type(:postal_code).first
        postal['long_name']
      end
    end

    def types
      @data['types']
    end

    def formatted_address
      @data['formatted_address']
    end

    def address_components
      @data['address_components']
    end

    ##
    # Get address components of a given type. Valid types are defined in
    # Google's Geocoding API documentation and include (among others):
    #
    #   :street_number
    #   :locality
    #   :neighborhood
    #   :route
    #   :postal_code
    #
    def address_components_of_type(type)
      address_components.select{ |c| c['types'].include?(type.to_s) }
    end

    def geometry
      @data['geometry']
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
geocoder-0.9.13 lib/geocoder/results/google.rb
geocoder-0.9.12 lib/geocoder/results/google.rb
rails-geocoder-0.9.11 lib/geocoder/results/google.rb
geocoder-0.9.11 lib/geocoder/results/google.rb