module Presenter module Dec class XmlSummary def initialize(view_model) @view_model = view_model end def to_xml dec_data = { report_type: @view_model.report_type, address_id: if @view_model.address_id&.include?("LPRN-") "" else @view_model.address_id end, output_engine: @view_model.output_engine, assessment_start_date: @view_model.or_assessment_start_date, assessment_end_date: @view_model.or_assessment_end_date, benchmarks: @view_model.benchmarks, energy_consumption: @view_model.or_energy_consumption, annual_energy_summary: @view_model.annual_energy_summary, dec_status: @view_model.respond_to?(:dec_status) ? @view_model.dec_status : nil, current_assessment_date: @view_model.current_assessment_date, energy_efficiency_rating: @view_model.energy_efficiency_rating, current_electricity_co2: @view_model.current_electricity_co2, current_heating_co2: @view_model.current_heating_co2, current_renewables_co2: @view_model.current_renewables_co2, main_heating_fuel: @view_model.main_heating_fuel, } xml = ERB.new(get_template).result_with_hash dec_data if !@view_model.respond_to?(:dec_status) || @view_model.dec_status.nil? doc = Nokogiri.XML(xml) doc.at("DEC-Status").remove xml = doc.to_xml end xml end private def get_template <<~ERB <%= report_type %> <%= address_id %> <%= output_engine %> <%= assessment_start_date %> <%= assessment_end_date %> <% benchmarks.each do |benchmark| %> <%= benchmark[:name] %> <%= benchmark[:id] %> <%= benchmark[:tufa] %> <% end %> <% energy_consumption.each do |consumption| %> <<%= consumption[:name] %>> <%= consumption[:consumption] %> <%= consumption[:start_date] %> <%= consumption[:end_date] %> <%= consumption[:estimate] %> ><% end %> <%= annual_energy_summary[:electrical] %> <%= annual_energy_summary[:fuel_thermal] %> <%= annual_energy_summary[:renewables_fuel_thermal] %> <%= annual_energy_summary[:renewables_electrical] %> <%= annual_energy_summary[:typical_thermal_use] %> <%= annual_energy_summary[:typical_electrical_use] %> <%= dec_status %> <%= current_assessment_date %> <%= energy_efficiency_rating %> <%= current_electricity_co2 %> <%= current_heating_co2 %> <%= current_renewables_co2 %> <%= main_heating_fuel %> ERB end end end end