Sha256: 022e4a5a0a441f40cf8cc995856b4bcb1dd23c31121e5a51aefd5265460a675a

Contents?: true

Size: 902 Bytes

Versions: 3

Compression:

Stored size: 902 Bytes

Contents

%w(uri net/http json cgi).each { |lib| require lib }

module GoogleLocalSearch
  
  class GeoCode
  
    GOOGLE_URL = "http://www.google.com/uds/GlocalSearch?"
    
    class NoMatchFound < StandardError; end
    
    class Location < Struct.new(:lat, :lng); end
  
    class << self
      def locate(address)
        options = { "rsz" => "small", "v" => "1.0" }
        options.merge!("q" => CGI.escape(address))
        response = Net::HTTP.get_response(URI.parse(GOOGLE_URL + options.map { |k,v| "#{k}=#{v}" }.join("&")))
        if response.is_a?(Net::HTTPSuccess)
          result = JSON.parse(response.body)
          raise NoMatchFound if result['responseData']['results'].empty?
          location = Location.new(result['responseData']['results'][0]['lat'], result['responseData']['results'][0]['lng'])
        else
          response.error!
        end
        
      end
    end
  
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kieranj-google-local-search-0.2.0 lib/google_local_search.rb
kieranj-google-local-search-0.2.1 lib/google_local_search.rb
kieranj-google-local-search-0.2.2 lib/google_local_search.rb