gistance 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +4 -0
- data/CONTRIBUTING.md +32 -0
- data/LICENSE +20 -0
- data/README.md +102 -0
- data/Rakefile +42 -0
- data/gistance.gemspec +30 -0
- data/lib/gistance/client/distance_matrix.rb +52 -0
- data/lib/gistance/client.rb +26 -0
- data/lib/gistance/configuration.rb +49 -0
- data/lib/gistance/connection.rb +28 -0
- data/lib/gistance/error.rb +52 -0
- data/lib/gistance/request.rb +29 -0
- data/lib/gistance/response/raise_error.rb +18 -0
- data/lib/gistance/version.rb +5 -0
- data/lib/gistance.rb +25 -0
- data/spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_an_array_of_destination_addresses.yml +66 -0
- data/spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_an_array_of_origin_addresses.yml +66 -0
- data/spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_destination_address.yml +66 -0
- data/spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_distance_between_origin_and_destination.yml +66 -0
- data/spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_origin_address.yml +66 -0
- data/spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_travel_duration_between_origin_and_destination.yml +66 -0
- data/spec/lib/gistance/client/distance_matrix_spec.rb +40 -0
- data/spec/lib/gistance/client_spec.rb +83 -0
- data/spec/lib/gistance/request_spec.rb +11 -0
- data/spec/lib/gistance_spec.rb +32 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/support/shared_examples/error.rb +16 -0
- metadata +154 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: be45454a9c540c04313d8a0b0c98859a193bc73e
|
4
|
+
data.tar.gz: 45714fb08ea043676b2b2ac69239f99095de65ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ad1aff0b8d2b1695aab6e9460512c83aea526eb298a6f1a9a2ec8ce10c65f9ba7beebb1fc4280570c86582207d3c94fa292d2a27fba16693e4146f02e088a86
|
7
|
+
data.tar.gz: 8af510bab65b26500fd37630cca0f145a11ff369a3f0dac7b04cfb2fe8b982b20a4f372faaca815ebab520e4fb29779feaf11366c998634d40af6e3aa942dfd5
|
data/.yardopts
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
## Running the test suite
|
4
|
+
|
5
|
+
bundle exec rake spec
|
6
|
+
|
7
|
+
### Code quality
|
8
|
+
|
9
|
+
Code quality is checked with [cane](https://github.com/square/cane) configured in `Rakefile`.
|
10
|
+
|
11
|
+
Build fails if code quality thresholds **are not met**.
|
12
|
+
|
13
|
+
### Code coverage
|
14
|
+
|
15
|
+
When running the test suite a test coverage report is generated with [SimpleCov](https://github.com/colszowka/simplecov) in `coverage/index.html`.
|
16
|
+
|
17
|
+
Build fails if coverage is below **99%**.
|
18
|
+
|
19
|
+
## Writing new tests
|
20
|
+
Gistance uses [VCR](https://github.com/vcr/vcr) for recording and playing back API fixtures (in `spec/cassettes`) during test runs.
|
21
|
+
|
22
|
+
## Documentation
|
23
|
+
|
24
|
+
Documentation is generated with [YARD](http://yardoc.org/).
|
25
|
+
|
26
|
+
## Workflow
|
27
|
+
|
28
|
+
1. Fork [Gistance](https://github.com/sush/gistance)
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create a new Pull Request
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) [2014] [Aylic Petit]
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
Simple Ruby wrapper for the [Google Distance Matrix API](https://developers.google.com/places/documentation).
|
2
|
+
|
3
|
+
**Current version**: [![Gem Version](https://badge.fury.io/rb/gistance.png)](http://badge.fury.io/rb/gistance)
|
4
|
+
|
5
|
+
**Build status**: [![Build Status](https://secure.travis-ci.org/sush/gistance.png?branch=master)](http://travis-ci.org/sush/gistance)
|
6
|
+
|
7
|
+
**Code metrics**:
|
8
|
+
[![Code Climate](https://codeclimate.com/github/sush/gistance.png)](https://codeclimate.com/github/sush/gistance)
|
9
|
+
[![Coverage Status](https://coveralls.io/repos/sush/gistance/badge.png?branch=master)](https://coveralls.io/r/sush/gistance?branch=master)
|
10
|
+
|
11
|
+
**Ruby support**:
|
12
|
+
|
13
|
+
- 1.9.2
|
14
|
+
- 1.9.3
|
15
|
+
- 2.0.0
|
16
|
+
- 2.1.1
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Install via Rubygems
|
21
|
+
|
22
|
+
gem install gistance
|
23
|
+
|
24
|
+
or add to your Gemfile
|
25
|
+
|
26
|
+
gem 'gistance'
|
27
|
+
|
28
|
+
### Configuration
|
29
|
+
|
30
|
+
API methods are available as module methods
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
Gistance.configure do |c|
|
34
|
+
c.api_key = 'YOUR_API_KEY'
|
35
|
+
c.unit = 'imperial' # default to metric
|
36
|
+
c.language = 'fr' # default to en
|
37
|
+
c.sensor = true # default to false
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
or as client instance methods
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
Gistance::Client.new(
|
45
|
+
api_key: 'YOUR_API_KEY',
|
46
|
+
unit: 'imperial',
|
47
|
+
language: 'fr',
|
48
|
+
sensor: true
|
49
|
+
)
|
50
|
+
```
|
51
|
+
|
52
|
+
The `unit`, `language` and `sensor` parameters can be set globally or can be provided for every request if passed as parameters.
|
53
|
+
|
54
|
+
## Authentication
|
55
|
+
|
56
|
+
Gistance only supports authentication via an API key.
|
57
|
+
|
58
|
+
You can request one following these [steps](https://developers.google.com/maps/documentation/distancematrix/#api_key).
|
59
|
+
|
60
|
+
## Usage
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
distances = Gistance.distance_matrix(
|
64
|
+
origins: ["37.769541, -122.486747"],
|
65
|
+
destinations: ["37.740497, -122.442887"]
|
66
|
+
)
|
67
|
+
|
68
|
+
distances.origin_addresses
|
69
|
+
# => ["Golden Gate Park"]
|
70
|
+
|
71
|
+
# etc…
|
72
|
+
```
|
73
|
+
|
74
|
+
## Features
|
75
|
+
|
76
|
+
Gistance supports all the [Google Distance Matrix parameters](https://developers.google.com/maps/documentation/distancematrix/#RequestParameters)
|
77
|
+
|
78
|
+
Complete Gistance public API's documentation [here](http://rubydoc.info/gems/gistance/frames).
|
79
|
+
|
80
|
+
## Similar libraries
|
81
|
+
|
82
|
+
- [google_distance_matrix](https://github.com/Skalar/google_distance_matrix)
|
83
|
+
|
84
|
+
## Versioning
|
85
|
+
Gistance follows the principles of [semantic versioning](http://semver.org).
|
86
|
+
|
87
|
+
1. Patch level releases contain only bug fixes.
|
88
|
+
2. Minor releases contain backward-compatible new features.
|
89
|
+
3. Major new releases contain backwards-incompatible changes to the public API.
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
Pull Requests are welcome !
|
94
|
+
|
95
|
+
Please refer to the [Contributing guide](https://github.com/sush/gistance/blob/master/CONTRIBUTING.md) for more details on how to run the test suite and to contribute.
|
96
|
+
|
97
|
+
|
98
|
+
## Copyright
|
99
|
+
|
100
|
+
Copyright © 2014 Aylic Petit
|
101
|
+
|
102
|
+
Released under the terms of the MIT licence. See the [LICENSE](https://github.com/sush/gistance/blob/master/LICENSE) file for more details.
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'cane/rake_task'
|
9
|
+
|
10
|
+
desc 'Run cane to check quality metrics'
|
11
|
+
Cane::RakeTask.new(:quality) do |cane|
|
12
|
+
cane.add_threshold 'coverage/.last_run.json', :>=, 99
|
13
|
+
cane.abc_glob = '{lib}/**/*.rb'
|
14
|
+
cane.abc_max = 15
|
15
|
+
cane.style_measure = 100
|
16
|
+
cane.style_exclude = %w[spec/spec_helper.rb]
|
17
|
+
cane.canefile = '.cane'
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
end
|
21
|
+
|
22
|
+
namespace :doc do
|
23
|
+
begin
|
24
|
+
require 'yard'
|
25
|
+
|
26
|
+
YARD::Rake::YardocTask.new do |task|
|
27
|
+
task.files = ['README.md', 'LICENSE.md', 'lib/**/*.rb']
|
28
|
+
task.options = [
|
29
|
+
'--output-dir', 'doc/yard',
|
30
|
+
'--markup', 'markdown'
|
31
|
+
]
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
Rake::Task[:spec].enhance do
|
38
|
+
Rake::Task[:quality].invoke
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :spec
|
42
|
+
task :default => :spec
|
data/gistance.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gistance/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'gistance'
|
8
|
+
gem.version = Gistance::VERSION.dup
|
9
|
+
gem.license = 'MIT'
|
10
|
+
|
11
|
+
gem.summary = %q{Google Distance Matrix API Ruby wrapper}
|
12
|
+
gem.description = %q{Simple Ruby wrapper for the Google Distance Matrix API.}
|
13
|
+
gem.homepage = 'https://github.com/sush/gistance'
|
14
|
+
|
15
|
+
gem.authors = ['Aylic Petit']
|
16
|
+
gem.email = ['sush@users.noreply.github.com']
|
17
|
+
|
18
|
+
gem.required_ruby_version = '>= 1.9.2'
|
19
|
+
|
20
|
+
gem.files = %w[.yardopts LICENSE README.md CONTRIBUTING.md Rakefile gistance.gemspec]
|
21
|
+
gem.files += Dir.glob('{spec,lib}/**/*.rb')
|
22
|
+
gem.require_paths = ['lib']
|
23
|
+
gem.test_files = Dir.glob('spec/**/*')
|
24
|
+
|
25
|
+
gem.add_dependency 'faraday', '~> 0.8.9'
|
26
|
+
gem.add_dependency 'faraday_middleware', '~> 0.9.0'
|
27
|
+
gem.add_dependency 'hashie', '~>2.0.5'
|
28
|
+
gem.add_dependency 'json', '~>1.8.1'
|
29
|
+
gem.add_dependency 'multi_json', '~>1.9.2'
|
30
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Gistance
|
2
|
+
class Client
|
3
|
+
# Methods for the Distance Matrix API
|
4
|
+
#
|
5
|
+
# @see https://developers.google.com/maps/documentation/distancematrix
|
6
|
+
module DistanceMatrix
|
7
|
+
# Calculate distances for a matrix of origins and destinations.
|
8
|
+
# @param options [Hash] a customizable set of options
|
9
|
+
# @option options [Hash] :origins an array of origins
|
10
|
+
# @option options [Hash] :destinations an array of destination
|
11
|
+
# @return [Hashie::Mash] the distance between origins and destinations
|
12
|
+
# @see https://developers.google.com/maps/documentation/distancematrix/#DistanceMatrixRequests
|
13
|
+
# @example
|
14
|
+
# Gistance.distance_matrix(
|
15
|
+
# origins: ["48.92088132,2.210950607"],
|
16
|
+
# destinations: ["48.922024,2.206292"]
|
17
|
+
# )
|
18
|
+
def distance_matrix(options={})
|
19
|
+
options[:origins] = format_locations_array(options[:origins])
|
20
|
+
options[:destinations] = format_locations_array(options[:destinations])
|
21
|
+
|
22
|
+
delete_status_from_response(get(options))
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def format_locations_array(locations)
|
28
|
+
locations.map { |location| format_location(location) }.join('|')
|
29
|
+
end
|
30
|
+
|
31
|
+
def format_location(location)
|
32
|
+
return location if location_is_coordinates?(location)
|
33
|
+
|
34
|
+
location.split(' ').join('+')
|
35
|
+
end
|
36
|
+
|
37
|
+
def location_is_coordinates?(location)
|
38
|
+
location.to_s =~ /^\s*-?\d+\.\d+\,\s?-?\d+\.\d+\s*$/
|
39
|
+
end
|
40
|
+
|
41
|
+
def delete_status_from_response(response)
|
42
|
+
response.delete(:status)
|
43
|
+
|
44
|
+
response.rows.each do |row|
|
45
|
+
row['elements'].each { |el| el.delete(:status) }
|
46
|
+
end
|
47
|
+
|
48
|
+
response
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'gistance/connection'
|
2
|
+
require 'gistance/request'
|
3
|
+
|
4
|
+
require 'gistance/client/distance_matrix'
|
5
|
+
|
6
|
+
module Gistance
|
7
|
+
# Client for the Google Distance Matrix API
|
8
|
+
#
|
9
|
+
# @see https://developers.google.com/maps/documentation/distancematrix
|
10
|
+
class Client
|
11
|
+
attr_accessor(*Configuration::VALID_OPTIONS)
|
12
|
+
|
13
|
+
def initialize(options={})
|
14
|
+
options = Gistance.options.merge(options)
|
15
|
+
|
16
|
+
Configuration::VALID_OPTIONS.each do |key|
|
17
|
+
send("#{key}=", options[key])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
include Gistance::Connection
|
22
|
+
include Gistance::Request
|
23
|
+
|
24
|
+
include Gistance::Client::DistanceMatrix
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Gistance
|
2
|
+
# Methods for Gistance configuration
|
3
|
+
module Configuration
|
4
|
+
# Configurable options
|
5
|
+
VALID_OPTIONS = [
|
6
|
+
:api_endpoint,
|
7
|
+
:api_key,
|
8
|
+
:language,
|
9
|
+
:unit,
|
10
|
+
:sensor
|
11
|
+
].freeze
|
12
|
+
|
13
|
+
# Default Google Distance Matrix API endpoint
|
14
|
+
DEFAULT_API_ENDPOINT = 'https://maps.googleapis.com/maps/api/distancematrix/json'
|
15
|
+
|
16
|
+
attr_accessor(*VALID_OPTIONS)
|
17
|
+
|
18
|
+
# @private
|
19
|
+
def self.extended(base_obj)
|
20
|
+
base_obj.initialize_default_options
|
21
|
+
end
|
22
|
+
|
23
|
+
# Set configuration options using a block
|
24
|
+
def configure
|
25
|
+
yield self
|
26
|
+
end
|
27
|
+
|
28
|
+
# Default options
|
29
|
+
def options
|
30
|
+
VALID_OPTIONS.inject({}){ |o, k| o.merge!(k => send(k)) }
|
31
|
+
end
|
32
|
+
|
33
|
+
# Initialize default options
|
34
|
+
def initialize_default_options
|
35
|
+
self.api_endpoint = DEFAULT_API_ENDPOINT
|
36
|
+
self.api_key = nil
|
37
|
+
self.language = 'en'
|
38
|
+
self.unit = 'metric'
|
39
|
+
self.sensor = false
|
40
|
+
end
|
41
|
+
|
42
|
+
# Reset configuration options to default values
|
43
|
+
def reset!
|
44
|
+
initialize_default_options
|
45
|
+
|
46
|
+
self
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
require 'gistance/response/raise_error'
|
3
|
+
|
4
|
+
module Gistance
|
5
|
+
# Faraday connection methods
|
6
|
+
module Connection
|
7
|
+
private
|
8
|
+
|
9
|
+
def connection
|
10
|
+
options = {
|
11
|
+
ssl: { verify: false },
|
12
|
+
url: self.api_endpoint
|
13
|
+
}
|
14
|
+
|
15
|
+
connection = Faraday.new(options) do |conn|
|
16
|
+
conn.response(:mashify)
|
17
|
+
conn.response(:json, content_type: /\bjson$/)
|
18
|
+
|
19
|
+
conn.use(Gistance::Response::RaiseError)
|
20
|
+
conn.use(FaradayMiddleware::FollowRedirects, limit: 3)
|
21
|
+
|
22
|
+
conn.adapter(Faraday.default_adapter)
|
23
|
+
end
|
24
|
+
|
25
|
+
connection
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
3
|
+
module Gistance
|
4
|
+
# Custom error class
|
5
|
+
class Error < StandardError
|
6
|
+
# Returns the Error based on status and response message.
|
7
|
+
#
|
8
|
+
# @param [Hash] response HTTP response
|
9
|
+
# @return [Gistance::Error]
|
10
|
+
def self.from_response(response)
|
11
|
+
status = ::MultiJson.load(response[:body])['status']
|
12
|
+
|
13
|
+
case status
|
14
|
+
when 'INVALID_REQUEST'
|
15
|
+
raise Gistance::InvalidRequest, status
|
16
|
+
when 'MAX_ELEMENTS_EXCEEDED'
|
17
|
+
raise Gistance::MaxElementsExceeded, status
|
18
|
+
when 'OVER_QUERY_LIMIT'
|
19
|
+
raise Gistance::OverQueryLimit, status
|
20
|
+
when 'REQUEST_DENIED'
|
21
|
+
raise Gistance::RequestDenied, status
|
22
|
+
when 'UNKNOWN_ERROR'
|
23
|
+
raise Gistance::UnknownError, status
|
24
|
+
when 'NOT_FOUND'
|
25
|
+
raise Gistance::NotFound, status
|
26
|
+
when 'ZERO_RESULTS'
|
27
|
+
raise Gistance::ZeroResults, status
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Raised when API returns INVALID_REQUEST
|
33
|
+
class InvalidRequest < Error; end
|
34
|
+
#
|
35
|
+
# Raised when API returns MAX_ELEMENTS_EXCEEDED
|
36
|
+
class MaxElementsExceeded < Error; end
|
37
|
+
|
38
|
+
# Raised when API returns OVER_QUERY_LIMIT
|
39
|
+
class OverQueryLimit < Error; end
|
40
|
+
|
41
|
+
# Raised when API returns REQUEST_DENIED
|
42
|
+
class RequestDenied < Error; end
|
43
|
+
|
44
|
+
# Raised when API returns UNKNOWN_ERROR
|
45
|
+
class UnknownError < Error; end
|
46
|
+
|
47
|
+
# Raised when API returns NOT_FOUND
|
48
|
+
class NotFound < Error; end
|
49
|
+
|
50
|
+
# Raised when API returns ZERO_RESULTS
|
51
|
+
class ZeroResults < Error; end
|
52
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Gistance
|
2
|
+
# Methods for HTTP requests
|
3
|
+
module Request
|
4
|
+
# Make a HTTP GET request.
|
5
|
+
#
|
6
|
+
# @param path [String] The path, relative to api_endpoint
|
7
|
+
# @param options [Hash] query params for request
|
8
|
+
# @return [Hashie::Mash]
|
9
|
+
def get(options={})
|
10
|
+
request(:get, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def request(method, options)
|
16
|
+
response = connection.send(method) do |request|
|
17
|
+
request.params[:key] = self.api_key if self.api_key
|
18
|
+
|
19
|
+
[:language, :unit, :sensor].each do |option|
|
20
|
+
request.params[option] = options.delete(option) || self.public_send(option)
|
21
|
+
end
|
22
|
+
|
23
|
+
request.url(self.api_endpoint, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
response.body
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'gistance/error'
|
3
|
+
|
4
|
+
module Gistance
|
5
|
+
# Faraday response middleware
|
6
|
+
module Response
|
7
|
+
# Raises a Gistance exception based on status response or HTTP status codes returned by the API
|
8
|
+
class RaiseError < Faraday::Response::Middleware
|
9
|
+
private
|
10
|
+
|
11
|
+
def on_complete(response)
|
12
|
+
if error = Gistance::Error.from_response(response)
|
13
|
+
raise error
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/gistance.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'gistance/configuration'
|
2
|
+
require 'gistance/client'
|
3
|
+
|
4
|
+
# Ruby wrapper for Google Distance Matrix API
|
5
|
+
module Gistance
|
6
|
+
extend Configuration
|
7
|
+
|
8
|
+
class << self
|
9
|
+
# Delegates to Gistance::Client#new
|
10
|
+
def new(options={})
|
11
|
+
Gistance::Client.new(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
# @private
|
15
|
+
def method_missing(method, *args, &block)
|
16
|
+
return super unless new.respond_to?(method)
|
17
|
+
new.send(method, *args, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
# @private
|
21
|
+
def respond_to?(method, include_private=false)
|
22
|
+
new.respond_to?(method, include_private) || super(method, include_private)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://maps.googleapis.com/maps/api/distancematrix/json?destinations=48.857066,%202.341138&language=en&origins=10%2BPlace%2Bde%2Bla%2BConcorde,%2B75008%2BParis,%2BFrance&sensor=false&unit=metric
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- Faraday v0.8.9
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
14
|
+
Accept:
|
15
|
+
- "*/*"
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Content-Type:
|
22
|
+
- application/json; charset=UTF-8
|
23
|
+
Date:
|
24
|
+
- Wed, 02 Apr 2014 22:24:09 GMT
|
25
|
+
Expires:
|
26
|
+
- Thu, 03 Apr 2014 22:24:09 GMT
|
27
|
+
Cache-Control:
|
28
|
+
- public, max-age=86400
|
29
|
+
Server:
|
30
|
+
- mafe
|
31
|
+
X-Xss-Protection:
|
32
|
+
- 1; mode=block
|
33
|
+
X-Frame-Options:
|
34
|
+
- SAMEORIGIN
|
35
|
+
Alternate-Protocol:
|
36
|
+
- 443:quic
|
37
|
+
Transfer-Encoding:
|
38
|
+
- chunked
|
39
|
+
body:
|
40
|
+
encoding: UTF-8
|
41
|
+
string: |
|
42
|
+
{
|
43
|
+
"destination_addresses" : [ "1561 Place du Pont Neuf, 75001 Paris, France" ],
|
44
|
+
"origin_addresses" : [ "10 Place de la Concorde, 75008 Paris, France" ],
|
45
|
+
"rows" : [
|
46
|
+
{
|
47
|
+
"elements" : [
|
48
|
+
{
|
49
|
+
"distance" : {
|
50
|
+
"text" : "2.3 km",
|
51
|
+
"value" : 2335
|
52
|
+
},
|
53
|
+
"duration" : {
|
54
|
+
"text" : "5 mins",
|
55
|
+
"value" : 293
|
56
|
+
},
|
57
|
+
"status" : "OK"
|
58
|
+
}
|
59
|
+
]
|
60
|
+
}
|
61
|
+
],
|
62
|
+
"status" : "OK"
|
63
|
+
}
|
64
|
+
http_version:
|
65
|
+
recorded_at: Wed, 02 Apr 2014 22:24:10 GMT
|
66
|
+
recorded_with: VCR 2.9.0
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://maps.googleapis.com/maps/api/distancematrix/json?destinations=48.857066,%202.341138&language=en&origins=10%2BPlace%2Bde%2Bla%2BConcorde,%2B75008%2BParis,%2BFrance&sensor=false&unit=metric
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- Faraday v0.8.9
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
14
|
+
Accept:
|
15
|
+
- "*/*"
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Content-Type:
|
22
|
+
- application/json; charset=UTF-8
|
23
|
+
Date:
|
24
|
+
- Wed, 02 Apr 2014 22:24:09 GMT
|
25
|
+
Expires:
|
26
|
+
- Thu, 03 Apr 2014 22:24:09 GMT
|
27
|
+
Cache-Control:
|
28
|
+
- public, max-age=86400
|
29
|
+
Server:
|
30
|
+
- mafe
|
31
|
+
X-Xss-Protection:
|
32
|
+
- 1; mode=block
|
33
|
+
X-Frame-Options:
|
34
|
+
- SAMEORIGIN
|
35
|
+
Alternate-Protocol:
|
36
|
+
- 443:quic
|
37
|
+
Transfer-Encoding:
|
38
|
+
- chunked
|
39
|
+
body:
|
40
|
+
encoding: UTF-8
|
41
|
+
string: |
|
42
|
+
{
|
43
|
+
"destination_addresses" : [ "1561 Place du Pont Neuf, 75001 Paris, France" ],
|
44
|
+
"origin_addresses" : [ "10 Place de la Concorde, 75008 Paris, France" ],
|
45
|
+
"rows" : [
|
46
|
+
{
|
47
|
+
"elements" : [
|
48
|
+
{
|
49
|
+
"distance" : {
|
50
|
+
"text" : "2.3 km",
|
51
|
+
"value" : 2335
|
52
|
+
},
|
53
|
+
"duration" : {
|
54
|
+
"text" : "5 mins",
|
55
|
+
"value" : 293
|
56
|
+
},
|
57
|
+
"status" : "OK"
|
58
|
+
}
|
59
|
+
]
|
60
|
+
}
|
61
|
+
],
|
62
|
+
"status" : "OK"
|
63
|
+
}
|
64
|
+
http_version:
|
65
|
+
recorded_at: Wed, 02 Apr 2014 22:24:10 GMT
|
66
|
+
recorded_with: VCR 2.9.0
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://maps.googleapis.com/maps/api/distancematrix/json?destinations=48.857066,%202.341138&language=en&origins=10%2BPlace%2Bde%2Bla%2BConcorde,%2B75008%2BParis,%2BFrance&sensor=false&unit=metric
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- Faraday v0.8.9
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
14
|
+
Accept:
|
15
|
+
- "*/*"
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Content-Type:
|
22
|
+
- application/json; charset=UTF-8
|
23
|
+
Date:
|
24
|
+
- Wed, 02 Apr 2014 22:24:10 GMT
|
25
|
+
Expires:
|
26
|
+
- Thu, 03 Apr 2014 22:24:10 GMT
|
27
|
+
Cache-Control:
|
28
|
+
- public, max-age=86400
|
29
|
+
Server:
|
30
|
+
- mafe
|
31
|
+
X-Xss-Protection:
|
32
|
+
- 1; mode=block
|
33
|
+
X-Frame-Options:
|
34
|
+
- SAMEORIGIN
|
35
|
+
Alternate-Protocol:
|
36
|
+
- 443:quic
|
37
|
+
Transfer-Encoding:
|
38
|
+
- chunked
|
39
|
+
body:
|
40
|
+
encoding: UTF-8
|
41
|
+
string: |
|
42
|
+
{
|
43
|
+
"destination_addresses" : [ "1561 Place du Pont Neuf, 75001 Paris, France" ],
|
44
|
+
"origin_addresses" : [ "10 Place de la Concorde, 75008 Paris, France" ],
|
45
|
+
"rows" : [
|
46
|
+
{
|
47
|
+
"elements" : [
|
48
|
+
{
|
49
|
+
"distance" : {
|
50
|
+
"text" : "2.3 km",
|
51
|
+
"value" : 2335
|
52
|
+
},
|
53
|
+
"duration" : {
|
54
|
+
"text" : "5 mins",
|
55
|
+
"value" : 293
|
56
|
+
},
|
57
|
+
"status" : "OK"
|
58
|
+
}
|
59
|
+
]
|
60
|
+
}
|
61
|
+
],
|
62
|
+
"status" : "OK"
|
63
|
+
}
|
64
|
+
http_version:
|
65
|
+
recorded_at: Wed, 02 Apr 2014 22:24:11 GMT
|
66
|
+
recorded_with: VCR 2.9.0
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://maps.googleapis.com/maps/api/distancematrix/json?destinations=48.857066,%202.341138&language=en&origins=10%2BPlace%2Bde%2Bla%2BConcorde,%2B75008%2BParis,%2BFrance&sensor=false&unit=metric
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- Faraday v0.8.9
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
14
|
+
Accept:
|
15
|
+
- "*/*"
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Content-Type:
|
22
|
+
- application/json; charset=UTF-8
|
23
|
+
Date:
|
24
|
+
- Wed, 02 Apr 2014 22:24:10 GMT
|
25
|
+
Expires:
|
26
|
+
- Thu, 03 Apr 2014 22:24:10 GMT
|
27
|
+
Cache-Control:
|
28
|
+
- public, max-age=86400
|
29
|
+
Server:
|
30
|
+
- mafe
|
31
|
+
X-Xss-Protection:
|
32
|
+
- 1; mode=block
|
33
|
+
X-Frame-Options:
|
34
|
+
- SAMEORIGIN
|
35
|
+
Alternate-Protocol:
|
36
|
+
- 443:quic
|
37
|
+
Transfer-Encoding:
|
38
|
+
- chunked
|
39
|
+
body:
|
40
|
+
encoding: UTF-8
|
41
|
+
string: |
|
42
|
+
{
|
43
|
+
"destination_addresses" : [ "1561 Place du Pont Neuf, 75001 Paris, France" ],
|
44
|
+
"origin_addresses" : [ "10 Place de la Concorde, 75008 Paris, France" ],
|
45
|
+
"rows" : [
|
46
|
+
{
|
47
|
+
"elements" : [
|
48
|
+
{
|
49
|
+
"distance" : {
|
50
|
+
"text" : "2.3 km",
|
51
|
+
"value" : 2335
|
52
|
+
},
|
53
|
+
"duration" : {
|
54
|
+
"text" : "5 mins",
|
55
|
+
"value" : 293
|
56
|
+
},
|
57
|
+
"status" : "OK"
|
58
|
+
}
|
59
|
+
]
|
60
|
+
}
|
61
|
+
],
|
62
|
+
"status" : "OK"
|
63
|
+
}
|
64
|
+
http_version:
|
65
|
+
recorded_at: Wed, 02 Apr 2014 22:24:10 GMT
|
66
|
+
recorded_with: VCR 2.9.0
|
data/spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_origin_address.yml
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://maps.googleapis.com/maps/api/distancematrix/json?destinations=48.857066,%202.341138&language=en&origins=10%2BPlace%2Bde%2Bla%2BConcorde,%2B75008%2BParis,%2BFrance&sensor=false&unit=metric
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- Faraday v0.8.9
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
14
|
+
Accept:
|
15
|
+
- "*/*"
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Content-Type:
|
22
|
+
- application/json; charset=UTF-8
|
23
|
+
Date:
|
24
|
+
- Wed, 02 Apr 2014 22:24:08 GMT
|
25
|
+
Expires:
|
26
|
+
- Thu, 03 Apr 2014 22:24:08 GMT
|
27
|
+
Cache-Control:
|
28
|
+
- public, max-age=86400
|
29
|
+
Server:
|
30
|
+
- mafe
|
31
|
+
X-Xss-Protection:
|
32
|
+
- 1; mode=block
|
33
|
+
X-Frame-Options:
|
34
|
+
- SAMEORIGIN
|
35
|
+
Alternate-Protocol:
|
36
|
+
- 443:quic
|
37
|
+
Transfer-Encoding:
|
38
|
+
- chunked
|
39
|
+
body:
|
40
|
+
encoding: UTF-8
|
41
|
+
string: |
|
42
|
+
{
|
43
|
+
"destination_addresses" : [ "1561 Place du Pont Neuf, 75001 Paris, France" ],
|
44
|
+
"origin_addresses" : [ "10 Place de la Concorde, 75008 Paris, France" ],
|
45
|
+
"rows" : [
|
46
|
+
{
|
47
|
+
"elements" : [
|
48
|
+
{
|
49
|
+
"distance" : {
|
50
|
+
"text" : "2.3 km",
|
51
|
+
"value" : 2335
|
52
|
+
},
|
53
|
+
"duration" : {
|
54
|
+
"text" : "5 mins",
|
55
|
+
"value" : 293
|
56
|
+
},
|
57
|
+
"status" : "OK"
|
58
|
+
}
|
59
|
+
]
|
60
|
+
}
|
61
|
+
],
|
62
|
+
"status" : "OK"
|
63
|
+
}
|
64
|
+
http_version:
|
65
|
+
recorded_at: Wed, 02 Apr 2014 22:24:09 GMT
|
66
|
+
recorded_with: VCR 2.9.0
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://maps.googleapis.com/maps/api/distancematrix/json?destinations=48.857066,%202.341138&language=en&origins=10%2BPlace%2Bde%2Bla%2BConcorde,%2B75008%2BParis,%2BFrance&sensor=false&unit=metric
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- Faraday v0.8.9
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
14
|
+
Accept:
|
15
|
+
- "*/*"
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 200
|
19
|
+
message: OK
|
20
|
+
headers:
|
21
|
+
Content-Type:
|
22
|
+
- application/json; charset=UTF-8
|
23
|
+
Date:
|
24
|
+
- Wed, 02 Apr 2014 22:24:10 GMT
|
25
|
+
Expires:
|
26
|
+
- Thu, 03 Apr 2014 22:24:10 GMT
|
27
|
+
Cache-Control:
|
28
|
+
- public, max-age=86400
|
29
|
+
Server:
|
30
|
+
- mafe
|
31
|
+
X-Xss-Protection:
|
32
|
+
- 1; mode=block
|
33
|
+
X-Frame-Options:
|
34
|
+
- SAMEORIGIN
|
35
|
+
Alternate-Protocol:
|
36
|
+
- 443:quic
|
37
|
+
Transfer-Encoding:
|
38
|
+
- chunked
|
39
|
+
body:
|
40
|
+
encoding: UTF-8
|
41
|
+
string: |
|
42
|
+
{
|
43
|
+
"destination_addresses" : [ "1561 Place du Pont Neuf, 75001 Paris, France" ],
|
44
|
+
"origin_addresses" : [ "10 Place de la Concorde, 75008 Paris, France" ],
|
45
|
+
"rows" : [
|
46
|
+
{
|
47
|
+
"elements" : [
|
48
|
+
{
|
49
|
+
"distance" : {
|
50
|
+
"text" : "2.3 km",
|
51
|
+
"value" : 2335
|
52
|
+
},
|
53
|
+
"duration" : {
|
54
|
+
"text" : "5 mins",
|
55
|
+
"value" : 293
|
56
|
+
},
|
57
|
+
"status" : "OK"
|
58
|
+
}
|
59
|
+
]
|
60
|
+
}
|
61
|
+
],
|
62
|
+
"status" : "OK"
|
63
|
+
}
|
64
|
+
http_version:
|
65
|
+
recorded_at: Wed, 02 Apr 2014 22:24:11 GMT
|
66
|
+
recorded_with: VCR 2.9.0
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Gistance::Client::DistanceMatrix do
|
4
|
+
describe '.distance_matrix', :vcr do
|
5
|
+
let(:options) do
|
6
|
+
{
|
7
|
+
origins: ['10 Place de la Concorde, 75008 Paris, France'],
|
8
|
+
destinations: ['48.857066, 2.341138']
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
subject { client.distance_matrix(options) }
|
13
|
+
|
14
|
+
it 'returns an array of origin addresses' do
|
15
|
+
expect(subject.origin_addresses).to be_a Array
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'returns the origin address' do
|
19
|
+
expect(subject.origin_addresses.first).to include 'Concorde'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns an array of destination addresses' do
|
23
|
+
expect(subject.destination_addresses).to be_a Array
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'returns the destination address' do
|
27
|
+
expect(subject.destination_addresses.first).to include 'Pont Neuf'
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns the distance between origin and destination' do
|
31
|
+
expect(subject.rows.first['elements'].first.distance.text).to eql('2.3 km')
|
32
|
+
expect(subject.rows.first['elements'].first.distance.value).to eql(2335)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'returns the travel duration between origin and destination' do
|
36
|
+
expect(subject.rows.first['elements'].first.duration.text).to eql('5 mins')
|
37
|
+
expect(subject.rows.first['elements'].first.duration.value).to eql(293)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Gistance::Client do
|
4
|
+
describe 'module configuration' do
|
5
|
+
before do
|
6
|
+
Gistance.reset!
|
7
|
+
|
8
|
+
Gistance.configure do |config|
|
9
|
+
Gistance::Configuration::VALID_OPTIONS.each do |key|
|
10
|
+
config.send("#{key}=", "Some #{key}")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
after { Gistance.reset! }
|
16
|
+
|
17
|
+
it "inherits the module configuration" do
|
18
|
+
client = Gistance::Client.new
|
19
|
+
|
20
|
+
Gistance::Configuration::VALID_OPTIONS.each do |key|
|
21
|
+
expect(client.instance_variable_get(:"@#{key}")).to eq "Some #{key}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'with class level configuration' do
|
27
|
+
describe 'api_key' do
|
28
|
+
it 'sets api_key' do
|
29
|
+
client = Gistance::Client.new
|
30
|
+
|
31
|
+
expect(client.api_key).to be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'overrides module configuration' do
|
35
|
+
client = Gistance::Client.new(api_key: 'randomkey')
|
36
|
+
|
37
|
+
expect(client.api_key).to eql 'randomkey'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'language' do
|
42
|
+
it 'sets language' do
|
43
|
+
client = Gistance::Client.new
|
44
|
+
|
45
|
+
expect(client.language).to eql 'en'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'overrides module configuration' do
|
49
|
+
client = Gistance::Client.new(language: 'fr')
|
50
|
+
|
51
|
+
expect(client.language).to eql 'fr'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'unit' do
|
56
|
+
it 'sets unit' do
|
57
|
+
client = Gistance::Client.new
|
58
|
+
|
59
|
+
expect(client.unit).to eql 'metric'
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'overrides module configuration' do
|
63
|
+
client = Gistance::Client.new(unit: 'imperial')
|
64
|
+
|
65
|
+
expect(client.unit).to eql 'imperial'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'sensor' do
|
70
|
+
it 'sets sensor' do
|
71
|
+
client = Gistance::Client.new
|
72
|
+
|
73
|
+
expect(client.sensor).to be_false
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'overrides module configuration' do
|
77
|
+
client = Gistance::Client.new(sensor: true)
|
78
|
+
|
79
|
+
expect(client.sensor).to be_true
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Gistance::Request do
|
4
|
+
it_behaves_like 'an error', 'INVALID_REQUEST'
|
5
|
+
it_behaves_like 'an error', 'MAX_ELEMENTS_EXCEEDED'
|
6
|
+
it_behaves_like 'an error', 'OVER_QUERY_LIMIT'
|
7
|
+
it_behaves_like 'an error', 'REQUEST_DENIED'
|
8
|
+
it_behaves_like 'an error', 'UNKNOWN_ERROR'
|
9
|
+
it_behaves_like 'an error', 'NOT_FOUND'
|
10
|
+
it_behaves_like 'an error', 'ZERO_RESULTS'
|
11
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Gistance do
|
4
|
+
describe '.respond_to?' do
|
5
|
+
context 'with an existing Gistance method' do
|
6
|
+
it 'responds to method' do
|
7
|
+
expect(Gistance.respond_to?(:new, true)).to be_true
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'with an existing Gistance::Client method' do
|
12
|
+
it 'responds to method' do
|
13
|
+
expect(Gistance.respond_to?(:request, true)).to be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'delegates to Gistance::Client' do
|
17
|
+
Gistance::Client.stub_chain(:new, :get).and_return(true)
|
18
|
+
|
19
|
+
expect(Gistance::Client).to receive(:new).ordered
|
20
|
+
expect(Gistance::Client).to receive(:new).ordered
|
21
|
+
|
22
|
+
Gistance.get('fuubar')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '.new' do
|
28
|
+
it 'returns a Gistance::Client' do
|
29
|
+
expect(Gistance.new).to be_a Gistance::Client
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'coveralls'
|
3
|
+
|
4
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
5
|
+
SimpleCov::Formatter::HTMLFormatter,
|
6
|
+
Coveralls::SimpleCov::Formatter
|
7
|
+
]
|
8
|
+
|
9
|
+
SimpleCov.start do
|
10
|
+
add_filter 'spec'
|
11
|
+
add_filter 'lib/gistance/response'
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'rspec'
|
15
|
+
require 'vcr'
|
16
|
+
require 'json'
|
17
|
+
require 'webmock/rspec'
|
18
|
+
require 'gistance'
|
19
|
+
|
20
|
+
Dir['./spec/support/**/*.rb'].each { |f| require f }
|
21
|
+
|
22
|
+
WebMock.disable_net_connect!(allow: 'coveralls.io')
|
23
|
+
|
24
|
+
VCR.configure do |c|
|
25
|
+
c.cassette_library_dir = 'spec/cassettes'
|
26
|
+
c.hook_into :webmock
|
27
|
+
c.configure_rspec_metadata!
|
28
|
+
end
|
29
|
+
|
30
|
+
RSpec.configure do |c|
|
31
|
+
c.treat_symbols_as_metadata_keys_with_true_values = true
|
32
|
+
end
|
33
|
+
|
34
|
+
def google_distance_matrix_url
|
35
|
+
"#{Gistance.api_endpoint}?language=en&sensor=false&unit=metric"
|
36
|
+
end
|
37
|
+
|
38
|
+
def client
|
39
|
+
Gistance.new
|
40
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
shared_examples 'an error' do |error|
|
2
|
+
error_class_name = error.split(/_/).map{ |word| word.capitalize }.join('')
|
3
|
+
exception = Gistance.const_get(error_class_name)
|
4
|
+
|
5
|
+
it "raising #{exception.name} when status is #{error}" do
|
6
|
+
VCR.turn_off!
|
7
|
+
|
8
|
+
stub_request(:get, google_distance_matrix_url).to_return(
|
9
|
+
body: { status: error }.to_json
|
10
|
+
)
|
11
|
+
|
12
|
+
expect { client.get }.to raise_error exception
|
13
|
+
|
14
|
+
VCR.turn_on!
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gistance
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aylic Petit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.9
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.9
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.9.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.9.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: hashie
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.0.5
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.0.5
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.8.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.8.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: multi_json
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.9.2
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.9.2
|
83
|
+
description: Simple Ruby wrapper for the Google Distance Matrix API.
|
84
|
+
email:
|
85
|
+
- sush@users.noreply.github.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".yardopts"
|
91
|
+
- CONTRIBUTING.md
|
92
|
+
- LICENSE
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- gistance.gemspec
|
96
|
+
- lib/gistance.rb
|
97
|
+
- lib/gistance/client.rb
|
98
|
+
- lib/gistance/client/distance_matrix.rb
|
99
|
+
- lib/gistance/configuration.rb
|
100
|
+
- lib/gistance/connection.rb
|
101
|
+
- lib/gistance/error.rb
|
102
|
+
- lib/gistance/request.rb
|
103
|
+
- lib/gistance/response/raise_error.rb
|
104
|
+
- lib/gistance/version.rb
|
105
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_an_array_of_destination_addresses.yml
|
106
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_an_array_of_origin_addresses.yml
|
107
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_destination_address.yml
|
108
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_distance_between_origin_and_destination.yml
|
109
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_origin_address.yml
|
110
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_travel_duration_between_origin_and_destination.yml
|
111
|
+
- spec/lib/gistance/client/distance_matrix_spec.rb
|
112
|
+
- spec/lib/gistance/client_spec.rb
|
113
|
+
- spec/lib/gistance/request_spec.rb
|
114
|
+
- spec/lib/gistance_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
- spec/support/shared_examples/error.rb
|
117
|
+
homepage: https://github.com/sush/gistance
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: 1.9.2
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.2.2
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Google Distance Matrix API Ruby wrapper
|
141
|
+
test_files:
|
142
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_an_array_of_destination_addresses.yml
|
143
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_an_array_of_origin_addresses.yml
|
144
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_destination_address.yml
|
145
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_distance_between_origin_and_destination.yml
|
146
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_origin_address.yml
|
147
|
+
- spec/cassettes/Gistance_Client_DistanceMatrix/_distance_matrix/returns_the_travel_duration_between_origin_and_destination.yml
|
148
|
+
- spec/lib/gistance/client/distance_matrix_spec.rb
|
149
|
+
- spec/lib/gistance/client_spec.rb
|
150
|
+
- spec/lib/gistance/request_spec.rb
|
151
|
+
- spec/lib/gistance_spec.rb
|
152
|
+
- spec/spec_helper.rb
|
153
|
+
- spec/support/shared_examples/error.rb
|
154
|
+
has_rdoc:
|