From 5215afb707124eda02a9035a88c530aadc28b60f Mon Sep 17 00:00:00 2001 From: Mike Heft Date: Wed, 3 Jul 2024 14:38:57 -0600 Subject: [PATCH] Add command to call google api for route info --- lib/base_command.rb | 2 +- lib/client/request.rb | 6 +++--- lib/rides/commands/get_direction_data.rb | 18 ++++++++++-------- .../rides/commands/get_direction_data_spec.rb | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 spec/lib/rides/commands/get_direction_data_spec.rb diff --git a/lib/base_command.rb b/lib/base_command.rb index 816afc3..7842910 100644 --- a/lib/base_command.rb +++ b/lib/base_command.rb @@ -2,7 +2,7 @@ class BaseCommand def self.call(**args) - new.call(args) + new.call(**args) end def call(**args) diff --git a/lib/client/request.rb b/lib/client/request.rb index 36e2597..9bc87f6 100644 --- a/lib/client/request.rb +++ b/lib/client/request.rb @@ -1,13 +1,13 @@ module Client class Request - CONNECTION = Faraday.freeze + CONNECTION = Faraday private_constant :CONNECTION - def self.connection(url, params = {}, headers = {}) + def self.connection(url:, params: {}, headers: {}) new(url, params, headers) end - def self.post(url, body = nil, headers = nil) + def post(url, body, headers = nil) connection.post(url, body.to_json, headers) end diff --git a/lib/rides/commands/get_direction_data.rb b/lib/rides/commands/get_direction_data.rb index 8e42607..b2fe2c9 100644 --- a/lib/rides/commands/get_direction_data.rb +++ b/lib/rides/commands/get_direction_data.rb @@ -5,18 +5,20 @@ module Commands class GetDirectionData < BaseCommand DIRECTIONS_API_URL = "https://routes.googleapis.com/directions" COMPUTE_ROUTES_URL = "/v2:computeRoutes" - DEFAULT_HEADERS = { "X-Goog-FieldMask" => "routes.distanceMeters,routes.duration" }.freeze - DEFAULT_PARAMS = { key: ENV["GOOGLE_API_KEY"] }.freeze + DEFAULT_HEADERS = { + "X-Goog-FieldMask" => "routes.distanceMeters,routes.duration", + "X-goog-api-key" => ENV["GOOGLE_API_KEY"], + "Content-Type" => "application/json" + }.freeze - def call(ride) + def call(ride:) data = get_direction_data_for_ride(ride) end private def connection @connection ||= Client::Request.connection( - DIRECTIONS_API_URL, - DEFAULT_PARAMS, - DEFAULT_HEADERS + url: DIRECTIONS_API_URL, + headers: DEFAULT_HEADERS ) end @@ -24,11 +26,11 @@ def call(ride) to_address = ride.to_address from_address = ride.from_address body = { - origin: from_address.full_address, destination: to_address.full_address, + origin: { placeId: from_address.place_id }, destination: { placeId: to_address.place_id }, routingPreference: "TRAFFIC_AWARE", travelMode: "DRIVE" } - connection.post(COMPUTE_ROUTES_URL, body) + connection.post(DIRECTIONS_API_URL + COMPUTE_ROUTES_URL, body) end end end diff --git a/spec/lib/rides/commands/get_direction_data_spec.rb b/spec/lib/rides/commands/get_direction_data_spec.rb new file mode 100644 index 0000000..953f86e --- /dev/null +++ b/spec/lib/rides/commands/get_direction_data_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +RSpec.describe Rides::Commands::GetDirectionData do + it "gets data for an address" do + VCR.use_cassette("first_ride_directions") do + from_address = create( + :address, :with_out_place_id, line_1: "711 Oval Drive", city: "Fort Collins", state: "CO", + zip_code: "80521" + ) + to_address = create( + :address, :with_out_place_id, line_1: "151 N College Ave", city: "Fort Collins", state: "CO", + zip_code: "80524" + ) + ride = create(:ride, from_address:, to_address:) + data = described_class.call(ride:) + binding.pry + end + end +end