Class: GoogleDistanceRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/google-distance-ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ GoogleDistanceRuby

Returns a new instance of GoogleDistanceRuby.



5
6
7
# File 'lib/google-distance-ruby.rb', line 5

def initialize(key)
  @key = key
end

Instance Method Details

#distance(origin, destination) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# 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