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.



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

def initialize(key)
  @key = key
end

Instance Method Details

#distance(origin, destination) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/google-distance-ruby.rb', line 8

def distance(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