class GoogleDistanceRuby

Public Class Methods

new(key) click to toggle source
# File lib/google-distance-ruby.rb, line 5
def initialize(key)
  @key = key
end

Public Instance Methods

distance(origin, destination) click to toggle source
# File lib/google-distance-ruby.rb, line 9
def distance(origin, destination)
  result = HTTParty.get("https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + CGI.escape(origin) + "&destinations=" + CGI.escape(destination) + "&key=" + @key)
  if result.code == 200
    return {
             origin: result["origin_addresses"][0],
             destination: result["destination_addresses"][0],
             distance: result["rows"][0]["elements"][0]["distance"]["value"]
           }
  else
    return nil
  end
end