Compare commits
2 Commits
c388f4c7a5
...
16981d9d3f
Author | SHA1 | Date | |
---|---|---|---|
16981d9d3f | |||
b96ff0bfb1 |
@ -64,3 +64,18 @@ weatherDecode jsonBody =
|
|||||||
case decode jsonBody :: Maybe WeatherResponse of
|
case decode jsonBody :: Maybe WeatherResponse of
|
||||||
Just decoded -> return decoded
|
Just decoded -> return decoded
|
||||||
Nothing -> error "Invalid JSON"
|
Nothing -> error "Invalid JSON"
|
||||||
|
|
||||||
|
getTemperature :: WeatherResponse -> Float
|
||||||
|
getTemperature (WeatherResponse _ Nothing Nothing) = error "No data provided!"
|
||||||
|
getTemperature (WeatherResponse _ (Just curr) Nothing) = getTemperatureFromCurrent curr
|
||||||
|
getTemperature (WeatherResponse _ Nothing (Just fore)) = getTemperatureFromForecastDay $ getForecastDayFromForecast fore
|
||||||
|
getTemperature (WeatherResponse _ (Just _) (Just fore)) = getTemperatureFromForecastDay $ getForecastDayFromForecast fore
|
||||||
|
|
||||||
|
getForecastDayFromForecast :: Forecast -> ForecastDay
|
||||||
|
getForecastDayFromForecast (Forecast forecasts) = head forecasts
|
||||||
|
|
||||||
|
getTemperatureFromCurrent :: Current -> Float
|
||||||
|
getTemperatureFromCurrent (Current temp) = temp
|
||||||
|
|
||||||
|
getTemperatureFromForecastDay :: ForecastDay -> Float
|
||||||
|
getTemperatureFromForecastDay (ForecastDay (Day temp)) = temp
|
10
app/Main.hs
10
app/Main.hs
@ -7,7 +7,6 @@ import Data.Time as T
|
|||||||
import Plot
|
import Plot
|
||||||
import qualified DataTypes as DT
|
import qualified DataTypes as DT
|
||||||
|
|
||||||
|
|
||||||
data WhichDay = Yesterday | Today | Tomorrow
|
data WhichDay = Yesterday | Today | Tomorrow
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
@ -34,25 +33,22 @@ main = do
|
|||||||
let yesterdayDate = formatDate $ addDays (-1) currentDate
|
let yesterdayDate = formatDate $ addDays (-1) currentDate
|
||||||
let yesterdayRequest = apiRequestBuilder apiKey Yesterday city yesterdayDate
|
let yesterdayRequest = apiRequestBuilder apiKey Yesterday city yesterdayDate
|
||||||
yesterdayResponse <- httpLBS yesterdayRequest
|
yesterdayResponse <- httpLBS yesterdayRequest
|
||||||
print $ getResponseStatusCode yesterdayResponse
|
|
||||||
yesterdayDecoded <- DT.weatherDecode $ getResponseBody yesterdayResponse
|
yesterdayDecoded <- DT.weatherDecode $ getResponseBody yesterdayResponse
|
||||||
print yesterdayDecoded
|
print ("Yesterday's temperature: ", DT.getTemperature yesterdayDecoded)
|
||||||
|
|
||||||
--request for today's weather
|
--request for today's weather
|
||||||
let todayDate = formatDate currentDate
|
let todayDate = formatDate currentDate
|
||||||
let todayRequest = apiRequestBuilder apiKey Today city todayDate
|
let todayRequest = apiRequestBuilder apiKey Today city todayDate
|
||||||
todayResponse <- httpLBS todayRequest
|
todayResponse <- httpLBS todayRequest
|
||||||
print $ getResponseStatusCode todayResponse
|
|
||||||
todayDecoded <- DT.weatherDecode $ getResponseBody todayResponse
|
todayDecoded <- DT.weatherDecode $ getResponseBody todayResponse
|
||||||
print todayDecoded
|
print ("Today's temperature: ", DT.getTemperature todayDecoded)
|
||||||
|
|
||||||
--request for tomorrow's weather
|
--request for tomorrow's weather
|
||||||
let tomorrowDate = formatDate $ addDays 1 currentDate
|
let tomorrowDate = formatDate $ addDays 1 currentDate
|
||||||
let tomorrowRequest = apiRequestBuilder apiKey Tomorrow city tomorrowDate
|
let tomorrowRequest = apiRequestBuilder apiKey Tomorrow city tomorrowDate
|
||||||
tomorrowResponse <- httpLBS tomorrowRequest
|
tomorrowResponse <- httpLBS tomorrowRequest
|
||||||
print $ getResponseStatusCode tomorrowResponse
|
|
||||||
tomorrowDecoded <- DT.weatherDecode $ getResponseBody tomorrowResponse
|
tomorrowDecoded <- DT.weatherDecode $ getResponseBody tomorrowResponse
|
||||||
print tomorrowDecoded
|
print ("Tomorrow's temperature: ", DT.getTemperature tomorrowDecoded)
|
||||||
|
|
||||||
|
|
||||||
--apiKey is stored in an env variable, not something that should be pushed onto git
|
--apiKey is stored in an env variable, not something that should be pushed onto git
|
||||||
|
Loading…
Reference in New Issue
Block a user