Skip to content

Instantly share code, notes, and snippets.

View agemooij's full-sized avatar

Age Mooij agemooij

View GitHub Profile
@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"
@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
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 / 1. README.md
Last active September 26, 2018 13:19
Quick spray-routing authentication example

Extremely bare example of Spray routing authentication using session cookies and XSRF tokens

This code was quickly ripped out of an active project to serve as an example. It will not compile in any way!

Any questions? Add them to the comments!

Notes

  • the application uses a version of the cake pattern to compose the Spray routing application together from various traits
  • a lot of those traits should be pretty self-explaining based on their name but if requested I can paste them into this Gist
@agemooij
agemooij / S3Signing.scala
Created June 6, 2014 20:07
Snippet from an S3 download/upload client based purely on spray-client
/**
* Amazon S3 uploads and downloads need to have an authorization header containing a signature based on a very specific formatting
* of several of the headers of the HTTP request. This method only implements the parts that are required for our use cases,
* thus very specific situations like headers spanning multiple lines are not supported.
* For the details of the string-to-sign that is constructed see http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html?r=1821
*/
def signAwsRequest(method: String, dateString: String, contentType: String, bucket: String, resource: String, amzHeaders: List[HttpHeader] = List.empty[HttpHeader], contentMd5: String = ""): String = {
val bucketResource = s"/$bucket$resource"
val signableAmzHeaders = amzHeaders.sortBy(_.name).map { header ⇒
def addItem(newItem: BasketItem): BasketState = {
copy(
items.foldRight((false, List.empty[BasketItem])) {
case (item, (_, out)) if (item.matchesProductAndSizeOf(newItem)) (true, item.incrementNumberBy(newItem.numberOfProducts) :: out)
case (item, (didSomethingMatch, out)) (didSomethingMatch, item :: out)
} match {
case (false, _) newItem :: items
case (true, modifiedItems) modifiedItems
}
)
@agemooij
agemooij / HttpsDirectives.scala
Last active April 15, 2020 23:21
Spray HttpDirectives
package scalapenos.spray.auth
import spray.routing._
import spray.routing.Directives._
import spray.http.HttpHeaders._
import spray.http.StatusCodes._
trait HttpsDirectives {
import HttpsDirectives._
@agemooij
agemooij / README.md
Last active July 29, 2016 21:36
My SBT prompt config

My customized SBT shell prompt

Example screenshot

A heavily customied SBT shell prompt that shows the following information:

  • a handy marker to show you are in SBT
  • the current Git status (a clean working directory is green, a dirty one is yellow)
  • the current project, including the root project of a multi-project build

Requirements

@agemooij
agemooij / NormalizeSupport.scala
Last active September 22, 2022 11:02
Scala text normalization
package rfs.rebb
package common
/**
* Performs standard Java/unicode normalization on the trimmed and lowercased form
* of the input String and then adds a few extra tricks for dealing with special
* characters.
*
* JVM/Unicode normalization references (warning: learning curve black hole, beware!):
*
@agemooij
agemooij / agnoster-agemooij.zsh-theme
Last active December 6, 2017 18:43
Customized version of the Agnoster @ohmyzsh theme
# vim:ft=zsh ts=2 sw=2 sts=2
#
# Age's version of Agnoster's Theme - https://gist.github.com/3712874
# A Powerline-inspired theme for ZSH
#
# # README
#
# In order for this theme to render correctly, you will need a
# [Powerline-patched font](https://github.com/Lokaltog/powerline-fonts).
#