Class: GoogleMaps::Services::Directions
- Inherits:
-
Object
- Object
- GoogleMaps::Services::Directions
- Defined in:
- lib/googlemaps/services/directions.rb
Overview
Performs requests to the Google Maps Directions API.
Instance Attribute Summary collapse
-
#client ⇒ Symbol
The HTTP client.
Instance Method Summary collapse
-
#initialize(client) ⇒ Directions
constructor
A new instance of Directions.
-
#query(origin:, destination:, mode: nil, waypoints: nil, alternatives: false, avoid: nil, language: nil, units: nil, region: nil, departure_time: nil, arrival_time: nil, optimize_waypoints: false, transit_mode: nil, transit_routing_preference: nil, traffic_model: nil) ⇒ Array, Nokogiri::XML::NodeSet
Get directions between an origin point and a destination point.
Constructor Details
#initialize(client) ⇒ Directions
Returns a new instance of Directions.
16 17 18 |
# File 'lib/googlemaps/services/directions.rb', line 16 def initialize(client) self.client = client end |
Instance Attribute Details
#client ⇒ Symbol
Returns The HTTP client.
14 15 16 |
# File 'lib/googlemaps/services/directions.rb', line 14 def client @client end |
Instance Method Details
#query(origin:, destination:, mode: nil, waypoints: nil, alternatives: false, avoid: nil, language: nil, units: nil, region: nil, departure_time: nil, arrival_time: nil, optimize_waypoints: false, transit_mode: nil, transit_routing_preference: nil, traffic_model: nil) ⇒ Array, Nokogiri::XML::NodeSet
Get directions between an origin point and a destination point.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/googlemaps/services/directions.rb', line 46 def query(origin:, destination:, mode: nil, waypoints: nil, alternatives: false, avoid: nil, language: nil, units: nil, region: nil, departure_time: nil, arrival_time: nil, optimize_waypoints: false, transit_mode: nil, transit_routing_preference: nil, traffic_model: nil) params = { 'origin' => Convert.to_latlng(origin), 'destination' => Convert.to_latlng(destination) } if mode unless Constants::TRAVEL_MODES.include? mode raise StandardError, 'invalid travel mode.' end params['mode'] = mode end if waypoints waypoints = Convert.piped_location(waypoints) if optimize_waypoints waypoints = 'optimize:true|' + waypoints end params['waypoints'] = waypoints end if alternatives params['alternatives'] = true end if avoid unless ArrayBox.contains_all?(Constants::AVOID_FEATURES, avoid) raise StandardError, 'invalid avoid feature.' end params['avoid'] = Convert.join_array('|', avoid) end if language params['language'] = language end if units params['units'] = units end if region params['region'] = region end if departure_time params['departure_time'] = Convert.unix_time(departure_time) end if arrival_time params['arrival_time'] = Convert.unix_time(arrival_time) end if departure_time && arrival_time raise StandardError, 'should not specify both departure_time and arrival_time.' end if transit_mode params['transit_mode'] = Convert.join_array('|', transit_mode) end if transit_routing_preference params['transit_routing_preference'] = transit_routing_preference end if traffic_model params['traffic_model'] = traffic_model end case self.client.response_format when :xml self.client .request(url: '/maps/api/directions/xml', params: params) .xpath('//route') when :json self.client .request(url: '/maps/api/directions/json', params: params) .fetch('routes', []) else raise StandardError, 'Unsupported response format. Should be either :json or :xml.' end end |