Sha256: 3ecad4496ccc20d125539cec83e84451648c9262e847b830163b4b50e38e1df6

Contents?: true

Size: 921 Bytes

Versions: 3

Compression:

Stored size: 921 Bytes

Contents

module GoMaps
  class Address
    def initialize(address)
      @address = address
    end

    def exists?
      location['status'] == 'OK'
    end

    def distance_to(address)
      route_to(address)['distance']['text'].to_f
    end

    def route_to(address, options = {})
      options[:language] ||= 'en'
      directions_to(address, "&language=#{options[:language]}")['routes'].first['legs'].first rescue raise AddressNotFoundException
    end

    private

    def directions_to(address, language)
      api_response :directions, "origin=#{@address}&destination=#{address}#{language}"
    end

    def location
      api_response :geocode, "address=#{@address}"
    end

    def api_response(api, query_string)
      Crack::JSON.parse open(URI.escape(url_for(api) + query_string)).read
    end

    def url_for(api)
      "http://maps.google.com/maps/api/#{api}/json?sensor=false&units=metric&"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
go_maps-0.3.2 lib/go_maps/address.rb
go_maps-0.3.1 lib/go_maps/address.rb
go_maps-0.3.0 lib/go_maps/address.rb