Skip to content

Instantly share code, notes, and snippets.

View fernandezpablo85's full-sized avatar

Pablo Fernandez fernandezpablo85

View GitHub Profile
[info] play - Application started (Dev)
[debug] s.s.s.BaseSession - Preparing statement: select x2."id", x2."name", x2."started", x2."patient_id" from "problems" x2 where x2."patient_id" = 1
[debug] s.s.s.BaseSession - Preparing statement: select x2."id", x2."problem_id", x2."patient_id", x2."description", x2."date", x2."error" from "evolutions" x2 where x2."patient_id" = 1
var coso = (function() {
var that = {};
function privada() {
}
that.publica = function() {
};
package model
import scala.concurrent._
import scala.concurrent.duration._
import play.libs.Akka
import play.api.libs.concurrent.Execution.Implicits._
object TimeoutFuture {
def apply[A](timeout: FiniteDuration)(block: => A): Future[A] = {
import org.zeromq.ZMQ;
//
// Reading from multiple sockets in Java
// This version uses a simple recv loop
//
public class msreader {
public static void main (String[] args) throws Exception {
// Prepare our context and sockets
@fernandezpablo85
fernandezpablo85 / Build.scala
Created June 1, 2013 19:56
removing play-java from your play dependencies
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "firehose"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
@fernandezpablo85
fernandezpablo85 / Enumeratee.scala
Last active December 17, 2015 18:39
Enumeratee
// Enumeratee transforms a stream from type A to B.
// It only needs you to specify the A type, it can infer B (in this case JsValue)
val json = Enumeratee.map[StatusUpdate] { status => Json.toJson(status)}
// Now `jsonStream` is a stream of JsValue! :D
val jsonStream = stream.through(json)
@fernandezpablo85
fernandezpablo85 / ToJson.scala
Created May 27, 2013 01:46
transform a case class into JsValue
import play.api.libs.json._
// We need to make an implicit `Writes` for our StatusUpdate class available.
// Play can convert any case class into a JsValue, using macros.
implicit private val StatusWrites = Json.writes[StatusUpdate]
val status = StatusUpdate(user = "pablo", update = "serialize this")
val json = Json.toJson(status)
# Stacktrace
[error] /Users/pfernand/projects/firehose/app/controllers/Application.scala:31: Cannot write an instance of model.StatusUpdate to HTTP response. Try to define a Writeable[model.StatusUpdate]
[error] SimpleResult(header, stream)
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 5 s, completed May 26, 2013 10:37:05 PM
# The message without the noise:
@fernandezpablo85
fernandezpablo85 / Stream.scala
Created May 27, 2013 01:34
Sending a stream to the users
// Our controller method.
def firehose = Action {
// Http headers, we sent the Content-Length to -1 since this is a stream and will never end.
// We also set Connection: close to explicit that the connection should not be reused.
val headers = Map("Content-Length" -> "-1", "Connection" -> "close","Content-Type" -> "application/json")
val header = ResponseHeader(200, headers)
// Return the header + our Enumerator[StatusUpdate]
SimpleResult(header, stream)
@fernandezpablo85
fernandezpablo85 / Updates.scala
Created May 27, 2013 01:25
Sending random status updates
// Create some updates.
val statuses = Array(
StatusUpdate("pablo", "Are stream updates cool or what?"),
StatusUpdate("hn_comments", "Nice tutorial!"),
StatusUpdate("fake_profile", "lorem ipsum bla bla bla bla."),
StatusUpdate("funes_mori", "please kill me before next match.")
)
// Use Play's scheduler to send them (actually Akka, but it's an implementation detail).
// We send roughly 20 updates/second. The first number (0.seconds) is