Skip to content

Instantly share code, notes, and snippets.

View agemooij's full-sized avatar

Age Mooij agemooij

View GitHub Profile
import java.nio.charset.StandardCharsets._
import java.security._
import javax.crypto._
import javax.crypto.spec._
import base64.Encode.{ apply => toBase64 }
import base64.Encode.{ urlSafe => toBase64UrlSafe }
import base64.Decode.{ apply => fromBase64 }
import base64.Decode.{ urlSafe => fromBase64UrlSafe }
@agemooij
agemooij / SnakifiedSprayJsonSupport.scala
Last active November 18, 2020 14:34
An example of how to customize the field mapping in spray-json to do generic transformation, in this case translating between camelcased Scala attributes and snakecase JSON attributes
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2015 Andrew Snare, Age Mooij
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@agemooij
agemooij / build.sbt
Last active December 21, 2015 03:29
import com.typesafe.sbt.SbtStartScript
seq(SbtStartScript.startScriptForClassesSettings: _*)
seq(Revolver.settings: _*)
name := "demo"
organization := "com.scalapenos"
[info] Compiling 1 Scala source to /Users/age/Development/src/agemooij/riak-scala-client/target/scala-2.10/test-classes...
01/19 23:59:09 DEBUG [akka://default/user/test-server]: SERVER: Starting akka://default/user/test-server on localhost/127.0.0.1:17249
01/19 23:59:09 INFO [akka://default/user/io-bridge]: akka://default/user/io-bridge started
01/19 23:59:09 DEBUG [akka://default/user/io-bridge]: SERVER: Executing Bind(localhost/127.0.0.1:17249,100,LogMark(SERVER))
01/19 23:59:09 INFO [akka://default/user/test-server]: SERVER: akka://default/user/test-server started on localhost/127.0.0.1:17249
01/19 23:59:09 DEBUG [akka://default/user/http-client/$a]: Opening connection 1 to localhost:17249
01/19 23:59:09 DEBUG [akka://default/user/io-bridge]: Executing Connect(localhost/127.0.0.1:17249,None,())
01/19 23:59:09 DEBUG [akka://default/user/io-bridge]: Connection request registered
01/19 23:59:09 DEBUG [akka://default/user/io-bridge]: SERVER: New connection accepted on localhost/127.0.0.1:17249
01/19 23:59:0
@agemooij
agemooij / SprayAdaptiveGzipSupport.scala
Created October 20, 2012 16:32
SprayAdaptiveGzipSupport
trait SprayAdaptiveGzipSupport extends directives.EncodingDirectives {
import spray.httpx.encoding._
def withAdaptiveCompressionSupport: Directive0 =
(decodeRequest(NoEncoding) | decodeRequest(Gzip) | decodeRequest(Deflate)) &
(encodeResponse(NoEncoding) | encodeResponse(Gzip) | encodeResponse(Deflate))
}
object SprayAdaptiveGzipSupport extends SprayAdaptiveGzipSupport
@agemooij
agemooij / akkaLogAndReceive.scala
Created June 21, 2012 16:28
Akka: logging any received message
scala> :paste
// Entering paste mode (ctrl-D to finish)
def wrap(partial: PartialFunction[Any, Unit]): PartialFunction[Any, Unit] = new PartialFunction[Any, Unit] {
def isDefinedAt(a: Any): Boolean = partial.isDefinedAt(a)
def apply(a: Any): Unit = {
println("----> received " + a)
partial.apply(a)
}
}
object LinearFit2 {
case class Point(x: Double = 0.0, y: Double = 0.0) {
def +(that: Point): Point = Point(this.x + that.x, this.y + that.y)
}
implicit def tupleToPoint(tuple: (Double, Double)): Point = Point(tuple._1, tuple._2)
def through (points : Point*) : Point = through(points.toList)
def through (points : List[Point]) : Point = {
val total = points.foldLeft(Point())(_ + _)
@agemooij
agemooij / gist:768208
Created January 6, 2011 17:20
Possible problem with Salat serialization of Scala case class ?
package com.agemooij.test
import java.util.Date
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import com.mongodb.casbah.Imports._
@agemooij
agemooij / MongoRepository.scala
Created December 30, 2010 12:13
A base trait for creating and managing a mongoDB connection/collection
package com.agemooij.mongodb
import com.mongodb.casbah.Imports._
trait MongoRepository {
protected val host = "localhost"
protected val port = 27017
protected val databaseName = "agemooij"
protected val collectionName: String
@agemooij
agemooij / gist:349566
Created March 30, 2010 20:37
Some Gradle code
repositories {
....
add(new org.apache.ivy.plugins.resolver.URLResolver()) {
name = "GitHub"
addArtifactPattern 'http://github.com/asikkema/adoptimizer/downloads/[organization]-[module]-[revision].[ext]'
}
}