Created
February 20, 2012 22:47
-
-
Save andypetrella/1872053 to your computer and use it in GitHub Desktop.
Neo4J using Play and Dispatch
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
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]) | |
} | |
}) | |
} |
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
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 | |
) | |
) | |
} |
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
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) | |
} | |
} |
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
[Play20WithNeo4J] $ reload | |
[Play20WithNeo4J] $ idea |
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
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) = subject >- { | |
(str) => | |
block(parse(str)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment