Class: TravelTime::Client

Inherits:
Object
  • Object
show all
Extended by:
Limiter::Mixin
Defined in:
lib/travel_time/client.rb

Overview

The Client class provides the main interface to interact with the TravelTime API

Constant Summary collapse

API_BASE_URL =
'https://api.traveltimeapp.com/v4/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rate_limit = nil) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
# File 'lib/travel_time/client.rb', line 16

def initialize(rate_limit = nil)
  init_connection
  init_proto_connection

  return unless rate_limit

  %i[perform_request perform_request_proto].each do |method_name|
    self.class.limit_method method_name, balanced: true, rate: rate_limit
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/travel_time/client.rb', line 14

def connection
  @connection
end

#proto_connectionObject (readonly)

Returns the value of attribute proto_connection.



14
15
16
# File 'lib/travel_time/client.rb', line 14

def proto_connection
  @proto_connection
end

Instance Method Details

#distance_map(departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil, format: nil) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/travel_time/client.rb', line 111

def distance_map(departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil, format: nil)
  payload = {
    departure_searches: departure_searches,
    arrival_searches: arrival_searches,
    unions: unions,
    intersections: intersections
  }.compact
  perform_request { connection.post('distance-map', payload, { 'Accept' => format }) }
end

#geocoding(query:, within_country: nil, format_name: nil, exclude: nil, limit: nil, force_postcode: nil, bounds: nil, accept_language: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/travel_time/client.rb', line 79

def geocoding(query:, within_country: nil, format_name: nil, exclude: nil, limit: nil, force_postcode: nil,
              bounds: nil, accept_language: nil)
  query = {
    query: query,
    'within.country': within_country.is_a?(Array) ? within_country.join(',') : within_country,
    'format.name': format_name,
    'format.exclude.country': exclude,
    limit: limit,
    'force.add.postcode': force_postcode,
    bounds: bounds&.join(',')
  }.compact
  perform_request { connection.get('geocoding/search', query, { 'Accept-Language' => accept_language }) }
end

#init_connectionObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/travel_time/client.rb', line 27

def init_connection
  @connection = Faraday.new(API_BASE_URL) do |f|
    f.request :json
    f.response :raise_error if TravelTime.config.raise_on_failure
    f.response :logger if TravelTime.config.enable_logging
    f.response :json
    f.use TravelTime::Middleware::Authentication
    f.adapter TravelTime.config.http_adapter || Faraday.default_adapter
  end
end

#init_proto_connectionObject



38
39
40
41
42
43
44
45
# File 'lib/travel_time/client.rb', line 38

def init_proto_connection
  @proto_connection = Faraday.new do |f|
    f.use TravelTime::Middleware::ProtoMiddleware
    f.response :raise_error if TravelTime.config.raise_on_failure
    f.response :logger if TravelTime.config.enable_logging
    f.adapter TravelTime.config.http_adapter || Faraday.default_adapter
  end
end

#map_infoObject



71
72
73
# File 'lib/travel_time/client.rb', line 71

def map_info
  perform_request { connection.get('map-info') }
end

#perform_requestObject



55
56
57
58
59
60
61
62
63
# File 'lib/travel_time/client.rb', line 55

def perform_request
  unwrap(yield)
rescue Faraday::Error => e
  raise TravelTime::Error.new(response: Response.from_hash(e.response)) if e.response

  raise TravelTime::Error.new(exception: e)
rescue StandardError => e
  raise TravelTime::Error.new(exception: e)
end

#perform_request_protoObject



65
66
67
68
69
# File 'lib/travel_time/client.rb', line 65

def perform_request_proto
  unwrap_proto(yield)
rescue StandardError => e
  raise TravelTime::Error.new(exception: e)
end

#reverse_geocoding(lat:, lng:, accept_language: nil) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/travel_time/client.rb', line 93

def reverse_geocoding(lat:, lng:, accept_language: nil)
  query = {
    lat: lat,
    lng: lng
  }.compact
  perform_request { connection.get('geocoding/reverse', query, { 'Accept-Language' => accept_language }) }
end

#routes(locations:, departure_searches: nil, arrival_searches: nil) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/travel_time/client.rb', line 187

def routes(locations:, departure_searches: nil, arrival_searches: nil)
  payload = {
    locations: locations,
    departure_searches: departure_searches,
    arrival_searches: arrival_searches
  }.compact
  perform_request { connection.post('routes', payload) }
end

#supported_locations(locations:) ⇒ Object



75
76
77
# File 'lib/travel_time/client.rb', line 75

def supported_locations(locations:)
  perform_request { connection.post('supported-locations', { locations: locations }) }
end

#time_filter(locations:, departure_searches: nil, arrival_searches: nil) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/travel_time/client.rb', line 128

def time_filter(locations:, departure_searches: nil, arrival_searches: nil)
  payload = {
    locations: locations,
    departure_searches: departure_searches,
    arrival_searches: arrival_searches
  }.compact
  perform_request { connection.post('time-filter', payload) }
end

#time_filter_fast(locations:, arrival_searches:) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/travel_time/client.rb', line 137

def time_filter_fast(locations:, arrival_searches:)
  payload = {
    locations: locations,
    arrival_searches: arrival_searches
  }.compact
  perform_request { connection.post('time-filter/fast', payload) }
end

#time_filter_fast_proto(country:, origin:, destinations:, transport:, traveltime:) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/travel_time/client.rb', line 145

def time_filter_fast_proto(country:, origin:, destinations:, transport:, traveltime:)
  message = ProtoUtils.make_proto_message(origin, destinations, transport, traveltime)
  payload = ProtoUtils.encode_proto_message(message)
  perform_request_proto do
    proto_connection.post("http://proto.api.traveltimeapp.com/api/v2/#{country}/time-filter/fast/#{transport}",
                          payload)
  end
end

#time_filter_fast_proto_distance(country:, origin:, destinations:, transport:, traveltime:) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/travel_time/client.rb', line 154

def time_filter_fast_proto_distance(country:, origin:, destinations:, transport:, traveltime:)
  message = ProtoUtils.make_proto_message(origin, destinations, transport, traveltime, properties: [1])
  payload = ProtoUtils.encode_proto_message(message)
  perform_request_proto do
    proto_connection.post("https://proto-with-distance.api.traveltimeapp.com/api/v2/#{country}/time-filter/fast/#{transport}",
                          payload)
  end
end

#time_filter_postcode_districts(departure_searches: nil, arrival_searches: nil) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/travel_time/client.rb', line 171

def time_filter_postcode_districts(departure_searches: nil, arrival_searches: nil)
  payload = {
    departure_searches: departure_searches,
    arrival_searches: arrival_searches
  }.compact
  perform_request { connection.post('time-filter/postcode-districts', payload) }
end

#time_filter_postcode_sectors(departure_searches: nil, arrival_searches: nil) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/travel_time/client.rb', line 179

def time_filter_postcode_sectors(departure_searches: nil, arrival_searches: nil)
  payload = {
    departure_searches: departure_searches,
    arrival_searches: arrival_searches
  }.compact
  perform_request { connection.post('time-filter/postcode-sectors', payload) }
end

#time_filter_postcodes(departure_searches: nil, arrival_searches: nil) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/travel_time/client.rb', line 163

def time_filter_postcodes(departure_searches: nil, arrival_searches: nil)
  payload = {
    departure_searches: departure_searches,
    arrival_searches: arrival_searches
  }.compact
  perform_request { connection.post('time-filter/postcodes', payload) }
end

#time_map(departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil, format: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/travel_time/client.rb', line 101

def time_map(departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil, format: nil)
  payload = {
    departure_searches: departure_searches,
    arrival_searches: arrival_searches,
    unions: unions,
    intersections: intersections
  }.compact
  perform_request { connection.post('time-map', payload, { 'Accept' => format }) }
end

#time_map_fast(arrival_searches:, format: nil) ⇒ Object



121
122
123
124
125
126
# File 'lib/travel_time/client.rb', line 121

def time_map_fast(arrival_searches:, format: nil)
  payload = {
    arrival_searches: arrival_searches
  }.compact
  perform_request { connection.post('time-map/fast', payload, { 'Accept' => format }) }
end

#unwrap(response) ⇒ Object



47
48
49
# File 'lib/travel_time/client.rb', line 47

def unwrap(response)
  Response.from_object(response)
end

#unwrap_proto(response) ⇒ Object



51
52
53
# File 'lib/travel_time/client.rb', line 51

def unwrap_proto(response)
  Response.from_object_proto(response)
end