Skip to content

Instantly share code, notes, and snippets.

@cmoore
Created May 23, 2009 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmoore/116787 to your computer and use it in GitHub Desktop.
Save cmoore/116787 to your computer and use it in GitHub Desktop.
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