This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object SchemaReads { | |
def trivial[T](description: String)(implicit reads: Reads[T]) = | |
SchemaReads(reads, JsonSchema.Single(description)) | |
implicit def seq[T](implicit sr: SchemaReads[T]): SchemaReads[Seq[T]] = SchemaReads(Reads.seq(sr.reads), JsonSchema.Array(sr.schema)) | |
implicit def set[T](implicit sr: SchemaReads[T]): SchemaReads[Set[T]] = SchemaReads(Reads.set(sr.reads), JsonSchema.Array(sr.schema)) | |
implicit val int: SchemaReads[Int] = trivial("int") | |
implicit val str: SchemaReads[String] = trivial("str") | |
implicit class PimpedJsPath(jsp: JsPath) { | |
def readWithSchema[T](implicit sr: SchemaReads[T]) = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed abstract class JsonSchema { | |
def asJson: JsValue | |
} | |
object JsonSchema { | |
final case class Single(description: String) extends JsonSchema { def asJson = JsString(description) } | |
final case class Optional(field: JsonSchema) extends JsonSchema { def asJson = field.asJson } | |
final case class Array(kind: JsonSchema) extends JsonSchema { def asJson = Json.arr(kind.asJson) } | |
final case class Object(fields: Seq[(String, JsonSchema)]) extends JsonSchema { | |
def asJson = JsObject(fields.map { | |
case (name, JsonSchema.Optional(schema)) => (name + "?") -> schema.asJson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class SchemaReads[T](reads: Reads[T], schema: JsonSchema) { | |
def map[S](f: T => S) = SchemaReads(reads map f, schema) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SlackErrorCode._ | |
def tryToUpdateMessage(token: SlackAccessToken, channel: SlackChannelId, ts: SlackTimestamp, newText: String): Future[Unit] = { | |
val updateRequest = SlackMessageUpdateRequest( | |
text = newText, | |
attachments = Seq.empty, | |
unfurlLinks = false, | |
unfurlMedia = false, | |
parseMode = "none" | |
) | |
slackClient.updateMessage(token, channel, ts, updateRequest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Public(value: String) extends SlackChannelId("C") | |
case class Private(value: String) extends SlackChannelId("G") | |
case class DM(value: String) extends SlackChannelId("D") | |
case class User(value: String) extends SlackChannelId("U") | |
private def parseChannelId(value: String): Try[SlackChannelId] = value.headOption match { | |
case Some('C') => Success(Public(value)) | |
case Some('G') => Success(Private(value)) | |
case Some('D') => Success(DM(value)) | |
case Some('U') => Success(User(value)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@json case class SlackTimestamp(value: String) extends Ordered[SlackTimestamp] { // channel-specific timestamp | |
def compare(that: SlackTimestamp) = value compare that.value | |
def toDateTime: DateTime = Try { | |
// "The bit before the . is a unix timestamp, the bit after is a sequence to guarantee uniqueness." | |
new DateTime(value.split('.').head.toLong * 1000) | |
}.getOrElse(throw new Exception(s"Could not parse a date-time out of $value")) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def runBF[T](bf: BatchFetchable[T]): T = { | |
val entities = Entities( | |
users = getManyUsersFromDb(bf.keys.users), | |
groups = getManyGroupsFromDb(bf.keys,groups), | |
posts = getManyPostsFromDb(bf.keys.posts), | |
msgs = getManyMsgsFromDb(bf.keys.msgs) | |
) | |
bf.run(entities) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(BatchFetchable.user(userId) and BatchFetchable.group(groupId)).tupled.map { | |
case (user, group) => s"${user.fullName} is part of ${group.name}" | |
} |