From b1fb2030fe15bf4d778c7c19f941c8209e40281e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 12 Mar 2018 15:25:50 +0100 Subject: TomTom::Batch: Extract code to `#extract_costs_to_way_costs!` Extract some code from `#batch` to allow us to test the part that takes distance and time values from the response JSON and put fill them into `WayCost`s. In order for the test to work, had to add an `#==` method to `WayCost` and make `distance` and `time` accessors. The JSON file fixture is a copy of a response from the TomTom `/batch` API. The file is pretty big. I'm not sure if maybe I should condense it for the sake of test performance. Refs #6095 --- lib/tom_tom/batch.rb | 9 +++++++-- lib/way_cost.rb | 10 +++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/tom_tom/batch.rb b/lib/tom_tom/batch.rb index 913f018b5..6ceb9c226 100644 --- a/lib/tom_tom/batch.rb +++ b/lib/tom_tom/batch.rb @@ -23,9 +23,14 @@ module TomTom }.to_json end - response = JSON.parse(response.body) + extract_costs_to_way_costs!( + way_costs, + JSON.parse(response.body) + ) + end - calculated_routes = response['batchItems'] + def extract_costs_to_way_costs!(way_costs, batch_json) + calculated_routes = batch_json['batchItems'] calculated_routes.each_with_index do |route, i| next if route['statusCode'] != 200 diff --git a/lib/way_cost.rb b/lib/way_cost.rb index 027d8f593..5de64177f 100644 --- a/lib/way_cost.rb +++ b/lib/way_cost.rb @@ -1,6 +1,6 @@ class WayCost attr_reader :departure, :arrival - attr_writer :distance, :time + attr_accessor :distance, :time def initialize( departure:, @@ -15,4 +15,12 @@ class WayCost @time = time @id = id end + + def ==(other) + other.is_a?(self.class) && + @departure == other.departure && + @arrival == other.arrival && + @distance == other.distance && + @time == other.time + end end -- cgit v1.2.3