Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/Google Directions.md
Original file line number Diff line number Diff line change
@@ -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']
])
```
16 changes: 14 additions & 2 deletions lib/outscraper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Loading