Skip to content

Commit

Permalink
Merge pull request #17 from kbhoffmann/refactorweatherfacade
Browse files Browse the repository at this point in the history
Refactor weather facade
  • Loading branch information
kbhoffmann authored Mar 14, 2022
2 parents 0a99509 + bc67221 commit 6a0ebd5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
6 changes: 2 additions & 4 deletions app/controllers/api/v1/weather_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class Api::V1::WeatherController < ApplicationController
def show
coords = MapFacade.get_lat_long(params[:location])
current = WeatherFacade.current_weather(coords.latitude, coords.longitude)
five_day = WeatherFacade.five_day_weather(coords.latitude, coords.longitude)
eight_hour = WeatherFacade.next_eight_hours(coords.latitude, coords.longitude)
render json: ForecastSerializer.weather_json(current, five_day, eight_hour)
weather_data = WeatherFacade.get_weather(coords.latitude, coords.longitude)
render json: ForecastSerializer.weather_json(weather_data)
end
end
28 changes: 19 additions & 9 deletions app/facades/weather_facade.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
class WeatherFacade
def self.current_weather(latitude, longitude)
current_weather_info = WeatherService.get_weather_data(latitude, longitude)[:current]
CurrentWeather.new(current_weather_info)
def self.get_weather(latitude, longitude)
weather_data = WeatherService.get_weather_data(latitude, longitude)

weather_hash = Hash.new
weather_hash[:current_weather] = current_weather(weather_data)
weather_hash[:daily_weather] = daily_weather(weather_data)
weather_hash[:hourly_weather] = hourly_weather(weather_data)
weather_hash
end

def self.current_weather(weather_data)
current_weather = (weather_data)[:current]
CurrentWeather.new(current_weather)
end

def self.five_day_weather(latitude, longitude)
five_days_data = WeatherService.get_weather_data(latitude, longitude)[:daily].first(5)
five_days_data.map do |day_data|
def self.daily_weather(weather_data)
days_data = (weather_data)[:daily].first(5)
days_data.map do |day_data|
DailyWeather.new(day_data)
end
end

def self.next_eight_hours(latitude, longitude)
eight_hours_data = WeatherService.get_weather_data(latitude, longitude)[:hourly].first(8)
eight_hours_data.map do |hour_data|
def self.hourly_weather(weather_data)
hours_data = (weather_data)[:hourly].first(8)
hours_data.map do |hour_data|
HourlyWeather.new(hour_data)
end
end
Expand Down
7 changes: 4 additions & 3 deletions app/serializers/forecast_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ForecastSerializer

def self.weather_json(current, five_day, eight_hour)
def self.weather_json(weather_data)
current = weather_data[:current_weather]
{
"data": {
"id": nil,
Expand All @@ -20,7 +21,7 @@ def self.weather_json(current, five_day, eight_hour)
},

"daily_weather":
five_day.map do |day|
weather_data[:daily_weather].map do |day|
{
"date": day.date,
"sunrise": day.sunrise,
Expand All @@ -33,7 +34,7 @@ def self.weather_json(current, five_day, eight_hour)
end,

"hourly_weather":
eight_hour.map do |hour|
weather_data[:hourly_weather].map do |hour|
{
"time": hour.time,
"temperature": hour.temperature,
Expand Down
28 changes: 18 additions & 10 deletions spec/facades/weather_facade_spec.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
require 'rails_helper'

RSpec.describe WeatherFacade do
it 'sends information to the Current Weather poro' do
it 'gets data from the weather service' do
json_response = File.read('spec/fixtures/request_weather_data.json')
stub_request(:get, "https://api.openweathermap.org/data/2.5/onecall?appid=b223e219a2cff0890dbe4fae9e6d5836&exclude=minutely,alerts&lat=39.738453&lon=-104.984853&units=imperial").
to_return(status: 200, body: json_response, headers: {})

latitude = 39.738453
longitude = -104.984853

expect(WeatherFacade.current_weather(latitude, longitude)).to be_a(CurrentWeather)
expect(WeatherFacade.get_weather(latitude, longitude)).to be_a(Hash)
end

it 'sends information to the Current Weather poro' do
json_response = File.read('spec/fixtures/request_weather_data.json')
stub_request(:get, "https://api.openweathermap.org/data/2.5/onecall?appid=b223e219a2cff0890dbe4fae9e6d5836&exclude=minutely,alerts&lat=39.738453&lon=-104.984853&units=imperial").
to_return(status: 200, body: json_response, headers: {})

weather_data = JSON.parse(json_response, symbolize_names: true)

expect(WeatherFacade.current_weather(weather_data)).to be_a(CurrentWeather)
end

it 'sends information to the Daily Weather poro' do
json_response = File.read('spec/fixtures/request_weather_data.json')
stub_request(:get, "https://api.openweathermap.org/data/2.5/onecall?appid=b223e219a2cff0890dbe4fae9e6d5836&exclude=minutely,alerts&lat=39.742043&lon=-104.991531&units=imperial").
to_return(status: 200, body: json_response, headers: {})

latitude = 39.742043
longitude = -104.991531
weather_data = JSON.parse(json_response, symbolize_names: true)

expect(WeatherFacade.five_day_weather(latitude, longitude).first).to be_a(DailyWeather)
expect(WeatherFacade.five_day_weather(latitude, longitude).length).to eq(5)
expect(WeatherFacade.daily_weather(weather_data).first).to be_a(DailyWeather)
expect(WeatherFacade.daily_weather(weather_data).length).to eq(5)
end

it 'sends information to the Hourly Weather poro' do
json_response = File.read('spec/fixtures/request_weather_data.json')
stub_request(:get, "https://api.openweathermap.org/data/2.5/onecall?appid=b223e219a2cff0890dbe4fae9e6d5836&exclude=minutely,alerts&lat=39.742043&lon=-104.991531&units=imperial").
to_return(status: 200, body: json_response, headers: {})

latitude = 39.742043
longitude = -104.991531
weather_data = JSON.parse(json_response, symbolize_names: true)

expect(WeatherFacade.next_eight_hours(latitude, longitude).first).to be_an(HourlyWeather)
expect(WeatherFacade.next_eight_hours(latitude, longitude).length).to eq(8)
expect(WeatherFacade.hourly_weather(weather_data).first).to be_an(HourlyWeather)
expect(WeatherFacade.hourly_weather(weather_data).length).to eq(8)
end

it 'sends information to the Hourly Weather poro for trip eta' do
Expand Down

0 comments on commit 6a0ebd5

Please sign in to comment.