travel_time 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +82 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/travel_time/client.rb +127 -0
- data/lib/travel_time/error.rb +21 -0
- data/lib/travel_time/middleware/authentication.rb +19 -0
- data/lib/travel_time/response.rb +36 -0
- data/lib/travel_time/version.rb +5 -0
- data/lib/travel_time.rb +21 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f918df7122a8d61288cb1f7b35ba225db9bada0dd5fadeeba2eda1bacc274ab9
|
4
|
+
data.tar.gz: 7974b295fd53a2cae24b19903aade4677784dcc375da89ad79e0c79865c83669
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f24c74fd8d0d3ab608b9954388fdcf77b6fbc48c1043941dadf93ec04f67c95be539ea234dd0bf386cb8e7c8ea36717fac4cdb1de494e1a839bac358f72aa42
|
7
|
+
data.tar.gz: af39ca592a3a8c016b91adf8832f1de9dbb9ec514f29a18acc6b39ef6087a5b9d4f6f67c8c583b341793541ff30b149b84d17a1ebf9091926bcf76ee4845cecf
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# TravelTime Ruby SDK
|
2
|
+
|
3
|
+
This open-source library allows you to access [TravelTime API](http://docs.traveltime.com/overview/introduction)
|
4
|
+
endpoints.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'travel_time'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle install
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install travel_time
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
In order to be able to call the API, you'll first need to set your Application ID and API Key:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
TravelTime.configure do |config|
|
28
|
+
config.application_id = '<your app id>'
|
29
|
+
config.api_key = '<your api key>'
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
After that, you can instantiate a client to initiate the API connection:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
client = TimeTravel::Client.new
|
37
|
+
```
|
38
|
+
|
39
|
+
You can then use the clint to call API endpoints:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
response = client.map_info
|
43
|
+
#=> #<TravelTime::Response:0x00000001452e94b0 @status=200, @headers={...}, @body={...}
|
44
|
+
```
|
45
|
+
|
46
|
+
### A note on Time
|
47
|
+
|
48
|
+
If you're calling an API endpoint that expects a time in "extended ISO-8601 format" (
|
49
|
+
e.g. `departure_searches.departure_time`), you can use the standard Ruby Time serializer:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# This require will add the #iso8601 method to Time objects
|
53
|
+
require 'time'
|
54
|
+
|
55
|
+
departure_search = {
|
56
|
+
id: "forward search example",
|
57
|
+
departure_location_id: "London center",
|
58
|
+
arrival_location_ids: ["Hyde Park", "ZSL London Zoo"],
|
59
|
+
transportation: { type: "bus" },
|
60
|
+
departure_time: Time.now.iso8601,
|
61
|
+
travel_time: 1800,
|
62
|
+
properties: ["travel_time"],
|
63
|
+
range: { enabled: true, max_results: 3, width: 600 }
|
64
|
+
}
|
65
|
+
|
66
|
+
client.time_map(departure_searches: [departure_search])
|
67
|
+
```
|
68
|
+
|
69
|
+
## Development
|
70
|
+
|
71
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
72
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
73
|
+
|
74
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
75
|
+
|
76
|
+
To release a new version, update the version number in `version.rb` and then create a GitHub release. This will trigger
|
77
|
+
a GitHub Action which will push the `.gem` file to [rubygems.org](https://rubygems.org).
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/traveltime-dev/travel_time.
|
82
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'travel_time'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require 'travel_time/middleware/authentication'
|
5
|
+
|
6
|
+
module TravelTime
|
7
|
+
# The Client class provides the main interface to interact with the TravelTime API
|
8
|
+
class Client # rubocop:disable Metrics/ClassLength
|
9
|
+
API_BASE_URL = 'https://api.traveltimeapp.com/v4/'
|
10
|
+
|
11
|
+
attr_reader :connection
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@connection = Faraday.new(API_BASE_URL) do |f|
|
15
|
+
f.request :json
|
16
|
+
f.response :raise_error if TravelTime.config.raise_on_failure
|
17
|
+
f.response :logger if TravelTime.config.enable_logging
|
18
|
+
f.response :json
|
19
|
+
f.use TravelTime::Middleware::Authentication
|
20
|
+
f.adapter TravelTime.config.http_adapter || Faraday.default_adapter
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def unwrap(response)
|
25
|
+
Response.from_object(response)
|
26
|
+
end
|
27
|
+
|
28
|
+
def perform_request
|
29
|
+
unwrap(yield)
|
30
|
+
rescue Faraday::Error => e
|
31
|
+
raise TravelTime::Error.new(response: Response.from_hash(e.response)) if e.response
|
32
|
+
|
33
|
+
raise TravelTime::Error.new(exception: e)
|
34
|
+
rescue StandardError => e
|
35
|
+
raise TravelTime::Error.new(exception: e)
|
36
|
+
end
|
37
|
+
|
38
|
+
def map_info
|
39
|
+
perform_request { connection.get('map-info') }
|
40
|
+
end
|
41
|
+
|
42
|
+
def supported_locations(locations:)
|
43
|
+
perform_request { connection.post('supported-locations', { locations: locations }) }
|
44
|
+
end
|
45
|
+
|
46
|
+
def geocoding(query:, within_country: nil, exclude: nil, limit: nil, force_postcode: nil)
|
47
|
+
query = {
|
48
|
+
query: query,
|
49
|
+
'within.country': within_country,
|
50
|
+
'exclude.location.types': exclude,
|
51
|
+
limit: limit,
|
52
|
+
'force.add.postcode': force_postcode
|
53
|
+
}.compact!
|
54
|
+
perform_request { connection.get('geocoding/search', query) }
|
55
|
+
end
|
56
|
+
|
57
|
+
def reverse_geocoding(lat:, lng:, within_country: nil, exclude: nil)
|
58
|
+
query = {
|
59
|
+
lat: lat,
|
60
|
+
lng: lng,
|
61
|
+
'within.country': within_country,
|
62
|
+
'exclude.location.types': exclude
|
63
|
+
}.compact!
|
64
|
+
perform_request { connection.get('geocoding/reverse', query) }
|
65
|
+
end
|
66
|
+
|
67
|
+
def time_map(departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil)
|
68
|
+
payload = {
|
69
|
+
departure_searches: departure_searches,
|
70
|
+
arrival_searches: arrival_searches,
|
71
|
+
unions: unions,
|
72
|
+
intersections: intersections
|
73
|
+
}.compact!
|
74
|
+
perform_request { connection.post('time-map', payload) }
|
75
|
+
end
|
76
|
+
|
77
|
+
def time_filter(locations:, departure_searches: nil, arrival_searches: nil)
|
78
|
+
payload = {
|
79
|
+
locations: locations,
|
80
|
+
departure_searches: departure_searches,
|
81
|
+
arrival_searches: arrival_searches
|
82
|
+
}.compact!
|
83
|
+
perform_request { connection.post('time-filter', payload) }
|
84
|
+
end
|
85
|
+
|
86
|
+
def time_filter_fast(locations:, arrival_searches:)
|
87
|
+
payload = {
|
88
|
+
locations: locations,
|
89
|
+
arrival_searches: arrival_searches
|
90
|
+
}.compact!
|
91
|
+
perform_request { connection.post('time-filter/fast', payload) }
|
92
|
+
end
|
93
|
+
|
94
|
+
def time_filter_postcodes(departure_searches: nil, arrival_searches: nil)
|
95
|
+
payload = {
|
96
|
+
departure_searches: departure_searches,
|
97
|
+
arrival_searches: arrival_searches
|
98
|
+
}.compact!
|
99
|
+
perform_request { connection.post('time-filter/postcodes', payload) }
|
100
|
+
end
|
101
|
+
|
102
|
+
def time_filter_postcode_districts(departure_searches: nil, arrival_searches: nil)
|
103
|
+
payload = {
|
104
|
+
departure_searches: departure_searches,
|
105
|
+
arrival_searches: arrival_searches
|
106
|
+
}.compact!
|
107
|
+
perform_request { connection.post('time-filter/postcode-districts', payload) }
|
108
|
+
end
|
109
|
+
|
110
|
+
def time_filter_postcode_sectors(departure_searches: nil, arrival_searches: nil)
|
111
|
+
payload = {
|
112
|
+
departure_searches: departure_searches,
|
113
|
+
arrival_searches: arrival_searches
|
114
|
+
}.compact!
|
115
|
+
perform_request { connection.post('time-filter/postcode-sectors', payload) }
|
116
|
+
end
|
117
|
+
|
118
|
+
def routes(locations:, departure_searches: nil, arrival_searches: nil)
|
119
|
+
payload = {
|
120
|
+
locations: locations,
|
121
|
+
departure_searches: departure_searches,
|
122
|
+
arrival_searches: arrival_searches
|
123
|
+
}.compact!
|
124
|
+
perform_request { connection.post('routes', payload) }
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TravelTime
|
4
|
+
# The Error class wraps exceptions and provide useful information.
|
5
|
+
class Error < StandardError
|
6
|
+
DEFAULT_MESSAGE = 'Error while processing the request'
|
7
|
+
|
8
|
+
attr_reader :response, :wrapped_exception
|
9
|
+
|
10
|
+
def initialize(message = nil, response: nil, exception: nil)
|
11
|
+
@response = response
|
12
|
+
@wrapped_exception = exception
|
13
|
+
exc = super(parse_message(message))
|
14
|
+
exc.set_backtrace(exception.backtrace) unless exception.nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse_message(message)
|
18
|
+
message || wrapped_exception&.message || response&.body&.[]('description') || DEFAULT_MESSAGE
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module TravelTime
|
6
|
+
module Middleware
|
7
|
+
# The Authentication middleware is responsible for setting the auth headers
|
8
|
+
# on each request. These are automatically taken from the `TravelTime.config`.
|
9
|
+
class Authentication < Faraday::Middleware
|
10
|
+
APP_ID_HEADER = 'X-Application-Id'
|
11
|
+
API_KEY_HEADER = 'X-Api-Key'
|
12
|
+
|
13
|
+
def on_request(env)
|
14
|
+
env.request_headers[APP_ID_HEADER] = TravelTime.config.application_id
|
15
|
+
env.request_headers[API_KEY_HEADER] = TravelTime.config.api_key
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module TravelTime
|
6
|
+
# The Response class represent an API response.
|
7
|
+
class Response
|
8
|
+
attr_reader :status, :body, :headers
|
9
|
+
|
10
|
+
def self.from_object(response)
|
11
|
+
new(
|
12
|
+
status: response.status,
|
13
|
+
headers: response.headers,
|
14
|
+
body: response.body
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.from_hash(response)
|
19
|
+
new(
|
20
|
+
status: response[:status],
|
21
|
+
headers: response[:headers],
|
22
|
+
body: response[:body]
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(status: nil, headers: nil, body: nil)
|
27
|
+
@status = status
|
28
|
+
@headers = headers
|
29
|
+
@body = body
|
30
|
+
end
|
31
|
+
|
32
|
+
def success?
|
33
|
+
(200..299).cover?(status)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/travel_time.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dry/configurable'
|
4
|
+
require 'travel_time/client'
|
5
|
+
require 'travel_time/error'
|
6
|
+
require 'travel_time/response'
|
7
|
+
require 'travel_time/version'
|
8
|
+
|
9
|
+
# Main TravelTime module
|
10
|
+
module TravelTime
|
11
|
+
extend Dry::Configurable
|
12
|
+
|
13
|
+
# Authentication
|
14
|
+
setting :application_id
|
15
|
+
setting :api_key
|
16
|
+
|
17
|
+
# HTTP Client
|
18
|
+
setting :http_adapter
|
19
|
+
setting :enable_logging, default: false
|
20
|
+
setting :raise_on_failure, default: false
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: travel_time
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TravelTime Team
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-configurable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.14.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.14.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '3.0'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '1.10'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.0'
|
47
|
+
description: TravelTime SDK for Ruby programming language
|
48
|
+
email:
|
49
|
+
- support@traveltime.com
|
50
|
+
executables:
|
51
|
+
- console
|
52
|
+
- setup
|
53
|
+
extensions: []
|
54
|
+
extra_rdoc_files: []
|
55
|
+
files:
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- bin/console
|
59
|
+
- bin/setup
|
60
|
+
- lib/travel_time.rb
|
61
|
+
- lib/travel_time/client.rb
|
62
|
+
- lib/travel_time/error.rb
|
63
|
+
- lib/travel_time/middleware/authentication.rb
|
64
|
+
- lib/travel_time/response.rb
|
65
|
+
- lib/travel_time/version.rb
|
66
|
+
homepage: https://traveltime.com
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata:
|
70
|
+
homepage_uri: https://traveltime.com
|
71
|
+
source_code_uri: https://github.com/traveltime-dev/traveltime-sdk-ruby
|
72
|
+
changelog_uri: https://github.com/traveltime-dev/traveltime-sdk-ruby/releases
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 2.7.0
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubygems_version: 3.1.6
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: TravelTime SDK for Ruby programming language
|
92
|
+
test_files: []
|