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 EdgeCaseBerg

📉
Wondering why Github added statuses instead of something useful
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / removeAllEmptyChildren.scala
Created February 22, 2017 18:53
How to remove empty text from HTML with Jsoup + Scala
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import scala.collection.JavaConverters._
def removeAllEmptyChildren(doc: Document): = {
val allElements = doc.body().getAllElements()
allElements.asScala.foreach { element =>
if(!element.hasText) {
element.remove()
import com.typesafe.config.ConfigFactory
val c = ConfigFactory.load().getConfig("gs")
import com.mcl.recipesearch.service._
val gsConf = GroceryServerConf(true, new java.net.URI(c.getString("endpoint")), c.getString("clientId"), c.getString("clientApiKey"))
@EdgeCaseBerg
EdgeCaseBerg / how-to-do-without-tuple-horror.scala
Last active June 27, 2017 14:19
Over 22 fields in play-json case class. How to deal.
import play.api.libs.json._
import play.api.libs.functional.syntax._
case class AToM(
a: Int,
b: Int,
c: Int,
d: Int,
e: Int,
f: Int,
@EdgeCaseBerg
EdgeCaseBerg / CustomShellPrompt.scala
Created August 2, 2017 16:08
Make a git prompt in sbt!
import sbt._
import Keys._
// This lifted from https://github.com/mingchuno/aws-wrap/blob/master/project/CustomShellPrompt.scala
object CustomShellPrompt {
val Branch = """refs/heads/(.*)\s""".r
def gitBranchOrSha =
(Process("git symbolic-ref HEAD") #|| Process("git rev-parse --short HEAD")).!! match {