class UnitConverter

Converts temperatures expressed in different units to the unit that the application works with

Attributes

desired_unit[R]

Public Class Methods

new(args) click to toggle source

Initialises the UnitConverter class @param desired_unit [desired_unit] The desired unit of the application

# File lib/unitconverter.rb, line 8
def initialize(args)
  @desired_unit = get_unit(unit: args[:desired_unit] || nil)
end

Public Instance Methods

celsius_to_fahrenheit(args) click to toggle source

converts a temperature in celcius to fahrenheit @param value [value] the input temperature @return [Number] the converted temperature

# File lib/unitconverter.rb, line 56
def celsius_to_fahrenheit(args)
  (args[:value] * 1.8) + 32
end
celsius_to_kelvin(args) click to toggle source

converts a temperature in celcius to kelvin @param value [value] the input temperature @return [Number] the converted temperature

# File lib/unitconverter.rb, line 49
def celsius_to_kelvin(args)
  args[:value] + 273.15
end
conversion_method(args) click to toggle source

gets the name of the right conversion method @param input_unit [input_unit] the sensor unit @return [String] the name of the needed conversion method

# File lib/unitconverter.rb, line 42
def conversion_method(args)
  args[:input_unit] + '_to_' + desired_unit
end
convert(args) click to toggle source

Converts the input temperature to the desired unit @param value [value] the input temperature @param input_unit [input_unit] the unit of the sensor defaults to celsius @return [Number] the converted temperature

# File lib/unitconverter.rb, line 30
def convert(args)
  output = args[:value]
  input_unit = get_unit(unit: args[:input_unit] || nil)
  if needs_conversion(input_unit: input_unit)
    output = send conversion_method(input_unit: input_unit), value: output
  end
  output
end
fahrenheit_to_celsius(args) click to toggle source

converts a temperature in fahrenheit to celsius @param value [value] the input temperature @return [Number] the converted temperature

# File lib/unitconverter.rb, line 63
def fahrenheit_to_celsius(args)
  (args[:value] - 32) / 1.8
end
fahrenheit_to_kelvin(args) click to toggle source

converts a temperature in fahrenheit to kelvin @param value [value] the input temperature @return [Number] the converted temperature

# File lib/unitconverter.rb, line 70
def fahrenheit_to_kelvin(args)
  celsiusToKelvin(fahrenheitToCelsius(value: args[:value]))
end
get_unit(args) click to toggle source

returns the unit in wich the external sensor works @param unit [unit] the unit of the sensor, defaults to celsius @return [String] the unit in wich the sensor works

# File lib/unitconverter.rb, line 15
def get_unit(args)
  args[:unit] || 'celsius'
end
kelvin_to_celsius(args) click to toggle source

converts a temperature in kelvin to celsius @param value [value] the input temperature @return [Number] the converted temperature

# File lib/unitconverter.rb, line 77
def kelvin_to_celsius(args)
  args[:value] - 273.15
end
kelvin_to_fahrenheit(args) click to toggle source

converts a temperature in kelvin to fahrenheit @param value [value] the input temperature @return [Number] the converted temperature

# File lib/unitconverter.rb, line 84
def kelvin_to_fahrenheit(args)
  (kelvinToCelsius(value: args[:value]) * 1.8) + 32
end
needs_conversion(args) click to toggle source

Checks if the input temperature actually needs to be converted @param input_unit [input_unit] the unit of the sensor @return [Boolean] true if the input temperature needs to be converted

# File lib/unitconverter.rb, line 22
def needs_conversion(args)
  desired_unit != args[:input_unit]
end