Skip to content

Commit

Permalink
Wrap IO in Pgn2Graph in IO monad
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Hull committed May 22, 2017
1 parent e827b59 commit 491e445
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ resolvers ++= Seq(

libraryDependencies ++= Seq(
"org.anormcypher" %% "anormcypher" % "0.9.1",
"org.specs2" %% "specs2-core" % "3.8.9" % "test"
"org.specs2" %% "specs2-core" % "3.8.9" % "test",
"org.scalaz" %% "scalaz-core" % "7.2.12",
"org.scalaz" %% "scalaz-effect" % "7.2.12"
)

31 changes: 17 additions & 14 deletions src/main/scala/app/Pgn2Graph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,30 @@ import org.anormcypher._
import play.api.libs.ws._
import scala.concurrent.ExecutionContext
import com.typesafe.config.ConfigFactory
import scalaz._, Scalaz._, effect._, IO._

object Pgn2Graph {
implicit val ec = ExecutionContext.global
implicit val config = ConfigFactory.load()

def main(args: Array[String]): Unit = {
val pgnPath = args(0)
val pgn = fromFile(pgnPath).mkString
def main(args: Array[String]): Unit =
pureMain(args(0)).unsafePerformIO

val p = Pgn(pgn) match {
case Right(p1) => p1
case Left(parseError) =>
println("PARSE ERROR: " + parseError)
sys.exit(1)
}
println("Processing [" + pgnPath + "]")
createRoot()
insertPgnIntoGraph(p)
private def pureMain(filePath: String): IO[Unit] =
for {
_ <- putStrLn("Processing [" + filePath+ "]")
pgn <- getPgnContent(filePath)
_ <- Pgn(pgn) match {
case Right(p) => createRoot |+| insertPgnIntoGraph(p)
case Left(parseError) => putStrLn("PARSE ERROR: " + parseError)
}
} yield()

private def getPgnContent(filePath: String): IO[String] = IO {
fromFile(filePath).mkString
}

private def createRoot(): Unit = {
private def createRoot(): IO[Unit] = IO {
implicit val wsclient = ning.NingWSClient()
implicit val connection = getNeo4jConnection(wsclient)

Expand All @@ -55,7 +58,7 @@ object Pgn2Graph {
wsclient.close()
}

private def insertPgnIntoGraph(pgn: Pgn): Unit = {
private def insertPgnIntoGraph(pgn: Pgn): IO[Unit] = IO {
implicit val wsclient = ning.NingWSClient()
implicit val connection = getNeo4jConnection(wsclient)

Expand Down

0 comments on commit 491e445

Please sign in to comment.