Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions lib/fit4ruby/Converters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,24 @@ module Converters
'kg' => { 'lbs' => 2.20462262 },
'C' => { 'F' => 9.0 / 5.0 }
}.freeze

OFFSETS = {
'C' => { 'F' => 32 }
}.freeze

def conversion_factor(from_unit, to_unit)
return 1.0 if from_unit == to_unit

unless FACTORS.include?(from_unit)
Log.fatal 'No conversion factors defined for unit ' \
"'#{from_unit}' to '#{to_unit}'"
end

factor = FACTORS[from_unit][to_unit]
if factor.nil?
Log.fatal "No conversion factor from '#{from_unit}' to '#{to_unit}' " \
'defined.'
end

factor
FACTORS.fetch(from_unit).fetch(to_unit)
rescue KeyError
Log.fatal 'No conversion factor defined ' \
"from unit '#{from_unit}' to unit '#{to_unit}'"
end

def conversion_offset(from_unit, to_unit)
return 0.0 if from_unit == to_unit || !OFFSETS.include?(from_unit) ||
!OFFSETS[from_unit].include?(to_unit)
return 0.0 if from_unit == to_unit

OFFSETS[from_unit][to_unit]
OFFSETS.fetch(from_unit, {}).fetch(to_unit, 0.0)
end

def speedToPace(speed, distance = 1000.0)
Expand Down
74 changes: 74 additions & 0 deletions spec/Converters_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = Converters_spec.rb -- Fit4Ruby - FIT file processing library for Ruby
#
# Copyright (c) 2014, 2015 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'fit4ruby/Converters'

describe Fit4Ruby::Converters do

include described_class

describe '#conversion_factor' do

it 'should return one for matching "from" and "to" units' do
expect(conversion_factor('C', 'C')).to be(1.0)
end

it 'should raise an error for unsupported "from" unit' do
expect {
conversion_factor('F', 'C')
}.to raise_error(Fit4Ruby::Error)
end

it 'should raise an error for unsupported "to" unit' do
expect {
conversion_factor('C', 'K')
}.to raise_error(Fit4Ruby::Error)
end

it 'should return correct Celsius-to-Fahrenheit factor' do
expect(conversion_factor('C', 'F')).to eq(9.0 / 5.0)
end

end

describe '#conversion_offset' do

it 'should return zero for missing "from" unit' do
expect(conversion_offset(nil, nil)).to be_zero
end

it 'should return zero for missing "to" unit' do
expect(conversion_offset('C', nil)).to be_zero
end

it 'should return zero for unmapped "to" unit' do
expect(conversion_offset('C', 'K')).to be_zero
end

it 'should return zero for unmapped "from" unit' do
expect(conversion_offset('F', 'C')).to be_zero
end

it 'should return zero for matching "from"/"to" units' do
expect(conversion_offset('C', 'C')).to be_zero
end

it 'should return correct Celsius-to-Fahrenheit offset' do
expect(conversion_offset('C', 'F')).to eq(32)
end

end

end