Skip to content

Instantly share code, notes, and snippets.

View dbousamra's full-sized avatar

Dominic Bou-Samra dbousamra

  • Piccolo Health
  • Australia
View GitHub Profile

Bring back the 90's.

You're lonely and bored. Your doritos are stale and no one is online, this loneliness you feel has a cure...A TAMAGOTCHI

For those of you who have never heard of a Tamagotchi, here's a quick summary:

A tamagotchi is a virtual pet whose life you must sustain through various activities including eating, playing, making it sleep etc. Tamagotchi's go through several life cycles, most notably, egg/infant, teen, adult, elderly. Tamagotchi's can die from lack of attention (in the classic ones, half a day of neglect would kill it) and also from age. For more information check the wiki http://en.wikipedia.org/wiki/Tamagotchi

Your job is to create a tamagotchi via command line, gui or any other avenue you'd like. Even just an internal API and a main method demonstrating it's usage. The goal is to see how you handle open ended design requirements.

def makeDownstreamRequest(client: Client, req: Request, service: DownstreamService, path: String, includeQueryParams: Boolean = true): Task[Response] = {
val serviceUriToUse = req.headers.get(`X-Forwarded-Proto`) match {
case Some(proto) => proto.value match {
case "https" => service.serviceUri.https
case "http" => service.serviceUri.http
case _ => service.serviceUri.http
}
case None => service.serviceUri.http
}
object EnrichedHttpService {
case class EnrichedRequest[A](a: A, request: Request)
type EnrichedHttpService[A] = Service[EnrichedRequest[A], Response]
/** Alternative application which lifts a partial function to an `AuthedHttpService`,
* answering with a [[Response]] with status [[Status.NotFound]] for any requests
* where the function is undefined.
*/
class AuthedRequest(a: AuthedAccount) extends Request {
def account: AuthedAccount = this.a
}
object AuthedHttpService {
type AuthedHttpService = Service[AuthedRequest, Response]
/** Alternative application which lifts a partial function to an `AuthedHttpService`,
* answering with a [[Response]] with status [[Status.NotFound]] for any requests
package com.cammy.nvr.repository.device
import com.cammy.nvr.domain.camera.{CameraId, CameraConfiguration}
import com.cammy.nvr.domain.device._
import doobie.imports._
trait DeviceRepository {
def addDevice(device: Device): ConnectionIO[Device] = {
DeviceQueries.insertDevice(device)
def getDeviceStatus(deviceId: DeviceId): Task[DeviceServiceError \/ DeviceState] = {
websockets.publishWithResponse[StatusResponseMessage](DeviceMessage(deviceId, StatusUpdateRequestMessage))
.map(_.leftMap[DeviceServiceError](WsError.apply))
.flatMap {
case \/-(res) =>
val state = DeviceState(DeviceStateId.random, deviceId, res.cameras.map(CameraStatus.fromCameraStatusDto), DeviceStatus.fromDeviceStatusDto(res.device), Instant.now())
databaseDeviceService.updateDeviceState(deviceId, state)
case -\/(err) => Task.apply(-\/(err))
}
}
package com.cammy.nvr.ws.messages
import argonaut._, Argonaut._
import com.cammy.nvr.domain.dtos.{CameraStatusDto, DeviceStatusDto}
import com.cammy.nvr.domain.manifest.VideoManifest
sealed trait DeviceResponsePayload
case class StatusResponseMessage(cameras: List[CameraStatusDto], device: DeviceStatusDto) extends DeviceResponsePayload
object DoobieHelpers {
/*
* See: https://tpolecat.github.io/doobie-0.2.0/10-Custom-Mappings.html
*
* Some modern databases support a json column type that can store structured data as a JSON document, along with
* various SQL extensions to allow querying and selecting arbitrary sub-structures. So an obvious thing we might
* want to do is provide a mapping from Scala model objects to JSON columns, via some kind of JSON serialization
* library.
trait Replaceable[A] {
def replace[A, B](src: A)(in: A, out: B): B
}
trait StringExample extends Replaceable[String] {
def replace(src: String)(in: String, out: String): String = {
src.replace(in, out)
}
}
object Example {
implicit val es = Executors.newCachedThreadPool
val metrics = new MetricsCollectorImpl(MetricsCollectorConfiguration("example", "localhost", 8125))
val ironConfig: IronMqConfig = ???
val iron = IronMqMessaging(ironConfig, QueueName("example-queue"))
def main(args: Array[String]) {
val consumer = StreamMq(iron, metrics).consumer