Sha256: d355c8972aa84d852f7d518bc4b3b56b7b4fb8b35970ae119b2b9453817a1e3e

Contents?: true

Size: 1.47 KB

Versions: 9

Compression:

Stored size: 1.47 KB

Contents

# encoding: UTF-8
module Graticule #:nodoc:
  module Geocoder #:nodoc:

    # A library for lookup of coordinates with http://geo.localsearchmaps.com/
    #
    # See http://emad.fano.us/blog/?p=277
    class LocalSearchMaps < Base

      def initialize
        @url = URI.parse 'http://geo.localsearchmaps.com/'
      end

      # This web service will handle some addresses outside the US
      # if given more structured arguments than just a string address
      # So allow input as a hash for the different arguments (:city, :country, :zip)
      def locate(params)
        get params.is_a?(String) ? {:loc => params} : map_attributes(location_from_params(params))
      end

    private

      def map_attributes(location)
        mapping = {:street => :street, :locality => :city, :region => :state, :postal_code => :zip, :country => :country}
        mapping.keys.inject({}) do |result,attribute|
          result[mapping[attribute]] = location.attributes[attribute] unless location.attributes[attribute].blank?
          result
        end
      end

      def check_error(js)
        case js
        when nil
          raise AddressError, "Empty Response"
        when /location not found/
          raise AddressError, 'Location not found'
        end
      end

      def parse_response(js)
        coordinates = js.match(/map.centerAndZoom\(new GPoint\((.+?), (.+?)\)/)
        Location.new(:longitude => coordinates[1].to_f, :latitude => coordinates[2].to_f)
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
graticule-2.7.2 lib/graticule/geocoder/local_search_maps.rb
graticule-2.7.1 lib/graticule/geocoder/local_search_maps.rb
graticule-2.7.0 lib/graticule/geocoder/local_search_maps.rb
graticule-2.6.0 lib/graticule/geocoder/local_search_maps.rb
graticule-2.5.0 lib/graticule/geocoder/local_search_maps.rb
graticule-2.4.0 lib/graticule/geocoder/local_search_maps.rb
graticule-2.3.0 lib/graticule/geocoder/local_search_maps.rb
graticule-2.2.0 lib/graticule/geocoder/local_search_maps.rb
graticule-2.1.0 lib/graticule/geocoder/local_search_maps.rb