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



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

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.



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

def connection
  @connection
end

#proto_connectionObject (readonly)

Returns the value of attribute proto_connection.



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

def proto_connection
  @proto_connection
end

Instance Method Details

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



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

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



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

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



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/travel_time/client.rb', line 217

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



230
231
232
233
234
235
236
237
238
239
# File 'lib/travel_time/client.rb', line 230

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



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/travel_time/client.rb', line 194

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



206
207
208
209
210
211
212
213
214
215
# File 'lib/travel_time/client.rb', line 206

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



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

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



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

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



73
74
75
# File 'lib/travel_time/client.rb', line 73

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

#perform_requestObject



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

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



67
68
69
70
71
# File 'lib/travel_time/client.rb', line 67

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



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

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



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

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



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

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

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



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

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



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

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



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/travel_time/client.rb', line 147

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("http://proto.api.traveltimeapp.com/api/v3/#{country}/time-filter/fast/#{transport_obj.url_name}",
                          payload)
  end
end

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



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

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



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

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



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

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



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

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



123
124
125
126
127
128
# File 'lib/travel_time/client.rb', line 123

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



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

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

#unwrap_proto(response) ⇒ Object



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

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