Skip to content

Instantly share code, notes, and snippets.

View EdgeCaseBerg's full-sized 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 February 17, 2017 20:42
This works even though my actual class doesn't for some reason. The real class fails on g and w :/
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 January 30, 2017 21:58
Example of how to clean out encoded characters from JSON values in scala
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 January 12, 2017 14:15
For a blog post later on (and maybe a PR to Play's documentation)

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 October 13, 2020 16:37
Basic Rate Limiting Actor implementation
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 November 10, 2016 19:10
Example of how to use copy_to in a reg-ex individual word or full string search
PUT example_copy
{
"mappings": {
"example" : {
"properties": {
"original" : {
"type": "string",
"copy_to": "copied"
},
"copied" : {
@EdgeCaseBerg
EdgeCaseBerg / GuiceThrowingProvidersExample.scala
Last active March 31, 2020 13:54
Example of using the ThrowingProviders and CheckedProviders in Guice. See full context: http://www.ethanjoachimeldridge.info/tech-blog/guice-scala-checked-providers
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 April 28, 2016 21:53 — forked from dk8996/BasicAuthFilter.scala
Basic Auth Filter for Play Framework
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 February 3, 2016 16:13
Playing with string contexts and what one could do with them.
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 January 25, 2016 16:10
sbt build task to see dependencies in a project.
//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 January 11, 2016 16:38
Get log from stdout without a log file
1. look up process id,
2. cd to /proc/{id}/fd
3. tail -f 1