Skip to content

Instantly share code, notes, and snippets.

@WadeWaldron
WadeWaldron / ChunkIteratee.scala
Last active August 29, 2015 14:10
Streaming Play Enumerators through Spray (ChunkIteratee)
package chunkedresponses
import akka.actor.ActorRef
import akka.util.Timeout
import akka.pattern.ask
import play.api.libs.iteratee.{Done, Step, Input, Iteratee}
import spray.http.HttpData
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
@WadeWaldron
WadeWaldron / ChunkedResponder.scala
Created November 20, 2014 23:11
Streaming Play Enumerators through Spray (ChunkedResponder)
package chunkedresponses
import akka.actor.{Actor, ActorRef}
import spray.http.HttpHeaders.RawHeader
import spray.http._
object ChunkedResponder {
case class Chunk(data: HttpData)
case object Shutdown
case object Ack
@WadeWaldron
WadeWaldron / package.scala
Created November 20, 2014 23:11
Streaming Play Enumerators through Spray (package)
import akka.actor.{ActorContext, ActorRefFactory, Props}
import play.api.libs.iteratee.Enumerator
import spray.http.{HttpData, ContentType}
import spray.routing.RequestContext
import scala.concurrent.ExecutionContext
package object chunkedresponses {
implicit class ChunkedRequestContext(requestContext: RequestContext) {
def completeChunked(contentType: ContentType, enumerator: Enumerator[HttpData])
@WadeWaldron
WadeWaldron / CustomerExample.scala
Last active July 28, 2018 13:15
Akka Streams Customer Example
import akka.actor.ActorSystem
import akka.stream.ActorFlowMaterializer
import akka.stream.scaladsl.{Flow, Sink, Source}
import scala.collection.immutable
import scala.util.Random
object InputCustomer {
def random():InputCustomer = {
InputCustomer(s"FirstName${Random.nextInt(1000)} LastName${Random.nextInt(1000)}")
@WadeWaldron
WadeWaldron / UserStore.scala
Created August 28, 2015 14:58
2015-08-28-DataStoreAbstractions-UserStore
trait UserStore {
def find(userId: UserId): Option[User]
def save(user: User): User
}
@WadeWaldron
WadeWaldron / UserAddressStore.scala
Created August 28, 2015 14:59
2015-08-28-DataStoreAbstractions-UserAddressStore
trait UserAddressStore {
def find(userId: UserId): Option[Address]
}
@WadeWaldron
WadeWaldron / UserStoreTests.scala
Last active August 28, 2015 15:23
2015-08-28-DataStoreAbstractions-UserStoreTests
trait UserStoreTests extends FreeSpec {
def userStore: UserStore
“find” - {
“should return a User when it exists in the store” in {
val user = User() // Let's ignore the details of what a user looks like or how it is constructed.
userStore.save(user)
val result = userStore.find(user.id)
assert(result === user)
}
@WadeWaldron
WadeWaldron / MigratingUserStore.scala
Created August 28, 2015 15:01
2015-08-28-DataStoreAbstractions-MigratingUserStore
class MigratingUserStore(origin: UserStore, destination: UserStore) extends UserStore {
def find(userId: UserId): Option[User] = {
destination.find(userId) match {
case None =>
origin.find(userId)
case result =>
result.map(user => destination.save(user))
}
}
@WadeWaldron
WadeWaldron / AkkaTyped-SimpleBehavior.scala
Last active October 8, 2015 16:50
AkkaTyped-SimpleBehavior
import akka.typed.ScalaDSL._
case class Message(value: String)
val behavior = Static[Message] {
case Message(value) => println(value)
}
@WadeWaldron
WadeWaldron / AkkaTyped-ActorSystem.scala
Last active October 8, 2015 16:50
AkkaTyped-ActorSystem
import akka.typed._
val system = ActorSystem("MySystem", Props(behavior))
system ! Message("Hello World")
system.terminate()