aboutsummaryrefslogtreecommitdiffstats
path: root/lib/route_way_cost_unit_converter.rb
blob: 52515e52cbcd271249b2e3077d15806f43280da4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class RouteWayCostUnitConverter
  def self.convert(way_costs)
    return if way_costs.nil?

    way_costs.each do |_, costs|
      costs['distance'] = self.meters_to_kilometers(costs['distance'])
      costs['time'] = self.seconds_to_minutes(costs['time'])
    end
  end

  # Round to 2 decimal places to appease JavaScript validation
  def self.meters_to_kilometers(num)
    return 0 unless num

    snap_to_one(num / 1000.0).to_i
  end

  def self.seconds_to_minutes(num)
    return 0 unless num

    snap_to_one(num / 60.0).to_i
  end

  private

  def self.snap_to_one(decimal)
    return 1 if decimal > 0 && decimal <= 1

    decimal
  end
end