Weather 0.1.0.0 → 0.1.0.1
raw patch · 5 files changed
+142/−75 lines, 5 files
Files
- Weather.cabal +4/−4
- Weather.cabal~ +2/−3
- src/Net/Weather.hs +68/−0
- src/Net/Weather.hs~ +68/−0
- src/Web/Weather.hs +0/−68
Weather.cabal view
@@ -2,22 +2,22 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: Weather-version: 0.1.0.0+version: 0.1.0.1 synopsis: Library for interacting with the Weather Underground JSON API. description: Weather is a simple library for interacting with the Weather Underground JSON API. It is not complete, but it may be useful still.-homepage: http://bryanstamour.com+homepage: https://github.com/bstamour/weather license: BSD3 license-file: LICENSE author: Bryan St. Amour maintainer: bryan@bryanstamour.com -- copyright:-category: Web+category: Net build-type: Simple -- extra-source-files: cabal-version: >=1.10 library- exposed-modules: Web.Weather+ exposed-modules: Net.Weather -- other-modules: -- other-extensions: build-depends: base >= 4.7.0.2 && < 5
Weather.cabal~ view
@@ -4,9 +4,8 @@ name: Weather version: 0.1.0.0 synopsis: Library for interacting with the Weather Underground JSON API.-description: Weather is a simple library for interacting with the Weather Underground \-JSON API. It is not complete, but it may be useful still.-homepage: http://bryanstamour.com+description: Weather is a simple library for interacting with the Weather Underground JSON API. It is not complete, but it may be useful still.+homepage: https://github.com/bstamour/weather license: BSD3 license-file: LICENSE author: Bryan St. Amour
+ src/Net/Weather.hs view
@@ -0,0 +1,68 @@+{-# LANGUAGE OverloadedStrings #-}++module Net.Weather+ ( Observation(..)+ , APIKey+ , getConditions+ ) where++import Control.Monad+import Control.Applicative++import Data.Text (Text)+import Data.Aeson+import Data.ByteString.Lazy.Char8 (pack)+import Network.HTTP (getResponseBody, simpleHTTP, getRequest)++import qualified Data.HashMap.Strict as H++-- | Observation data.+data Observation =+ Observation { obsTime :: String -- ^ The time the observation was taken.+ , obsWeather :: String -- ^ Description of the weather.+ , obsTemp :: Float -- ^ Temperature (F).+ , obsRelHumidity :: String -- ^ Relative humidity (%).+ , obsWind :: String -- ^ Wind condition.+ , obsFeelsLike :: String -- ^ What it feels like.+ } deriving (Show)++instance FromJSON Observation where+ parseJSON (Object v) = Observation+ <$> v .: "observation_time"+ <*> v .: "weather"+ <*> v .: "temp_f"+ <*> v .: "relative_humidity"+ <*> v .: "wind_string"+ <*> v .: "feelslike_string"+ parseJSON _ = mzero++-- | API key. Obtain yours at http://wunderground.com.+type APIKey = String++-- | Get the current weather conditions of a city.+getConditions :: APIKey -> String -> String -> IO (Maybe Observation)+getConditions key city state = do+ obj <- evalJSONRequest $ conditionsQuery key city state+ return $ obj >>= getProperty "current_observation"++-- Fetch a property from a JSON object.+getProperty :: FromJSON a => Text -> Value -> Maybe a+getProperty property (Object v) = do+ val <- H.lookup property v+ case fromJSON val of+ Error _ -> Nothing+ Success x -> Just x+getProperty _ _ = Nothing++-- Evaluate a JSON request and return the parsed object.+evalJSONRequest :: FromJSON a => String -> IO (Maybe a)+evalJSONRequest request = do+ body <- getResponseBody <=< simpleHTTP $ getRequest request+ return . decode $ pack body++-- The query used to get weather conditions.+conditionsQuery :: String -> String -> String -> String+conditionsQuery key city state =+ "http://api.wunderground.com/api/" ++ key+ ++ "/conditions/q/" ++ state+ ++ "/" ++ city ++ ".json"
+ src/Net/Weather.hs~ view
@@ -0,0 +1,68 @@+{-# LANGUAGE OverloadedStrings #-}++module Web.Weather+ ( Observation(..)+ , APIKey+ , getConditions+ ) where++import Control.Monad+import Control.Applicative++import Data.Text (Text)+import Data.Aeson+import Data.ByteString.Lazy.Char8 (pack)+import Network.HTTP (getResponseBody, simpleHTTP, getRequest)++import qualified Data.HashMap.Strict as H++-- | Observation data.+data Observation =+ Observation { obsTime :: String -- ^ The time the observation was taken.+ , obsWeather :: String -- ^ Description of the weather.+ , obsTemp :: Float -- ^ Temperature (F).+ , obsRelHumidity :: String -- ^ Relative humidity (%).+ , obsWind :: String -- ^ Wind condition.+ , obsFeelsLike :: String -- ^ What it feels like.+ } deriving (Show)++instance FromJSON Observation where+ parseJSON (Object v) = Observation+ <$> v .: "observation_time"+ <*> v .: "weather"+ <*> v .: "temp_f"+ <*> v .: "relative_humidity"+ <*> v .: "wind_string"+ <*> v .: "feelslike_string"+ parseJSON _ = mzero++-- | API key. Obtain yours at http://wunderground.com.+type APIKey = String++-- | Get the current weather conditions of a city.+getConditions :: APIKey -> String -> String -> IO (Maybe Observation)+getConditions key city state = do+ obj <- evalJSONRequest $ conditionsQuery key city state+ return $ obj >>= getProperty "current_observation"++-- Fetch a property from a JSON object.+getProperty :: FromJSON a => Text -> Value -> Maybe a+getProperty property (Object v) = do+ val <- H.lookup property v+ case fromJSON val of+ Error _ -> Nothing+ Success x -> Just x+getProperty _ _ = Nothing++-- Evaluate a JSON request and return the parsed object.+evalJSONRequest :: FromJSON a => String -> IO (Maybe a)+evalJSONRequest request = do+ body <- getResponseBody <=< simpleHTTP $ getRequest request+ return . decode $ pack body++-- The query used to get weather conditions.+conditionsQuery :: String -> String -> String -> String+conditionsQuery key city state =+ "http://api.wunderground.com/api/" ++ key+ ++ "/conditions/q/" ++ state+ ++ "/" ++ city ++ ".json"
− src/Web/Weather.hs
@@ -1,68 +0,0 @@-{-# LANGUAGE OverloadedStrings #-}--module Web.Weather- ( Observation(..)- , APIKey- , getConditions- ) where--import Control.Monad-import Control.Applicative--import Data.Text (Text)-import Data.Aeson-import Data.ByteString.Lazy.Char8 (pack)-import Network.HTTP (getResponseBody, simpleHTTP, getRequest)--import qualified Data.HashMap.Strict as H---- | Observation data.-data Observation =- Observation { obsTime :: String -- ^ The time the observation was taken.- , obsWeather :: String -- ^ Description of the weather.- , obsTemp :: Float -- ^ Temperature (F).- , obsRelHumidity :: String -- ^ Relative humidity (%).- , obsWind :: String -- ^ Wind condition.- , obsFeelsLike :: String -- ^ What it feels like.- } deriving (Show)--instance FromJSON Observation where- parseJSON (Object v) = Observation- <$> v .: "observation_time"- <*> v .: "weather"- <*> v .: "temp_f"- <*> v .: "relative_humidity"- <*> v .: "wind_string"- <*> v .: "feelslike_string"- parseJSON _ = mzero---- | API key. Obtain yours at http://wunderground.com.-type APIKey = String---- | Get the current weather conditions of a city.-getConditions :: APIKey -> String -> String -> IO (Maybe Observation)-getConditions key city state = do- obj <- evalJSONRequest $ conditionsQuery key city state- return $ obj >>= getProperty "current_observation"---- Fetch a property from a JSON object.-getProperty :: FromJSON a => Text -> Value -> Maybe a-getProperty property (Object v) = do- val <- H.lookup property v- case fromJSON val of- Error _ -> Nothing- Success x -> Just x-getProperty _ _ = Nothing---- Evaluate a JSON request and return the parsed object.-evalJSONRequest :: FromJSON a => String -> IO (Maybe a)-evalJSONRequest request = do- body <- getResponseBody <=< simpleHTTP $ getRequest request- return . decode $ pack body---- The query used to get weather conditions.-conditionsQuery :: String -> String -> String -> String-conditionsQuery key city state =- "http://api.wunderground.com/api/" ++ key- ++ "/conditions/q/" ++ state- ++ "/" ++ city ++ ".json"