Skip to content

Instantly share code, notes, and snippets.

@cer
Created April 18, 2013 21:43
Show Gist options
  • Save cer/5416475 to your computer and use it in GitHub Desktop.
Save cer/5416475 to your computer and use it in GitHub Desktop.
A scala Future version of the Netflix video grid example ( http://techblog.netflix.com/2013/02/rxjava-netflix-api.html ) that was written using RxJava.
@RunWith(classOf[JUnitRunner])
class VideoGridTest extends FunSuite with ShouldMatchers {
// Make scalaz work with futures
implicit object FutureFunctor extends Functor[Future] {
def fmap[A, B](r : Future[A], f : scala.Function1[A, B]) : Future[B] = r.map(f)
}
implicit object FutureApply extends Apply[Future] {
def apply[A, B](f: Future[(A) => B], a: Future[A]) = (f zip a).map { case (f, a) => f(a) }
}
// "domain classes"
case class MetaData(name : String)
case class Rating(name : String, userId : String)
case class Bookmark(name : String, userId : String)
case class GridEntry(m : MetaData, r : Rating, b : Bookmark)
case class Video(title: String) {
def getMetaData() = Future { MetaData("For " + title)}
def getRating(userId : String) = Future { Rating("For " + title, "User " + userId)}
def getBookmark(userId : String) = Future { Bookmark("For " + title, "User " + userId)}
}
case class VideoList(videos : List[Video]) {
def getVideos(): Future[List[Video]] = Future { videos }
}
def getListOfLists(userId: String) = Future {
List(VideoList(List(Video("When Harry met Sally"), Video("Blues Brothers"))), VideoList(List(Video("When Dan met Barry"), Video("Saturday Night Fever"))))
}
def getVideoGridForDisplay(userId: String) = {
getListOfLists(userId).flatMap { lists => Future.sequence {
lists.map { videoList =>
videoList.getVideos().flatMap {
videos => Future.sequence {
videos.take(10).map { video =>
val m = video.getMetaData()
val r = video.getRating(userId)
val b = video.getBookmark(userId)
(m |@| r |@| b) {GridEntry(_, _, _)}
}
}
}
}
}
}.map(_.flatten)
}
test("getVideoGridForDisplay should return list of GridEntries") {
val z = getVideoGridForDisplay("foo")
val r : List[GridEntry] = Await.result(z, 1 seconds)
r should equal(List(GridEntry(MetaData("For When Harry met Sally"),Rating("For When Harry met Sally","User foo"),Bookmark("For When Harry met Sally","User foo")),
GridEntry(MetaData("For Blues Brothers"),Rating("For Blues Brothers","User foo"),Bookmark("For Blues Brothers","User foo")),
GridEntry(MetaData("For When Dan met Barry"),Rating("For When Dan met Barry","User foo"),Bookmark("For When Dan met Barry","User foo")),
GridEntry(MetaData("For Saturday Night Fever"),Rating("For Saturday Night Fever","User foo"),Bookmark("For Saturday Night Fever","User foo"))))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment