diff --git a/examples/Google Directions.md b/examples/Google Directions.md new file mode 100644 index 0000000..289fa3c --- /dev/null +++ b/examples/Google Directions.md @@ -0,0 +1,32 @@ +# Emails And Contacts Scraper 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.7159662444353, 76.8053887016268', '29.696596, 76.994928 30.723065, 76.770169']) +``` diff --git a/lib/outscraper.rb b/lib/outscraper.rb index d5974b2..ca4dae2 100644 --- a/lib/outscraper.rb +++ b/lib/outscraper.rb @@ -4,6 +4,19 @@ require_relative "outscraper/version" +QUERY_DELIMITER = ' ' + +def format_direction_queries(q) + if q.is_a?(Array) && !q.empty? && q.first.is_a?(Array) + return q.map { |pair| pair.join(QUERY_DELIMITER) } + end + + return q if q.is_a?(Array) + + [q] +end + + module Outscraper class Client include HTTParty @@ -61,10 +74,10 @@ def google_maps_search_v3(query, limit: 20, language: 'en', region: nil, skip: 0 }).parsed_response['data'] end - 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) + def google_maps_directions(query:, departure_time: nil, finish_time: nil, interval: nil, travel_mode: 'best', language: 'en', region: nil, fields: nil, async_request: true) + queries = format_direction_queries(query) response = self.class.get('/maps/directions', query: { - origin: Array(origin), - destination: Array(destination), + query: queries, departure_time: departure_time, finish_time: finish_time, interval: interval, diff --git a/lib/outscraper/version.rb b/lib/outscraper/version.rb index 39d13fc..339832d 100644 --- a/lib/outscraper/version.rb +++ b/lib/outscraper/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Outscraper - VERSION = "0.3.3" + VERSION = "0.3.4" end