Skip to content

Commit

Permalink
Got examples done
Browse files Browse the repository at this point in the history
  • Loading branch information
dpp committed Mar 4, 2011
1 parent 3f24f54 commit 3a8141e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
60 changes: 60 additions & 0 deletions samples/http_rest/http_rest.lyx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ HTTP and REST
\begin_layout Standard
We explored Lift's HTML generation features.
Let's dive down to a lower level and handle HTTP requests REST-style.
The code for this chapter can be found at
\begin_inset CommandInset href
LatexCommand href
name "https://github.com/dpp/simply_lift/tree/master/samples/http_rest"
target "https://github.com/dpp/simply_lift/tree/master/samples/http_rest"

\end_inset


\end_layout

\begin_layout Section
Expand All @@ -66,7 +75,58 @@ Lift makes use of Scala's pattern matching to allow you match incoming HTTP
the results.
\end_layout

\begin_layout Section
REST the hard way
\end_layout

\begin_layout Standard
The
\end_layout

\begin_layout Standard
\begin_inset CommandInset include
LatexCommand lstinputlisting
filename "src/main/scala/code/lib/BasicExample.scala"
lstparams "caption={BasicExample.scala}"

\end_inset


\end_layout

\begin_layout Section
Making it easier with
\family typewriter
RestHelper
\end_layout

\begin_layout Standard
blasdasd
\end_layout

\begin_layout Standard
\begin_inset CommandInset include
LatexCommand lstinputlisting
filename "src/main/scala/code/lib/BasicWithHelper.scala"
lstparams "caption={BasicWithHelper.scala}"

\end_inset


\end_layout

\begin_layout Section
A complete REST example
\end_layout

\begin_layout Standard
\begin_inset CommandInset include
LatexCommand lstinputlisting
filename "src/main/scala/code/lib/FullRest.scala"
lstparams "caption={FullRest.scala}"

\end_inset


\end_layout

Expand Down
17 changes: 17 additions & 0 deletions samples/http_rest/src/main/scala/code/lib/FullRest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import net.liftweb._
import common._
import http._
import rest._
import util._
import Helpers._
import json._
import scala.xml._

Expand Down Expand Up @@ -45,5 +47,20 @@ object FullRest extends RestHelper {
// the POST body and update the item
case Item(item) :: Nil JsonPost json -> _ =>
Item(mergeJson(item, json)).map(Item.add(_): JValue)

// Wait for a change to the Items
// But do it asynchronously
case "change" :: Nil JsonGet _ =>
RestContinuation.async {
f => {
// schedule a "Null" return if there's no other answer
// after 110 seconds
Schedule.schedule(() => f(JNull), 110 seconds)

// register for an "onChange" event. When it
// fires, return the changed item as a response
Item.onChange(item => f(item: JValue))
}
}
})
}
43 changes: 38 additions & 5 deletions samples/http_rest/src/main/scala/code/model/Item.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model

import net.liftweb._
import util._
import Helpers._
import common._
import json._

Expand All @@ -25,6 +26,8 @@ object Item {

private var items: List[Item] = parse(data).extract[List[Item]]

private var listeners: List[Item => Unit] = Nil

/**
* Convert the item to JSON format. This is
* implicit and in the companion object, so
Expand Down Expand Up @@ -63,8 +66,6 @@ object Item {
case _ => None
}
}



/**
* Convert an item to XML
Expand Down Expand Up @@ -95,6 +96,7 @@ object Item {
*/
def inventoryItems: Seq[Item] = items

// The raw data
private def data =
"""[
{"id": "1234", "name": "Cat Food",
Expand Down Expand Up @@ -128,10 +130,16 @@ object Item {
]
"""

/**
* Select a random Item
*/
def randomItem: Item = synchronized {
items(Helpers.randomInt(items.length))
}

/**
* Find an item by id
*/
def find(id: String): Box[Item] = synchronized {
items.find(_.id == id)
}
Expand All @@ -142,7 +150,7 @@ object Item {
def add(item: Item): Item = {
synchronized {
items = item :: items.filterNot(_.id == item.id)
item
updateListeners(item)
}
}

Expand Down Expand Up @@ -174,12 +182,37 @@ object Item {
case _ => true
}

ret
ret.map(updateListeners)
}

/**
* Update listeners when the data changes
*/
private def updateListeners(item: Item): Item = {
synchronized {
listeners.foreach(f =>
Schedule.schedule(() => f(item), 0 seconds))

listeners = Nil
}
item
}

/**
* Add an onChange listener
*/
def onChange(f: Item => Unit) {
synchronized {
// prepend the function to the list of listeners
listeners ::= f
}
}

}


/**
* A helper that will JSON serialize BigDecimal
*/
object BigDecimalSerializer extends Serializer[BigDecimal] {
private val Class = classOf[BigDecimal]

Expand Down

0 comments on commit 3a8141e

Please sign in to comment.