Skip to content

Instantly share code, notes, and snippets.

View gbougeard's full-sized avatar

Greg BOUGEARD gbougeard

View GitHub Profile
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Association du Paris Java User Group.
*
* 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 furnished to do so,
// Enum type based on Sum types (OR)
trait ADTEnum[A] {
import play.api.data.mapping.json.Writes._
import play.api.data.mapping.json.Rules._
val list: Seq[A]
def withName(name: String): Option[A] = {
list.find(_.toString.toLowerCase == name.toLowerCase)
}
@divarvel
divarvel / enums.scala
Created January 23, 2015 22:23
Enums
package models
import scalaz._
import Scalaz._
import anorm._
import org.postgresql.util.PGobject
import org.postgresql.jdbc4.Jdbc4Array
@julienrf
julienrf / Application.scala
Created April 26, 2012 20:47
Write a Mapping[A] and get a QueryStringBindable[A] for free
// --- Definition of a query string binder for type A using a Mapping[A]
object Binders {
implicit def mappingBinder[A](implicit mapping: Mapping[A]) = new QueryStringBindable[A] {
override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, A]] = {
val data = for {
(k, ps) <- params
if k startsWith key
p <- ps.headOption
} yield (k.drop(key.length + 1), p)
@leon
leon / SlickUserService.scala
Created March 12, 2013 07:55
Secure Social UserService Slick implementation (Not Working)
package service
import play.api._
import securesocial.core._
import securesocial.core.providers.Token
import securesocial.core.UserId
import models._
class SlickUserService(application: Application) extends UserServicePlugin(application) {
@mandubian
mandubian / gist:5231877
Last active December 15, 2015 08:38
Play Framework Json sample: Copy JsObject only if passwords equal
import play.api.libs.json._
import play.api.data.validation._
import play.api.libs.functional.syntax._
val json = Json.obj(
"name" -> "John",
"email" -> "john.doe@company.com",
"password" -> "password",
"confirmPassword" -> "password"
)
@helena
helena / CloudExtension.scala
Last active December 21, 2015 00:29
Simple (and truncated) example of the CloudExtension's load-time ordered provisioning and ordered graceful shutdown. Unfortunately this had to be written in an older version of scala and akka - for now. MyNodeGuardian.scala is started in CloudExtension.register() and is a sample of using ProvisioningGuardian which extends OrderedGracefulShutdown.
/**
* CloudExtension and factory for creating CloudExtension instances.
* Example:
* {{{
* val application = CloudExtension(system, config)
* }}}
*
* @author Helena Edelson
*/
object CloudExtension extends ExtensionId[CloudExtension] with ExtensionIdProvider {
@simonbrandhof
simonbrandhof / gist:7914401
Last active December 31, 2015 01:29
Quality profile used for the Java sources of SonarQube project. Note that some rules added in versions 4.0 or 4.1 are probably enabled. This file can be imported via the top-right link "Restore Profile" of the page "Quality Profiles".
<?xml version="1.0" encoding="UTF-8"?>
<profile><name>Default</name><language>java</language><rules><rule><repositoryKey>squid</repositoryKey><key>S1223</key><priority>MAJOR</priority></rule><rule><repositoryKey>squid</repositoryKey><key>S1318</key><priority>CRITICAL</priority></rule><rule><repositoryKey>squid</repositoryKey><key>S1312</key><priority>MAJOR</priority><parameters><parameter><key>format</key><value>LOG(?:GER)?</value></parameter></parameters></rule><rule><repositoryKey>squid</repositoryKey><key>S1319</key><priority>MAJOR</priority></rule><rule><repositoryKey>squid</repositoryKey><key>S1231</key><priority>MINOR</priority></rule><rule><repositoryKey>pmd</repositoryKey><key>CompareObjectsWithEquals</key><priority>MAJOR</priority></rule><rule><repositoryKey>squid</repositoryKey><key>S1217</key><priority>CRITICAL</priority></rule><rule><repositoryKey>squid</repositoryKey><key>S1219</key><priority>CRITICAL</priority></rule><rule><repositoryKey>squid</repositoryKey><key>S1210</key><priority>CRITICAL</p
@ohac
ohac / hmacsha1.scala
Created February 22, 2010 09:00
HMAC-SHA1 for Scala
import javax.crypto.spec.SecretKeySpec
import javax.crypto.Mac
object A {
def main(args: Array[String]): Unit = {
val secret = new SecretKeySpec("the_secret".getBytes, "HmacSHA1")
val mac = Mac.getInstance("HmacSHA1")
mac.init(secret)
val result: Array[Byte] = mac.doFinal("foo".getBytes)
println(result.map(_.toString).mkString(","))
@jwreagor
jwreagor / aws-sdk-example.go
Created November 20, 2015 00:53
Example of the AWS SDK v1.0 release...
// Create a session
s := session.New(aws.NewConfig().WithRegion("us-west-2"))
// Add a handler to print every API request for the session
s.Handlers.Send.PushFront(func(r *request.Request) {
fmt.Printf("Request: %s/%s\n", r.ClientInfo.ServiceName, r.Operation)
})
// We want to start all instances in a VPC, so let's get their IDs first.
ec2client := ec2.New(s)
var instanceIDsToStart []*string
describeInstancesInput := &ec2.DescribeInstancesInput{