Skip to content

Commit

Permalink
Use 'Forecast' entities in forecasts view
Browse files Browse the repository at this point in the history
  • Loading branch information
edavey committed Oct 3, 2024
1 parent 4d661ec commit 7ead96f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
7 changes: 2 additions & 5 deletions app/controllers/forecasts_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/cerc_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 16 additions & 16 deletions app/views/forecasts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,48 @@
<button type="submit" data-prevent-double-click="true">View forecast</button>
</form>
<%= 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;
Expand Down
17 changes: 15 additions & 2 deletions spec/controllers/forecasts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7ead96f

Please sign in to comment.