Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Card Flip Editor</title>
<style>
body, html {
height: 100%;
margin: 0;
@sam
sam / index.html
Last active July 21, 2023 13:30
Pure CSS tabs example with optional JavaScript to support direct-linking to tab URLs.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Tabs Example</title>
<script>
/*
This script enables direct-linking to a tab.
@sam
sam / MultipartParsing.scala
Created December 2, 2016 15:21
When receiving a multipart POST, parse the application/json part into an entity, and capture the file upload.
pathEndOrSingleSlash {
(post & parameter("rev")) { rev =>
// This is very important, so we can provide a more specific
// implicit to overcome the Json4sSupport default unmarshaller
// that will require an `application/json` ContentType!
import Unmarshaller._
entity(as[Multipart.General]) { body =>
import scala.concurrent.duration._
@sam
sam / for-blahbrehensions.scala
Last active August 26, 2016 22:04
A trivial, common example and for-comprehensions failing at it.
// fold
client ! Projects(rows.headOption.fold(Nil)(_ \ "value" extract))
// map.getOrElse
client ! Projects(rows.headOption
.map(_ \ "value" extract)
.getOrElse(Nil))
// Option.toList
client ! Projects(rows.headOption.toList.map(_ \ "value" extract))
@sam
sam / build.sbt
Created August 26, 2016 18:14
There's got to be a better way...
// Don't include private.conf in main package.
mappings in (Compile, packageBin) ~= {
_ filter {
case (_, "private.conf") => false
case _ => true
}
}
// Don't include private.conf in main sources package.
mappings in (Compile, packageSrc) ~= {
@sam
sam / IdSerializer.scala
Created August 23, 2016 14:22
Example of a json4s Format for a sealed trait (Id) with two implementations (NewId or IdWithRev).
import org.json4s._
case object IdSerializer extends CustomSerializer[Id](formats => ( {
case JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: JField("rev", JString(rev)) :: Nil) => IdWithRev(prefix, id, rev)
case JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: Nil) => NewId(prefix, id)
}, {
case NewId(prefix, id) => JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: Nil)
case IdWithRev(prefix, id, rev) => JObject(JField("prefix", JString(prefix)) :: JField("id", JString(id)) :: JField("rev", JString(rev)) :: Nil)
}
))
@sam
sam / 1_Password.scala
Created March 28, 2016 14:02
How to transform a JSON field into a composed class member during serialization.
// This is just a basic sealed trait implementing a couple cases.
sealed trait Password {
val value: String
def hashed: HashedPassword
def check(plainTextCandidate: String): Boolean
}
case class PlainTextPassword(value: String) extends Password {
def hashed = HashedPassword.Scrypt(value)
def check(plainTextCandidate: String) = plainTextCandidate == value
@sam
sam / FunctionZipping.scala
Created March 22, 2016 14:23
Trying to figure out how I can zip both 1-arg functions, and no-arg functions for use in ScalaTest test-fixture "loan" functions.
type FixtureFunction[T] = T => Any
implicit class FixtureFunctionZipper[A](f1: FixtureFunction[A] => Any) {
def zip[B](f2: FixtureFunction[B] => Any): ((A, B) => Any) => Any = { test =>
f1(a => f2(b => test(a, b)))
}
def zip(f2: (Unit => Any) => Any): (A => Any) => Any = { test =>
f1(a => f2(_ => test(a)))
}
@sam
sam / ActorPerRequest.scala
Created March 14, 2016 17:28
Trying to figure out how to clean up actor-per-request actors with akka-http.
// Just inlining the revelant bits of https://github.com/hseeberger/akka-http-json/blob/master/akka-http-json4s/src/main/scala/de/heikoseeberger/akkahttpjson4s/Json4sSupport.scala#L44
// for the gist.
trait Json4sSupport {
implicit def json4sUnmarshallerConverter[A: Manifest](serialization: Serialization, formats: Formats): FromEntityUnmarshaller[A] =
json4sUnmarshaller(manifest, serialization, formats)
/**
* HTTP entity => `A`
*
* @tparam A type to decode
@sam
sam / BrokenMacro.scala
Created February 12, 2016 14:57
Not sure how to create a macro that would allow me to extend a trait and have generated code.
// Example actor:
class EchoActor extends Actor {
def receive = {
case message => context.sender ! message
}
}
// I want to be able to write:
object EchoActor extends ActorGenerator0