diff --git a/examples/Google Directions.md b/examples/Google Directions.md new file mode 100644 index 0000000..04202ed --- /dev/null +++ b/examples/Google Directions.md @@ -0,0 +1,35 @@ +# Google Directions With Ruby + +Returns directions between two points from Google Maps. [Outscraper API](https://app.outscraper.cloud/api-docs#tag/Google/paths/~1maps~1directions/get). + +## Installation + +Install the gem and add to the application's Gemfile by executing: +```bash +bundle add outscraper +``` + +If bundler is not being used to manage dependencies, install the gem by executing: +```bash +gem install outscraper +``` + +[Link to the Ruby package page](https://rubygems.org/gems/outscraper) + +## Initialization +```ruby +require 'Outscraper' + +client = Outscraper::Client.new('SECRET_API_KEY') +``` +[Link to the profile page to create the API key](https://app.outscraper.com/profile) + +## Usage + +```ruby +# Returns directions: +results = client.google_maps_directions([ + ['29.696596,76.994928', '30.715966244353,76.8053887016268'], + ['29.696596,76.994928', '30.723065,76.770169'] +]) +``` diff --git a/lib/outscraper.rb b/lib/outscraper.rb index 8cdc16a..819add9 100644 --- a/lib/outscraper.rb +++ b/lib/outscraper.rb @@ -9,6 +9,8 @@ class Client include HTTParty base_uri 'https://api.app.outscraper.com' + QUERY_DELIMITER = ' ' + def initialize(api_key) self.class.headers 'X-API-KEY' => api_key, 'Accept-Encoding' => 'utf-8' end @@ -63,8 +65,7 @@ def google_maps_search_v3(query, limit: 20, language: 'en', region: nil, skip: 0 def google_maps_directions(origin: '', destination: '', departure_time: nil, finish_time: nil, interval: nil, travel_mode: 'best', language: 'en', region: nil, fields: nil, async_request: true) response = self.class.get('/maps/directions', query: { - origin: Array(origin), - destination: Array(destination), + queries: format_queries(queries), departure_time: departure_time, finish_time: finish_time, interval: interval, @@ -352,5 +353,16 @@ def yellowpages_search(query, location: 'New York, NY', limit: 100, region: nil, webhook: webhook }).parsed_response['data'] end + + private + + def format_queries(queries) + queries = Array(queries) + if queries.all? { |i| i.is_a?(Array) } + queries.map { |pair| pair.join(QUERY_DELIMITER) } + else + queries + end + end end end