Created
May 23, 2009 21:49
-
-
Save cmoore/116787 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Network.SunlightLabs.Legislators where | |
import Network.SunlightLabs.Utils | |
import Network.SunlightLabs.JSON | |
import Control.Monad | |
import Data.Maybe | |
import Network.HTTP | |
import qualified Data.Map as M | |
request :: String -> String -> String | |
request k m = "http://services.sunlightlabs.com/api/" ++ m ++ ".json?apikey=" ++ k | |
getRawLegislatorsList :: String -> IO Value | |
getRawLegislatorsList k = liftM ( fromJust . parse ) $ fetch $ request k "legislators.getList" | |
legislatorsGetList :: String -> IO Value | |
legislatorsGetList k = liftM ( getE "legislators" . getE "response" ) $ getRawLegislatorsList k | |
legislatorsSearch :: String -> String -> IO Value | |
legislatorsSearch k x = liftM ( getE "results" . getE "response" . fromJust . parse ) $ fetch $ ( request k "legislators.search" ) ++ "&name=" ++ x | |
legislatorsAllForZip :: String -> String -> IO Value | |
legislatorsAllForZip k z = liftM ( fromJust . parse ) $ fetch $ ( request k "legislators.allForZip.json" ) ++ "&zip=" ++ z | |
legislatorsGet :: String -> String -> String -> IO Value | |
legislatorsGet k p v = liftM ( fromJust . parse ) $ fetch $ ( request k "legislators.get.json" ) ++ "&" ++ urlEncode ( p ++ "=" ++ v ) | |
{- | |
Disect the layers of the response onion. | |
-} | |
-- | getE - Get an element out of the value | |
getE :: String -> Value -> Value | |
getE elem val = | |
case val of | |
Object y -> | |
case ( M.lookup elem ) y of | |
Just w -> | |
w | |
_ -> Null | |
_ -> Null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment