Class: TravelTime::Client
- Inherits:
-
Object
- Object
- TravelTime::Client
- 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
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#proto_connection ⇒ Object
readonly
Returns the value of attribute proto_connection.
Instance Method Summary collapse
- #distance_map(departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil, format: nil) ⇒ Object
- #geocoding(query:, within_country: nil, format_name: nil, exclude: nil, limit: nil, force_postcode: nil, bounds: nil, accept_language: nil) ⇒ Object
- #init_connection ⇒ Object
- #init_proto_connection ⇒ Object
-
#initialize(rate_limit = nil) ⇒ Client
constructor
A new instance of Client.
- #map_info ⇒ Object
- #perform_request ⇒ Object
- #perform_request_proto ⇒ Object
- #reverse_geocoding(lat:, lng:, accept_language: nil) ⇒ Object
- #routes(locations:, departure_searches: nil, arrival_searches: nil) ⇒ Object
- #supported_locations(locations:) ⇒ Object
- #time_filter(locations:, departure_searches: nil, arrival_searches: nil) ⇒ Object
- #time_filter_fast(locations:, arrival_searches:) ⇒ Object
- #time_filter_fast_proto(country:, origin:, destinations:, transport:, traveltime:) ⇒ Object
- #time_filter_fast_proto_distance(country:, origin:, destinations:, transport:, traveltime:) ⇒ Object
- #time_filter_postcode_districts(departure_searches: nil, arrival_searches: nil) ⇒ Object
- #time_filter_postcode_sectors(departure_searches: nil, arrival_searches: nil) ⇒ Object
- #time_filter_postcodes(departure_searches: nil, arrival_searches: nil) ⇒ Object
- #time_map(departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil, format: nil) ⇒ Object
- #time_map_fast(arrival_searches:, format: nil) ⇒ Object
- #unwrap(response) ⇒ Object
- #unwrap_proto(response) ⇒ Object
Constructor Details
#initialize(rate_limit = nil) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/travel_time/client.rb', line 17 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
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
15 16 17 |
# File 'lib/travel_time/client.rb', line 15 def connection @connection end |
#proto_connection ⇒ Object (readonly)
Returns the value of attribute proto_connection.
15 16 17 |
# File 'lib/travel_time/client.rb', line 15 def proto_connection @proto_connection end |
Instance Method Details
#distance_map(departure_searches: nil, arrival_searches: nil, unions: nil, intersections: nil, format: nil) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/travel_time/client.rb', line 112 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
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/travel_time/client.rb', line 80 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_connection ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/travel_time/client.rb', line 28 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_connection ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/travel_time/client.rb', line 39 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_info ⇒ Object
72 73 74 |
# File 'lib/travel_time/client.rb', line 72 def map_info perform_request { connection.get('map-info') } end |
#perform_request ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/travel_time/client.rb', line 56 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_proto ⇒ Object
66 67 68 69 70 |
# File 'lib/travel_time/client.rb', line 66 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
94 95 96 97 98 99 100 |
# File 'lib/travel_time/client.rb', line 94 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
190 191 192 193 194 195 196 197 |
# File 'lib/travel_time/client.rb', line 190 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
76 77 78 |
# File 'lib/travel_time/client.rb', line 76 def supported_locations(locations:) perform_request { connection.post('supported-locations', { locations: locations }) } end |
#time_filter(locations:, departure_searches: nil, arrival_searches: nil) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/travel_time/client.rb', line 129 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
138 139 140 141 142 143 144 |
# File 'lib/travel_time/client.rb', line 138 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
146 147 148 149 150 151 152 153 154 |
# File 'lib/travel_time/client.rb', line 146 def time_filter_fast_proto(country:, origin:, destinations:, transport:, traveltime:) transport_obj = Transport.new(transport) = ProtoUtils.(origin, destinations, transport_obj, traveltime) payload = ProtoUtils.() perform_request_proto do proto_connection.post("http://proto.api.traveltimeapp.com/api/v2/#{country}/time-filter/fast/#{transport_obj.url_name}", payload) end end |
#time_filter_fast_proto_distance(country:, origin:, destinations:, transport:, traveltime:) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/travel_time/client.rb', line 156 def time_filter_fast_proto_distance(country:, origin:, destinations:, transport:, traveltime:) transport_obj = Transport.new(transport) = ProtoUtils.(origin, destinations, transport_obj, traveltime, properties: [1]) payload = ProtoUtils.() perform_request_proto do proto_connection.post("https://proto-with-distance.api.traveltimeapp.com/api/v2/#{country}/time-filter/fast/#{transport_obj.url_name}", payload) end end |
#time_filter_postcode_districts(departure_searches: nil, arrival_searches: nil) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/travel_time/client.rb', line 174 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
182 183 184 185 186 187 188 |
# File 'lib/travel_time/client.rb', line 182 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
166 167 168 169 170 171 172 |
# File 'lib/travel_time/client.rb', line 166 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
102 103 104 105 106 107 108 109 110 |
# File 'lib/travel_time/client.rb', line 102 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
122 123 124 125 126 127 |
# File 'lib/travel_time/client.rb', line 122 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
48 49 50 |
# File 'lib/travel_time/client.rb', line 48 def unwrap(response) Response.from_object(response) end |
#unwrap_proto(response) ⇒ Object
52 53 54 |
# File 'lib/travel_time/client.rb', line 52 def unwrap_proto(response) Response.from_object_proto(response) end |