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/'
PROTO_BASE_URL =
'https://proto.api.traveltimeapp.com/api/v3/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rate_limit = nil) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
# File 'lib/travel_time/client.rb', line 19

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.



17
18
19
# File 'lib/travel_time/client.rb', line 17

def connection
  @connection
end

#proto_connectionObject (readonly)

Returns the value of attribute proto_connection.



17
18
19
# File 'lib/travel_time/client.rb', line 17

def proto_connection
  @proto_connection
end

Instance Method Details

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



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

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



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/travel_time/client.rb', line 86

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

#geohash(resolution:, properties:, departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/travel_time/client.rb', line 223

def geohash(resolution:, properties:, departure_searches: nil, arrival_searches: nil, unions: nil,
            intersections: nil)
  payload = {
    resolution: resolution,
    properties: properties,
    departure_searches: departure_searches,
    arrival_searches: arrival_searches,
    unions: unions,
    intersections: intersections
  }.compact
  perform_request { connection.post('geohash', payload) }
end

#geohash_fast(resolution:, properties:, arrival_searches:, unions: nil, intersections: nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/travel_time/client.rb', line 236

def geohash_fast(resolution:, properties:, arrival_searches:, unions: nil, intersections: nil)
  payload = {
    resolution: resolution,
    properties: properties,
    arrival_searches: arrival_searches,
    unions: unions,
    intersections: intersections
  }.compact
  perform_request { connection.post('geohash/fast', payload) }
end

#h3(resolution:, properties:, departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/travel_time/client.rb', line 200

def h3(resolution:, properties:, departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil)
  payload = {
    resolution: resolution,
    properties: properties,
    departure_searches: departure_searches,
    arrival_searches: arrival_searches,
    unions: unions,
    intersections: intersections
  }.compact
  perform_request { connection.post('h3', payload) }
end

#h3_fast(resolution:, properties:, arrival_searches:, unions: nil, intersections: nil) ⇒ Object



212
213
214
215
216
217
218
219
220
221
# File 'lib/travel_time/client.rb', line 212

def h3_fast(resolution:, properties:, arrival_searches:, unions: nil, intersections: nil)
  payload = {
    resolution: resolution,
    properties: properties,
    arrival_searches: arrival_searches,
    unions: unions,
    intersections: intersections
  }.compact
  perform_request { connection.post('h3/fast', payload) }
end

#init_connectionObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/travel_time/client.rb', line 30

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



41
42
43
44
45
46
47
48
# File 'lib/travel_time/client.rb', line 41

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

#map_infoObject



78
79
80
# File 'lib/travel_time/client.rb', line 78

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

#perform_requestObject



58
59
60
61
62
63
64
65
66
# File 'lib/travel_time/client.rb', line 58

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

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

#perform_request_protoObject



68
69
70
71
72
73
74
75
76
# File 'lib/travel_time/client.rb', line 68

def perform_request_proto
  unwrap_proto(yield)
rescue Faraday::Error => e
  raise TravelTime::Error.new(response: Response.from_proto_error(e.response), exception: e) if e.response

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

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



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

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



191
192
193
194
195
196
197
198
# File 'lib/travel_time/client.rb', line 191

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



82
83
84
# File 'lib/travel_time/client.rb', line 82

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

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



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

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



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

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:, with_distance: false, request_type: nil) ⇒ Object



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

def time_filter_fast_proto(country:, origin:, destinations:, transport:, traveltime:, with_distance: false,
                           request_type: nil)
  transport_obj = Transport.new(transport)
  distance_prop = Com::Igeolise::Traveltime::Rabbitmq::Requests::TimeFilterFastRequest::Property::DISTANCES
  properties = with_distance ? [distance_prop] : nil
  message = ProtoUtils.make_proto_message(origin, destinations, transport_obj, traveltime,
                                          properties: properties, request_type: request_type)
  payload = ProtoUtils.encode_proto_message(message)
  perform_request_proto do
    proto_connection.post("#{PROTO_BASE_URL}#{country}/time-filter/fast/#{transport_obj.url_name}", payload)
  end
end

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



175
176
177
178
179
180
181
# File 'lib/travel_time/client.rb', line 175

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



183
184
185
186
187
188
189
# File 'lib/travel_time/client.rb', line 183

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



167
168
169
170
171
172
173
# File 'lib/travel_time/client.rb', line 167

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



108
109
110
111
112
113
114
115
116
# File 'lib/travel_time/client.rb', line 108

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:, unions: nil, intersections: nil, format: nil) ⇒ Object



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

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

#unwrap(response) ⇒ Object



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

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

#unwrap_proto(response) ⇒ Object



54
55
56
# File 'lib/travel_time/client.rb', line 54

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