class JSONThermostat

Class that uses JSON input to handle the thermostat @example

settings = JSON.generate({temperature: 20.0, range: 1.0})
measurement = JSON.generate({temperature: 20.0})
expected_result = JSON.generate({cooling: false, heating: false})
thermostat = JSONThermostat.new settings
result = thermostat.update measurement

Attributes

range[RW]
unit[RW]
wanted_value[RW]

Public Class Methods

new() click to toggle source

Takes in a JSON object and parses the different values. @param [JSON] input JSON file with unit, range and wanted value Uses [converter] method for converter instance.

# File lib/json_thermostat.rb, line 18
def initialize()
  @unit = "celsius"
  @range  = 1.0;
  @wanted_value = 21.0;

end

Public Instance Methods

converter() click to toggle source

Uses the Converter class to make a converter instance that can be used.

# File lib/json_thermostat.rb, line 35
def converter
  @converter ||= Converter.new
end
thermo() click to toggle source

Uses the Thermostat class to make a thermostat instance to process input.

# File lib/json_thermostat.rb, line 41
def thermo
  message = { wanted_value: wanted_value, range: range }
  @thermo ||= Thermostat.new(message)
end
update(testjson) click to toggle source

Takes in JSON object to update the current temperature. Uses thermo instance to update the thermostat. @param [JSON] input JSON file with current value Uses [thermo] method for thermo instance.

# File lib/json_thermostat.rb, line 51
def update(testjson)
  parsed = JSON.parse(testjson)
  args = { unit: parsed['unit'], value: parsed['temperature'] }
  thermo.update_current(converter.temp_to_celcius(args))
  thermo.update_thermo
end