Class: GoogleApiDirections::GoogleDirections
- Inherits:
-
Object
- Object
- GoogleApiDirections::GoogleDirections
- Defined in:
- lib/google_api_directions/google_directions.rb
Instance Method Summary collapse
- #directions(origin, destination, language = "en") ⇒ Object
-
#initialize(apikey = "") ⇒ GoogleDirections
constructor
A new instance of GoogleDirections.
Constructor Details
#initialize(apikey = "") ⇒ GoogleDirections
Returns a new instance of GoogleDirections.
9 10 11 12 |
# File 'lib/google_api_directions/google_directions.rb', line 9 def initialize(apikey = "") @apikey = apikey @directions = [] end |
Instance Method Details
#directions(origin, destination, language = "en") ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/google_api_directions/google_directions.rb', line 14 def directions(origin, destination, language = "en") raise ArgumentError, "Argument(s) are missing or are empty" if origin.strip.empty? || destination.strip.empty? @directions.clear unless @directions.empty? set_uri(origin, destination, language) begin resp = Net::HTTP.get_response(URI.parse(@uri)) data = resp.body distance = JSON.parse(data)["routes"][0]["legs"][0]["distance"]["text"] duration = JSON.parse(data)["routes"][0]["legs"][0]["duration"]["text"] result = JSON.parse(data)["routes"][0]["legs"][0]["steps"] rescue Exception => e raise e. end result.each do |direction| text = (direction["html_instructions"]) route_distance = direction["distance"]["text"] route_duration = direction["duration"]["text"] @directions << GoogleApiDirections::GoogleDirectionsRoute.new(text.gsub(" ", " "), route_distance, route_duration) end {:directions => @directions, :distance => distance, :duration => duration} end |