class GetTheDistance

Public Class Methods

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

Public Instance Methods

calculate(origin, destination) click to toggle source
# File lib/get_the_distance.rb, line 9
def calculate(origin, destination)
  result = HTTParty.get("https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + origin + "&destinations=" + 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