Skip to content

Instantly share code, notes, and snippets.

val extractedInfo = extractUnmatchedPath & extractRequest & extractClientIP & extractHost
val route = extractedInfo { (path, req, addr, host) =>
// Use the path, addr and host to figure out where the request goes
// and how to rewrite the request
Http().singleRequest(req)
}
" format / wrap and gq comments but not code
setlocal formatoptions=crq
" wrap comments at 80 chars
setlocal textwidth=80
"highlight the 100'th column
set colorcolumn=100
class MyActorSpect // with mixins
{
class SubActorSys(name) extends ActorSys(name) {
def makeActor(): ActorRef = system.actorOf(Props(new MyActor), "MyActor")
}
"My Actor" should {
"do something" in new SubActorSys("Test1") {
// blah
}
@derekwyatt
derekwyatt / Broken-WithoutMagnet.scala
Created January 11, 2014 20:56
Is the magnet pattern the only decent way to get around this limitation in the Scala compiler? `... multiple overloaded alternatives of method set define default arguments.` http://spray.io/blog/2012-12-13-the-magnet-pattern
def set(key: String, value: String)(implicit ec: ExecutionContext, jitter: TTLJitter = NoJitter): Future[CAS]
def set(key: String, value: ByteString)(implicit ec: ExecutionContext, jitter: TTLJitter = NoJitter): Future[CAS]
// Compiler error: ... multiple overloaded alternatives of method set define default arguments.
[error] found : B1(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)
[error] required: B1(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)(in method applyOrElse)
@derekwyatt
derekwyatt / AroundDirective.scala
Created June 14, 2013 15:47
The 'around' directive for spray
def around[A](before: RequestContext => (RequestContext, A))(after: A => HttpResponse => HttpResponse): Directive0 =
mapRequestContext { ctx =>
val (newCtx, a) = before(ctx)
newCtx.withHttpResponseMapped(after(a))
}
@derekwyatt
derekwyatt / works.scala
Last active December 16, 2015 02:39
Transparent deserialization of a JsValue in pattern matching
import spray.json._
object MyProtocol extends DefaultJsonProtocol {
implicit object MyMessageFormat extends RootJsonFormat[MyMessage] {
def write(msg: MyMessage) = JsObject("arg1" -> JsString(msg.arg1), "arg2" -> JsString(msg.arg2))
def read(value: JsValue) = value.asJsObject.getFields("arg1", "arg2") match {
case Seq(JsString(arg1), JsString(arg2)) => new MyMessage(arg1, arg2)
case _ => deserializationError("MyMessage expected")
}
}
@derekwyatt
derekwyatt / Server.scala
Created March 19, 2013 12:42
Spray routing head scratcher...
package org.derekwyatt
import spray.http._
import spray.http.MediaTypes._
import spray.httpx.encoding._
import spray.httpx.SprayJsonSupport
import spray.json._
import spray.json.DefaultJsonProtocol
import spray.routing.HttpService
#!/usr/bin/env ruby
# Load in the PrimalAccess class
require './PrimalAccess.rb'
require 'rubygems'
# We require this particular gem
#
# To install it:
# gem install json