From 7ead96ff9b9ef830f0218ca51e8a3b9f9d25d168 Mon Sep 17 00:00:00 2001 From: Ed Davey Date: Thu, 3 Oct 2024 08:08:25 +0100 Subject: [PATCH] Use 'Forecast' entities in forecasts view --- app/controllers/forecasts_controller.rb | 7 ++-- app/models/cerc_api_client.rb | 4 +++ app/views/forecasts/show.html.erb | 32 +++++++++---------- spec/controllers/forecasts_controller_spec.rb | 17 ++++++++-- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/app/controllers/forecasts_controller.rb b/app/controllers/forecasts_controller.rb index 0cc8f00..c4ccc33 100644 --- a/app/controllers/forecasts_controller.rb +++ b/app/controllers/forecasts_controller.rb @@ -1,10 +1,7 @@ class ForecastsController < ApplicationController def show - forecast_response = CercApiClient.fetch_data(params.has_key?("zone") ? params["zone"] : "Southwark") - - @forecast = forecast_response["zones"].first - - @dates = @forecast["forecasts"].map { |day_forecast| day_forecast["forecast_date"] } + @forecasts = CercApiClient + .forecasts_for(params.has_key?("zone") ? params["zone"] : "Southwark") @zones = JSON.parse(File.read("#{Rails.root}/config/list-of-zones.json")) @alerts = [] end diff --git a/app/models/cerc_api_client.rb b/app/models/cerc_api_client.rb index 9f977c5..8337a23 100644 --- a/app/models/cerc_api_client.rb +++ b/app/models/cerc_api_client.rb @@ -15,4 +15,8 @@ def self.fetch_data(zone) HTTParty.get("#{BASE_URL}/getforecast/all", query: query) end + + def self.forecasts_for(zone) + ForecastFactory.build(fetch_data(zone)) + end end diff --git a/app/views/forecasts/show.html.erb b/app/views/forecasts/show.html.erb index 9869b53..b58ae09 100644 --- a/app/views/forecasts/show.html.erb +++ b/app/views/forecasts/show.html.erb @@ -19,48 +19,48 @@ <%= govuk_table do |table| - table.with_caption(size: 'm', text: "Three day forecast for #{@forecast["zone_name"]}") + table.with_caption(size: 'm', text: "Three day forecast for #{@forecasts.first.zone.name}") table.with_head do |head| head.with_row do |row| row.with_cell(text: '') - row.with_cell(text: Date.parse(@dates[0]).to_formatted_s(:short)) - row.with_cell(text: Date.parse(@dates[1]).to_formatted_s(:short)) - row.with_cell(text: Date.parse(@dates[2]).to_formatted_s(:short)) + @forecasts.map { |forecast| + row.with_cell(text: forecast.forecast_for) + } end; end; table.with_body do |body| body.with_row(html_attributes: {class: "air-pollution"}) do |row| row.with_cell(text: 'Air pollution') - @forecast["forecasts"].map { | forecast| + @forecasts.map { | forecast| row.with_cell( - text: forecast["total_status"].capitalize, - html_attributes: { "data-date" => forecast['forecast_date']} + text: forecast.air_pollution.overall_label, + html_attributes: { "data-date" => forecast.forecast_for} ) } end; body.with_row(html_attributes: {class: "uv"}) do |row| row.with_cell(text: 'UV') - @forecast["forecasts"].map { | forecast| + @forecasts.map { | forecast| row.with_cell( - text: forecast["uv"], - html_attributes: { "data-date" => forecast['forecast_date']} + text: forecast.uv, + html_attributes: { "data-date" => forecast.forecast_for} ) } end; body.with_row(html_attributes: {class: "pollen"}) do |row| row.with_cell(text: 'Pollen') - @forecast["forecasts"].map { | forecast| - row.with_cell(text: forecast["pollen"], - html_attributes: { "data-date" => forecast['forecast_date']} + @forecasts.map { | forecast| + row.with_cell(text: forecast.pollen, + html_attributes: { "data-date" => forecast.forecast_for} ) } end; body.with_row(html_attributes: {class: "temperature"}) do |row| row.with_cell(text: 'Temperature') - @forecast["forecasts"].map { | forecast| + @forecasts.map { | forecast| row.with_cell( - text: "#{forecast["temp_min"].round}-#{forecast["temp_max"].round}°C", - html_attributes: { "data-date" => forecast['forecast_date']} + text: "#{forecast.temperature.min.round}-#{forecast.temperature.max.round}°C", + html_attributes: { "data-date" => forecast.forecast_for} ) } end; diff --git a/spec/controllers/forecasts_controller_spec.rb b/spec/controllers/forecasts_controller_spec.rb index 5e9c171..caa8a31 100644 --- a/spec/controllers/forecasts_controller_spec.rb +++ b/spec/controllers/forecasts_controller_spec.rb @@ -44,12 +44,25 @@ } JSON end + + let(:forecasts) do + ForecastFactory.build(JSON.parse(json)) + end + + it "obtains forecasts for the default zone (Southwark) from the CercApiClient" do + allow(CercApiClient).to receive(:forecasts_for).and_return(forecasts) + + get :show + + expect(CercApiClient).to have_received(:forecasts_for).with("Southwark") + expect(response).to render_template("show") + end + it "renders the _show_ template" do - allow(CercApiClient).to receive(:fetch_data).and_return(JSON.parse(json)) + allow(CercApiClient).to receive(:forecasts_for).and_return(forecasts) get :show - expect(CercApiClient).to have_received(:fetch_data).with("Southwark") expect(response).to render_template("show") end end