class JSONThermostat
@author Jop Bouckaert Handles JSON input
Attributes
calculator[RW]
parsed[RW]
parsed_range[RW]
parsed_range_celsius[RW]
parsed_wanted_temperature[RW]
parsed_wanted_temperature_celsius[RW]
thermostat[RW]
Public Class Methods
new(input_json)
click to toggle source
# File lib/json_thermostat.rb, line 16 def initialize(input_json) @calculator = UnitCalculator.new parse_json(input_json) set_setting end
Public Instance Methods
check_celsius()
click to toggle source
Checks if unit is celsius
# File lib/json_thermostat.rb, line 80 def check_celsius unit == 'celsius' end
check_fahrenheit()
click to toggle source
Checks if unit is fahrenheit
# File lib/json_thermostat.rb, line 70 def check_fahrenheit unit == 'fahrenheit' end
check_kelvin()
click to toggle source
Checks if unit is kelvin
# File lib/json_thermostat.rb, line 75 def check_kelvin unit == 'kelvin' end
parse_json(input_json)
click to toggle source
Parses the input JSON
# File lib/json_thermostat.rb, line 34 def parse_json(input_json) @parsed = JSON.parse(input_json) @parsed_wanted_temperature = @parsed['temperature'] @parsed_range = @parsed['range'] end
range()
click to toggle source
Returns range in celsius @return [Numeric] Number with the calculated range in celsius
# File lib/json_thermostat.rb, line 109 def range if check_fahrenheit @calculator.calculate_fahrenheit_range(@parsed_range) elsif check_kelvin || check_celsius @parsed_range end end
set_setting()
click to toggle source
Sets wanted values
# File lib/json_thermostat.rb, line 41 def set_setting if unit @parsed_wanted_temperature_celsius = temperature(@parsed_wanted_temperature) @parsed_range_celsius = range else @parsed_wanted_temperature_celsius = @parsed_wanted_temperature @parsed_range_celsius = @parsed_range end end
set_thermostat()
click to toggle source
Call the thermostat class with updated and wanted values
# File lib/json_thermostat.rb, line 85 def set_thermostat @thermostat = Thermostat.new( current_value: @parsed_temperature_update_celsius, wanted_value: @parsed_wanted_temperature_celsius, range: @parsed_range_celsius ) @thermostat.state end
set_update()
click to toggle source
Sets updated values
# File lib/json_thermostat.rb, line 53 def set_update if unit @parsed_temperature_update_celsius = temperature(@parsed_temperature_update) @parsed_range_celsius = range else @parsed_temperature_update_celsius = @parsed_temperature_update @parsed_range_celsius = @parsed_range end end
temperature(temperature)
click to toggle source
Returns temperature in celsius @param temperature [Numeric] Number with given temperature @return [Numeric] Number with the calculated value in celsius
# File lib/json_thermostat.rb, line 97 def temperature(temperature) if check_fahrenheit @calculator.calculate_fahrenheit(temperature) elsif check_kelvin @calculator.calculate_kelvin(temperature) elsif check_celsius temperature end end
unit()
click to toggle source
Returns the unit
# File lib/json_thermostat.rb, line 65 def unit @parsed['unit'] end
update(update_json)
click to toggle source
Update values @param update_json [String] JSON string with update value @return [String] JSON string with states of cooling and heating
# File lib/json_thermostat.rb, line 25 def update(update_json) @parsed = JSON.parse(update_json) @parsed_temperature_update = @parsed['temperature'] set_update set_thermostat JSON.generate(cooling: @thermostat.cooling, heating: @thermostat.heating) end