rdioh 0.1.0 → 0.1.1
raw patch · 2 files changed
+48/−2 lines, 2 files
Files
- rdioh.cabal +2/−2
- src/RdioResult.hs +46/−0
rdioh.cabal view
@@ -1,5 +1,5 @@ name: rdioh-version: 0.1.0+version: 0.1.1 category: Network, API, Web license: MIT license-file: LICENSE@@ -20,7 +20,7 @@ , json , hoauth , urlencoded- exposed-modules: Rdioh+ exposed-modules: Rdioh, RdioResult hs-source-dirs: src ghc-options: -W -Wall
+ src/RdioResult.hs view
@@ -0,0 +1,46 @@+module RdioResult where+import qualified Text.JSON as J+import qualified Data.List as L++-- functions to convert a JSValue to a RdioResult:++-- this one converts all JSONObjects to nice tuples that can be used to make an RDioDict+conv x = RdioDict $ map (\(str, jsval) -> (str, clean jsval) ) $ J.fromJSObject x++-- helper methods to make all values into RdioResult values+-- so they can be stuffed into an RDioDict+clean (J.JSObject x) = conv x+clean (J.JSString x) = RdioString $ J.fromJSString x+clean (J.JSBool x) = RdioBool x+clean (J.JSRational _ x) = RdioRational x+clean (J.JSArray xs) = RdioArray $ map clean xs+clean J.JSNull = RdioNull++data RdioResult = RdioDict {rdioDictValue :: [(String, RdioResult)]} | RdioArray {rdioArrayValue :: [RdioResult]} | RdioString {rdioStringValue :: String} | RdioRational {rdioRationalValue :: Rational} | RdioBool {rdioBoolValue :: Bool} | RdioNull++dropLast x l = take (L.genericLength l - x) l++instance Show RdioResult where+ show (RdioDict xs) = "{" ++ (dropLast 2 (foldl (\acc (str, obj) -> acc ++ "\"" ++ str ++ "\"" ++ " : " ++ (show obj) ++ ", ") "" xs)) ++ "}"+ show (RdioArray xs) = "[" ++ (dropLast 2 (foldl (\acc x -> acc ++ (show x) ++ ", ") "" xs)) ++ "]"+ show (RdioString x) = "\"" ++ x ++ "\""+ show (RdioBool x) = show x+ show (RdioRational x) = show x+ show RdioNull = ""+++instance J.JSON RdioResult where+ showJSON (RdioDict xs) = J.showJSONs xs+ showJSON (RdioString x) = J.showJSON x+ showJSON (RdioRational x) = J.showJSON (show x)+ showJSON (RdioBool x) = J.showJSON (show x)+ showJSON (RdioArray xs) = J.showJSONs xs+ showJSON RdioNull = J.showJSON ""+ readJSON (J.JSObject obj) = J.Ok $ conv obj++-- helper methods for accessing parts of our RdioResult object+(RdioDict xs) ! str = snd . head $ filter (\(key, obj) -> key == str) xs+(RdioArray xs) .! num = xs !! num++keys (RdioDict xs) = map fst xs+values (RdioDict xs) = map snd xs