Skip to content

Commit

Permalink
Add command to call google api for route info
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeheft committed Jul 3, 2024
1 parent ee75fd3 commit 5215afb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/base_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class BaseCommand
def self.call(**args)
new.call(args)
new.call(**args)
end

def call(**args)
Expand Down
6 changes: 3 additions & 3 deletions lib/client/request.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 10 additions & 8 deletions lib/rides/commands/get_direction_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@ 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

private def get_direction_data_for_ride(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
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/rides/commands/get_direction_data_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5215afb

Please sign in to comment.