diff options
| -rw-r--r-- | app/models/chouette/route.rb | 4 | ||||
| -rw-r--r-- | app/services/route_way_cost_calculator.rb | 12 | ||||
| -rw-r--r-- | app/workers/route_way_cost_worker.rb | 8 | ||||
| -rw-r--r-- | spec/services/route_way_cost_calculator_spec.rb | 31 |
4 files changed, 55 insertions, 0 deletions
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb index d0d776bc6..a1d44f53f 100644 --- a/app/models/chouette/route.rb +++ b/app/models/chouette/route.rb @@ -188,6 +188,10 @@ module Chouette journey_pattern end + def calculate_costs! + RouteWayCostWorker.perform_async(id) + end + protected def self.vehicle_journeys_timeless(stop_point_id) diff --git a/app/services/route_way_cost_calculator.rb b/app/services/route_way_cost_calculator.rb new file mode 100644 index 000000000..2e30c94fc --- /dev/null +++ b/app/services/route_way_cost_calculator.rb @@ -0,0 +1,12 @@ +class RouteWayCostCalculator + def initialize(route) + @route = route + end + + def calculate! + way_costs = StopAreasToWayCostsConverter.new(@route.stop_areas).convert + way_costs = TomTom.batch(way_costs) + way_costs = WayCostCollectionJSONSerializer.dump(way_costs) + @route.update(costs: way_costs) + end +end diff --git a/app/workers/route_way_cost_worker.rb b/app/workers/route_way_cost_worker.rb new file mode 100644 index 000000000..adb10957b --- /dev/null +++ b/app/workers/route_way_cost_worker.rb @@ -0,0 +1,8 @@ +class RouteWayCostWorker + include Sidekiq::Worker + + def perform(route_id) + route = Chouette::Route.find(route_id) + RouteWayCostCalculator.new(route).calculate! + end +end diff --git a/spec/services/route_way_cost_calculator_spec.rb b/spec/services/route_way_cost_calculator_spec.rb new file mode 100644 index 000000000..d5358fcf6 --- /dev/null +++ b/spec/services/route_way_cost_calculator_spec.rb @@ -0,0 +1,31 @@ +RSpec.describe RouteWayCostCalculator do + describe "#calculate!" do + it "calculates and stores WayCosts in the given route's #cost field" do + route = create(:route) + + # Fake the request to the TomTom API, but don't actually send the right + # things in the request or response. This is just to fake the request so + # we don't actually call their API in tests. The test doesn't test + # anything given in the response. + stub_request(:post, "https://api.tomtom.com/routing/1/batch/json?key") + .with( + headers: { + 'Accept'=>'*/*', + 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Content-Type'=>'application/json', + 'User-Agent'=>'Faraday v0.9.2' + } + ) + .to_return( + status: 200, + body: "{\"formatVersion\":\"0.0.1\",\"batchItems\":[{\"statusCode\":200,\"response\":{\"routes\":[{\"summary\":{\"lengthInMeters\":117947,\"travelTimeInSeconds\":7969,\"trafficDelayInSeconds\":0,\"departureTime\":\"2018-03-12T12:32:26+01:00\",\"arrivalTime\":\"2018-03-12T14:45:14+01:00\"}}]}}]}", + headers: {} + ) + + RouteWayCostCalculator.new(route).calculate! + + expect(route.costs).not_to be_nil + expect { JSON.parse(JSON.dump(route.costs)) }.not_to raise_error + end + end +end |
