~ s/run/workout/
authorAlexandre Bertails <bertails@w3.org>
Wed, 24 Aug 2011 15:32:51 -0400
changeset 65 f959fad73852
parent 64 764b4cf0f325
child 66 b86696030786
~ s/run/workout/
src/main/scala/Main.scala
src/main/scala/Record.scala
src/main/scala/Workout.scala
src/test/scala/RecordAPISpec.scala
src/test/scala/Test.scala
src/test/scala/WorkoutAPISpec.scala
--- a/src/main/scala/Main.scala	Wed Aug 24 14:43:50 2011 -0400
+++ b/src/main/scala/Main.scala	Wed Aug 24 15:32:51 2011 -0400
@@ -7,7 +7,7 @@
 import unfiltered.scalate._
 import unfiltered.jetty._
 
-import org.w3.virtualtrainer.database._
+import org.w3.virtualtrainer.model._
 import com.novus.salat._
 import com.novus.salat.global._
 import com.mongodb.casbah.Imports._
@@ -36,13 +36,13 @@
       Ok ~> ContentType("application/xhtml+xml") ~> Scalate(req, "geolocation.ssp")(engine)
   }
   
-  val recordAPI = unfiltered.filter.Planify {
-    case req @ POST(Path(Seg("user" :: user :: "run" :: Nil))) & Host(host) => {
+  val workoutAPI = unfiltered.filter.Planify {
+    case req @ POST(Path(Seg("user" :: user :: "workout" :: Nil))) & Host(host) => {
       val json:MongoDBObject = com.mongodb.util.JSON.parse(Body.string(req)).asInstanceOf[com.mongodb.DBObject]
-      val record = grater[Record].asObject(json)
-      RecordDAO.insert(record) match {
+      val workout = grater[Workout].asObject(json)
+      RecordDAO.insert(workout) match {
         case Some(id) => {
-          val path = "%s://%s/user/%s/run/%s" format (req.underlying.getScheme, host, user, id.toString)
+          val path = "%s://%s/user/%s/workout/%s" format (req.underlying.getScheme, host, user, id.toString)
           Created ~> Location(path) ~> ResponseString("object available at %s" format path)
         }
         case None => {
@@ -50,10 +50,10 @@
         }
       }
     }
-    case req @ Path(Seg("user" :: user :: "run" :: id :: Nil)) => req match {
+    case req @ Path(Seg("user" :: user :: "workout" :: id :: Nil)) => req match {
       case GET(_) => {
         RecordDAO.findOneByID(id) match {
-          case Some(record) => Ok ~> ContentType("application/json") ~> ResponseString(record.asJSON)
+          case Some(workout) => Ok ~> ContentType("application/json") ~> ResponseString(workout.asJSON)
           case None => NotFound
         }
       }
@@ -85,7 +85,7 @@
       ctx.resources(fromClasspath("scripts/").toURI.toURL)
     }.context("/styles"){ ctx:ContextBuilder =>
       ctx.resources(fromClasspath("styles/").toURI.toURL)
-    }.filter(tracker).filter(recordAPI).filter(echo).run()
+    }.filter(tracker).filter(workoutAPI).filter(echo).run()
 
     
   }
--- a/src/main/scala/Record.scala	Wed Aug 24 14:43:50 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-package org.w3.virtualtrainer.database
-
-import com.novus.salat._
-import com.novus.salat.global._
-import com.mongodb.casbah.Imports._
-import com.novus.salat.annotations._
-
-import com.novus.salat.dao._
-import com.mongodb.casbah.MongoConnection
-  
-/*
-{
- activity:"http://dbpedia.org/resource/Running",
- events:
- [
-  {
-   t: 1314049555597,
-   c: [-71.09068298339844,42.361671924591064,66]
-  },
-  {
-   t: 1314049559622,
-   s: "pause"
-  },
-  {
-   t: 1314549555597,
-   c: [-71.09068298339844,42.361671924591064,66]
-  },
-   t: 1315049559622,
-   s: "pause"
-  }
- ]
-}
-*/
-
-case class Record(@Key("_id") id: ObjectId = new ObjectId,
-                  activity:String,
-                  events:List[Event]) {
-
-  def asJSON:String = grater[Record].asDBObject(this).toString
-
-}
-
-case class Event(@Key("t") timestamp:Long,
-                 @Key("c") coordinates:Option[(Double, Double, Option[Double])],
-                 @Key("s") status:Option[String])
-
-object RecordDAO extends SalatDAO[Record, ObjectId](collection = MongoConnection("gw42.w3.org", 27017)("virtual_trainer")("user/records")) {
- 
-  def findOneByID(id:String):Option[Record] = {
-    try {
-      val objectId = new ObjectId(id)
-      findOneByID(objectId)
-    } catch {
-      case iae:IllegalArgumentException => None
-    }
-    
-  }
-  
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/Workout.scala	Wed Aug 24 15:32:51 2011 -0400
@@ -0,0 +1,59 @@
+package org.w3.virtualtrainer.model
+
+import com.novus.salat._
+import com.novus.salat.global._
+import com.mongodb.casbah.Imports._
+import com.novus.salat.annotations._
+
+import com.novus.salat.dao._
+import com.mongodb.casbah.MongoConnection
+  
+/*
+{
+ activity:"http://dbpedia.org/resource/Running",
+ events:
+ [
+  {
+   t: 1314049555597,
+   c: [-71.09068298339844,42.361671924591064,66]
+  },
+  {
+   t: 1314049559622,
+   s: "pause"
+  },
+  {
+   t: 1314549555597,
+   c: [-71.09068298339844,42.361671924591064,66]
+  },
+   t: 1315049559622,
+   s: "pause"
+  }
+ ]
+}
+*/
+
+case class Workout(@Key("_id") id: ObjectId = new ObjectId,
+                  activity:String,
+                  events:List[Event]) {
+
+  def asJSON:String = grater[Workout].asDBObject(this).toString
+
+}
+
+case class Event(@Key("t") timestamp:Long,
+                 @Key("c") coordinates:Option[(Double, Double, Option[Double])],
+                 @Key("s") status:Option[String])
+
+object RecordDAO extends SalatDAO[Workout, ObjectId](collection = MongoConnection("gw42.w3.org", 27017)("virtual_trainer")("user/workouts")) {
+ 
+  def findOneByID(id:String):Option[Workout] = {
+    try {
+      val objectId = new ObjectId(id)
+      findOneByID(objectId)
+    } catch {
+      case iae:IllegalArgumentException => None
+    }
+    
+  }
+  
+}
--- a/src/test/scala/RecordAPISpec.scala	Wed Aug 24 14:43:50 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-package org.w3.virtualtrainer
-
-import org.w3.virtualtrainer.util.RequestHelper
-
-import org.specs._
-import java.net.URL
-
-object RecordAPISpec extends Specification("Specification for the Record API") with unfiltered.spec.jetty.Served {
-  import unfiltered.response._
-  import unfiltered.request._
-
-  import dispatch._
-
-  def setup = { _.filter(VirtualTrainerApp.recordAPI) }
-
-  val record = """
-{
- "activity":"http://dbpedia.org/resource/Running",
- "events":
- [
-  {
-   "t": 1314049555597,
-   "c": [-71.09068298339844,42.361671924591064,66]
-  },
-  {
-   "t": 1314049559622,
-   "s": "pause"
-  },
-  {
-   "t": 1314549555597,
-   "c": [-71.09068298339844,42.361671924591064,66]
-  },
-  {
-   "t": 1315049559622,
-   "s": "pause"
-  }
- ]
-}
-"""
-    
-  "POST record" should {
-    val POSTrun = host / "user" / "jdoe" / "run"
-    "return a location telling where the record is GETtable" in {
-      val locationHeader:String = Http(RequestHelper.POST(POSTrun, record) >:> { headers =>
-        headers get "Location" getOrElse sys.error("no Location header") head
-      })
-      val locationURL = new URL(locationHeader)
-      locationURL.getHost must_== POSTrun.host.getHostName
-      locationURL.getPort must_== POSTrun.host.getPort
-      val run = Http(url(locationHeader) as_str)
-      run must not be empty
-    }
-  }
-  
-}
\ No newline at end of file
--- a/src/test/scala/Test.scala	Wed Aug 24 14:43:50 2011 -0400
+++ b/src/test/scala/Test.scala	Wed Aug 24 15:32:51 2011 -0400
@@ -1,4 +1,6 @@
-package org.w3.virtualtrainer.database
+package org.w3.virtualtrainer
+
+import org.w3.virtualtrainer.model._
 
 import org.scalatest.FunSuite
 
@@ -6,8 +8,8 @@
 
   test("") {
 
-    val record =
-      Record(
+    val workout =
+      Workout(
           activity="http://dbpedia.org/resource/Running",
           events=List(
               Event(timestamp=1314049555597L, coordinates=Some((-71.09068298339844,42.361671924591064,Some(66))), status=None),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/scala/WorkoutAPISpec.scala	Wed Aug 24 15:32:51 2011 -0400
@@ -0,0 +1,55 @@
+package org.w3.virtualtrainer
+
+import org.w3.virtualtrainer.util.RequestHelper
+
+import org.specs._
+import java.net.URL
+
+object WorkoutAPISpec extends Specification("Specification for the Workout API") with unfiltered.spec.jetty.Served {
+  import unfiltered.response._
+  import unfiltered.request._
+
+  import dispatch._
+
+  def setup = { _.filter(VirtualTrainerApp.workoutAPI) }
+
+  val workout = """
+{
+ "activity":"http://dbpedia.org/resource/Running",
+ "events":
+ [
+  {
+   "t": 1314049555597,
+   "c": [-71.09068298339844,42.361671924591064,66]
+  },
+  {
+   "t": 1314049559622,
+   "s": "pause"
+  },
+  {
+   "t": 1314549555597,
+   "c": [-71.09068298339844,42.361671924591064,66]
+  },
+  {
+   "t": 1315049559622,
+   "s": "pause"
+  }
+ ]
+}
+"""
+  
+  "POST workout" should {
+    val POSTworkout = host / "user" / "jdoe" / "workout"
+    "return a location telling where the workout is GETtable" in {
+      val locationHeader:String = Http(RequestHelper.POST(POSTworkout, workout) >:> { headers =>
+        headers get "Location" getOrElse sys.error("no Location header") head
+      })
+      val locationURL = new URL(locationHeader)
+      locationURL.getHost must_== POSTworkout.host.getHostName
+      locationURL.getPort must_== POSTworkout.host.getPort
+      val run = Http(url(locationHeader) as_str)
+      run must not be empty
+    }
+  }
+  
+}
\ No newline at end of file