Skip to content

Commit

Permalink
Merge pull request #14 from kbhoffmann/formattime
Browse files Browse the repository at this point in the history
Format travel time to human readable string
  • Loading branch information
kbhoffmann authored Mar 9, 2022
2 parents 58610c5 + 43f3994 commit e753f07
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 8 additions & 1 deletion app/poros/trip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ def initialize(trip_data, origin, destination)
@end_city = destination
@travel_time = route_available(trip_data)
@travel_hours = route_available(trip_data)

end

def route_available(trip_data)
if trip_data[:routeError][:errorCode] == 2
@travel_time = "Impossible"
@travel_hours = "Impossible"
else
@travel_time = trip_data[:formattedTime]
@travel_time = format_time(trip_data)
@travel_hours = trip_data[:realTime] / 3600
end
end

def format_time(trip_data)
hours = trip_data[:formattedTime][0] + trip_data[:formattedTime][1]
minutes = trip_data[:formattedTime][3] + trip_data[:formattedTime][4]
"#{hours} hours, #{minutes} minutes"
end
end
20 changes: 19 additions & 1 deletion spec/poros/trip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
:formattedTime=>"14:59:49",
:routeError=>{:errorCode=> -400, :message=>""}
}

trip = Trip.new(trip_data, origin, destination)

expect(trip).to be_an_instance_of(Trip)
expect(trip.start_city).to eq("Denver, CO")
expect(trip.end_city).to eq("Milwaukee, WI")
expect(trip.travel_time).to eq("14:59:49")
expect(trip.travel_time).to eq("14 hours, 59 minutes")
expect(trip.travel_hours).to eq(15)
end

Expand All @@ -27,10 +28,27 @@
trip_data = {:routeError=>{:errorCode=>2, :message=>""}}

trip = Trip.new(trip_data, origin, destination)

expect(trip).to be_an_instance_of(Trip)
expect(trip.start_city).to eq("Denver, CO")
expect(trip.end_city).to eq("Zurich, Switzerland")
expect(trip.travel_time).to eq("Impossible")
expect(trip.travel_hours).to eq("Impossible")
end

it 'can format time to human readable string' do
origin = "Denver, CO"
destination = "Milwaukee, WI"

trip_data = {
:distance=>1047.017,
:realTime=>54577,
:formattedTime=>"14:59:49",
:routeError=>{:errorCode=> -400, :message=>""}
}

trip = Trip.new(trip_data, origin, destination)

expect(trip.format_time(trip_data)).to eq("14 hours, 59 minutes")
end
end

0 comments on commit e753f07

Please sign in to comment.