aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTeddy Wing2018-03-23 17:01:16 +0100
committerTeddy Wing2018-03-27 16:46:18 +0200
commite2cbcdb4dc32db05e2c6d7a7bb57952e1da6dab3 (patch)
tree8af3dcb7c9fdf765f60292f235cad63ab632336d /lib
parent4bc33f14a148feb4056db1a2839303b07c67a6b3 (diff)
downloadchouette-core-e2cbcdb4dc32db05e2c6d7a7bb57952e1da6dab3.tar.bz2
TomTom::Matrix#points_from_way_costs: Include stop IDs with points
We need to persist stop IDs in order to properly construct `WayCost` objects from the costs returned from the TomTom matrix API. In order to persist stop IDs, my idea here is to group together a point and its corresponding ID into a bucket. When we later `#extract_costs_to_way_costs!`, we'll be able to grab the correct ID for a given coordinate to create a `WayCost` from it. Here, we create a new `TomTom::Matrix::Point` class that encapsulates a coordinate and an ID, and build a `Set` of those. I needed an `#eql?` and `#hash` method on `Point` as described in the `Set` documentation (https://ruby-doc.org/stdlib-1.9.3/libdoc/set/rdoc/Set.html) in order to properly maintain a unique set. Refs #6222
Diffstat (limited to 'lib')
-rw-r--r--lib/tom_tom/matrix.rb16
-rw-r--r--lib/tom_tom/matrix/point.rb24
2 files changed, 38 insertions, 2 deletions
diff --git a/lib/tom_tom/matrix.rb b/lib/tom_tom/matrix.rb
index 985055dae..4ef620f99 100644
--- a/lib/tom_tom/matrix.rb
+++ b/lib/tom_tom/matrix.rb
@@ -32,8 +32,20 @@ module TomTom
points = Set.new
way_costs.each do |way_cost|
- points.add(way_cost.departure)
- points.add(way_cost.arrival)
+ ids = way_cost.id.split('-')
+
+ points.add(
+ TomTom::Matrix::Point.new(
+ way_cost.departure,
+ ids[0]
+ )
+ )
+ points.add(
+ TomTom::Matrix::Point.new(
+ way_cost.arrival,
+ ids[1]
+ )
+ )
end
points
diff --git a/lib/tom_tom/matrix/point.rb b/lib/tom_tom/matrix/point.rb
new file mode 100644
index 000000000..4e9d11e68
--- /dev/null
+++ b/lib/tom_tom/matrix/point.rb
@@ -0,0 +1,24 @@
+module TomTom
+ class Matrix
+ class Point
+ attr_reader :coordinates, :id
+
+ def initialize(coordinates, id)
+ @coordinates = coordinates
+ @id = id
+ end
+
+ def ==(other)
+ other.is_a?(self.class) &&
+ @coordinates == other.coordinates &&
+ @id == other.id
+ end
+
+ alias :eql? :==
+
+ def hash
+ @coordinates.hash + @id.hash
+ end
+ end
+ end
+end