Skip to content

Instantly share code, notes, and snippets.

Avatar
📉
Wondering why Github added statuses instead of something useful

Ethan Eldridge EdgeCaseBerg

📉
Wondering why Github added statuses instead of something useful
View GitHub Profile
@EdgeCaseBerg
EdgeCaseBerg / circe-breaking.scala
Last active Feb 17, 2017
This works even though my actual class doesn't for some reason. The real class fails on g and w :/
View circe-breaking.scala
import io.circe._, io.circe.generic.semiauto._
import io.circe.parser.parse
import java.util.Locale
import cats.syntax.either._
object E extends Enumeration {
val x = Value("x")
}
trait JsonEncoderSupport {
@EdgeCaseBerg
EdgeCaseBerg / build.sbt
Created Jan 30, 2017
Example of how to clean out encoded characters from JSON values in scala
View build.sbt
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.5.9"
libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.4"
scalaVersion := "2.11.7"
@EdgeCaseBerg
EdgeCaseBerg / repeated name form mapping in play.md
Created Jan 12, 2017
For a blog post later on (and maybe a PR to Play's documentation)
View repeated name form mapping in play.md

Looking at the source code, there's something useful tips for handling this:

In the binding there's code like this:

def bindFromRequest(data: Map[String, Seq[String]]): Form[T] = {
bind {
  data.foldLeft(Map.empty[String, String]) {
    case (s, (key, values)) if key.endsWith("[]") => s ++ values.zipWithIndex.map { case (v, i) => (key.dropRight(2) + "[" + i + "]") -> v }
    case (s, (key, values)) => s + (key -> values.headOption.getOrElse(""))
@EdgeCaseBerg
EdgeCaseBerg / ActorTest.scala
Last active Oct 13, 2020
Basic Rate Limiting Actor implementation
View ActorTest.scala
package actors
import org.scalatest._
import org.scalatest.concurrent._
import org.scalatest.time.{ Millis, Seconds, Span }
import akka.actor.ActorSystem
import akka.util.Timeout
import java.util.concurrent.TimeUnit.MILLISECONDS
@EdgeCaseBerg
EdgeCaseBerg / example_copy_to.sense
Last active Nov 10, 2016
Example of how to use copy_to in a reg-ex individual word or full string search
View example_copy_to.sense
PUT example_copy
{
"mappings": {
"example" : {
"properties": {
"original" : {
"type": "string",
"copy_to": "copied"
},
"copied" : {
@EdgeCaseBerg
EdgeCaseBerg / GuiceThrowingProvidersExample.scala
Last active Mar 31, 2020
Example of using the ThrowingProviders and CheckedProviders in Guice. See full context: http://www.ethanjoachimeldridge.info/tech-blog/guice-scala-checked-providers
View GuiceThrowingProvidersExample.scala
import com.google.inject.{AbstractModule, Provides, Guice}
import com.google.inject.throwingproviders.{ CheckedProvides, CheckedProvider, ThrowingProviderBinder }
import com.typesafe.config.{ ConfigException, ConfigFactory }
import java.net.{URL, MalformedURLException}
import javax.inject.Inject
import scala.collection.JavaConversions._
import scala.util.{Try, Success, Failure}
@EdgeCaseBerg
EdgeCaseBerg / BasicAuthFilter.scala
Last active Apr 28, 2016 — forked from dk8996/BasicAuthFilter.scala
Basic Auth Filter for Play Framework
View BasicAuthFilter.scala
import com.typesafe.scalalogging.slf4j.Logging
import sun.misc.BASE64Decoder
import play.api.mvc._
import scala.concurrent.Future
import play.mvc.Results._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
object BasicAuthFilter extends Filter with Logging {
private lazy val unauthResult = Results.Unauthorized.withHeaders(("WWW-Authenticate",
"Basic realm=\"myRealm\""))
@EdgeCaseBerg
EdgeCaseBerg / LoggerContext for Play.scala
Last active Feb 3, 2016
Playing with string contexts and what one could do with them.
View LoggerContext for Play.scala
import play.Logger
scala> implicit class LogHelper(val sc: StringContext) extends AnyVal {
| def log(args: Any*): Unit = {
| Logger.info(sc.s(args:_*))
| println(sc.s(args:_*))
| }}
defined class LogHelper
scala> log"Hello $hi"
@EdgeCaseBerg
EdgeCaseBerg / versionReport.sbt
Created Jan 25, 2016
sbt build task to see dependencies in a project.
View versionReport.sbt
//https://groups.google.com/forum/#!topic/simple-build-tool/rcPh-lWbDtk
lazy val versionReport = TaskKey[String]("version-report")
// Add this setting to your project.
versionReport <<= (externalDependencyClasspath in Compile, streams) map {
(cp: Seq[Attributed[File]], streams) =>
val report = cp.map {
attributed =>
attributed.get(Keys.moduleID.key) match {
case Some(moduleId) => "%40s %20s %10s %10s".format(
@EdgeCaseBerg
EdgeCaseBerg / gist:783ddbddf79da9c34a00
Created Jan 11, 2016
Get log from stdout without a log file
View gist:783ddbddf79da9c34a00
1. look up process id,
2. cd to /proc/{id}/fd
3. tail -f 1