Last active
August 29, 2015 14:15
-
-
Save dvdsgl/a7b9054781c9c3cca49b 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 Trello | |
( BoardId() | |
, Credentials() | |
, Trello() | |
, runTrello | |
, Board() | |
, getBoard | |
) where | |
import Data.Maybe | |
import Data.Either | |
import Data.Function | |
import Data.Foreign | |
import Data.Foreign.Class | |
import Control.Monad.Eff | |
import Control.Monad.Lift | |
import Control.Monad.Eff.Exception | |
import Control.Monad.Reader | |
import Control.Monad.Reader.Class | |
import Control.Monad.Reader.Trans | |
import Node.Thunk | |
import Util | |
type BoardId = String | |
type Credentials = { key :: String, token :: String } | |
type Trello = ReaderT Client Thunk | |
-- TODO make Trello newtype so we don't have to be so general | |
instance liftReader :: Lift m (ReaderT r m) where | |
lift = liftReaderT | |
instance liftEitherThunk :: Lift (Either Error) Thunk where | |
lift = liftEither | |
data Board = Board | |
{ id :: String | |
, name :: String | |
} | |
instance showBoard :: Show Board where | |
show (Board b) = | |
let prop name val = " " ++ name ++ ": " ++ show val ++ ",\n" in | |
"Board {\n" | |
++ prop "id" b.id | |
++ prop "name" b.name | |
++ "}" | |
instance boardIsForeign :: IsForeign Board where | |
read object = do | |
id <- readProp "id" object | |
name <- readProp "name" object | |
return $ Board | |
{ id: id | |
, name: name | |
} | |
foreign import data Client :: * | |
foreign import trello | |
"var trello = require('node-trello');" | |
:: forall eff. Eff eff Unit | |
foreign import client | |
""" | |
function client(credentials) { | |
return new trello(credentials.key, credentials.token); | |
} | |
""" :: Credentials -> Client | |
foreign import getImpl | |
""" | |
function getImpl(client, path, done) { | |
client.get(path, done); | |
} | |
""" :: ThunkFn2 Client String Foreign | |
get :: forall a. (IsForeign a) => String -> Trello a | |
get path = do | |
client <- ask | |
object <- lift $ runThunkFn2 getImpl client path | |
readM object | |
-- Read foreign values in the Trello context, failing if the read fails | |
readM :: forall a. (IsForeign a) => Foreign -> Trello a | |
readM object = lift $ liftEither do | |
readWith (error <<< show) object | |
getBoard :: BoardId -> Trello Board | |
getBoard id' = get $ "/1/board/" ++ id' | |
runTrelloT :: forall a. Trello a -> Credentials -> Thunk a | |
runTrelloT trello credentials = runReaderT trello $ client credentials | |
runTrello :: forall a eff1 eff2. Trello a -> Credentials -> (Either Error a -> Eff eff2 Unit) -> Eff eff1 Unit | |
runTrello trello credentials = runThunk $ runTrelloT trello credentials |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment