Sha256: d96410f44fa20ea6e8bcd8bbc361665f054ea83903f480d1283421539c42bc71

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

module TollBooth
  class Route
    include HTTParty
    attr_accessor :name, :steps, :distance, :travel_time

    default_params(
      :output => "json"
    )
    base_uri "http://maps.google.com/maps/nav"

    class << self
      # get driving directions from the origin to the specified destination(s)
      # @param origin [TollBooth::Location] the starting point for the trip
      # @param destinations [Array] a list of destinations to drive to
      # @return [TollBooth::RouteCollection] a list of potential routes for the trip
      def find(origin, destinations)
        response = get("", 
          :query => {:q => "from: #{origin.description} " + 
            destinations.collect{|d| " to: #{d.description}"}.join("")}) 
        routes = parse(response)

        routes
      end

      # parses response from google
      # @param the response received from google
      # @return [TollBooth::RouteCollection] returns a route collection based on the request
      def parse(response)
        resp = JSON.parse(response.body)

        code = response["Status"]["code"] 
        routes = TollBooth::RouteCollection.new
        if code == 200
          resp["Directions"]["Routes"].each do |r|
            route = new 
            route.distance = r["Distance"]["html"].gsub("&nbsp;mi", "").to_f
            route.travel_time = ChronicDuration.parse(r["Duration"]["html"])
            route.steps = TollBooth::RouteStep.parse(r["Steps"])

            routes << route
          end 

        else
          routes.errors = [error_for(code)]
        end
        routes
      end

      private
      # get a friently error message for the status code specified
      # @param code [Integer] code to translate
      # @return [String] a friendly error message
      def error_for(code)
        if code == 602
          "Bad Address Specified"
        elsif code == 500
          "Server Error"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
dpickett-toll_booth-0.1.0 lib/toll_booth/route.rb
toll_booth-0.2.0 lib/toll_booth/route.rb
toll_booth-0.1.0 lib/toll_booth/route.rb