Skip to content

Instantly share code, notes, and snippets.

@andypetrella
Created February 20, 2012 22:47
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 andypetrella/1872053 to your computer and use it in GitHub Desktop.
Save andypetrella/1872053 to your computer and use it in GitHub Desktop.
Neo4J using Play and Dispatch
import dispatch._
import models.Model
import dispatch.HttpExecutor
import utils.dispatch.PlayJsonDispatchHttp._
trait Neo4JRestService {
val neoRest = :/("localhost", 7474)
val neoRestBase = neoRest / "db" / "data"
val neoRestNode = neoRestBase / "node"
val neoRestRel = neoRestBase / "relationship"
val neoRestCypher = neoRestBase / "cypher"
def selfRestUriToId(uri: String) = uri.substring(uri.lastIndexOf('/') + 1).toInt
def neoRestNodeIndex(indexName: String) = neoRestBase / "index" / "node" / indexName
def neoRestNodeById(id: Int) = neoRestNode / id.toString
def neoRestRelById(id: Int) = neoRestRel / id.toString
lazy val root:{val id:Int } = Http(neoRestBase <:< Map("Accept" -> "application/json") >! {
jsValue => new {
val id = selfRestUriToId((jsValue \ "reference_node").as[String])
}
})
}
import sbt._
import Keys._
import PlayProject._
object ApplicationBuild extends Build {
val appName = "Play20WithNeo4J"
val appVersion = "1.0"
val sbtIdeaRepo = "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
val appDependencies = Seq(
// Add your project dependencies here,
"net.databinder" %% "dispatch-http" % "0.8.7" withSources
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
resolvers ++= Seq(
sbtIdeaRepo
)
)
}
object Neo4J extends Controller {
object neo extends Neo4JRestService
def createNodeWithProperties = Action {
val props = toJson(Map("prop1" -> "value1", "prop2" -> "value2"))
val (node, data: JsObject) = Http(
(neo.neoRestNode <<(stringify(props), "application/json"))
<:< Map("Accept" -> "application/json")
>! {
jsValue =>
((jsValue \ "self").as[String], (jsValue \ "data").as[JsObject])
})
Ok("" + node + " -- " + data.toString)
}
def relationship(id: Int) = Action {
val (rel, start, end) = Http(neo.neoRestRelById(id) <:< Map("Accept" -> "application/json") >! {
jsValue =>
((jsValue \ "self").as[String],
(jsValue \ "start").as[String],
(jsValue \ "end").as[String])
})
Ok("" + start + " - [" + rel + "] - " + end)
}
}
[Play20WithNeo4J] $ reload
[Play20WithNeo4J] $ idea
import dispatch._
import play.api.libs.json._
import play.api.libs.json.Json._
class PlayJsonHandlers(subject: HandlerVerbs) {
/**Process response as JsValue in block */
def >![T](block: (JsValue) => T) = subject >- {
(str) =>
block(parse(str))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment