Skip to content

Instantly share code, notes, and snippets.

@jeffmay
Created February 26, 2014 21:58
Show Gist options
  • Save jeffmay/9239482 to your computer and use it in GitHub Desktop.
Save jeffmay/9239482 to your computer and use it in GitHub Desktop.
Github Event models and serializers with JsPath
package models.github
import org.joda.time.DateTime
import models.{Entity, EntityId}
import models.common.Email
import play.api.libs.json._
import play.api.libs.functional.syntax._
trait Event[T <: Entity] {
val id: EventId
val data: T
val timestamp: DateTime
}
case class EventId(value: String) extends AnyVal with EntityId
case class GithubEvent(id: EventId, data: GithubPushEventData, timestamp: DateTime)
extends Event[GithubPushEventData]
case class GithubPushEventData(
repository: Repository,
commitCount: Int,
githubUser: GithubUser,
pushedAt: DateTime
)
case class GithubPushEventId(value: String) extends EntityId
case class Repository(id: RepositoryId, name: String, isPrivate: Boolean) extends Entity
case class RepositoryId(value: String) extends AnyVal with EntityId
case class GithubUser(name: String, email: Email)
object GithubSerializers {
implicit val repositoryReader: Reads[Repository] = {
val reads =
(__ \ "id").read[String].map(RepositoryId) and
(__ \ "name").read[String] and
(__ \ "private").read[Boolean]
reads.tupled map Repository.tupled
}
implicit val userReader: Reads[GithubUser] = {
val reads =
(__ \ "name").read[String] and
(__ \ "email").read[String].map(Email)
reads.tupled map GithubUser.tupled
}
implicit val pushEventReader: Reads[GithubPushEventData] = {
val reads = {
(__ \ "repository").read[Repository] and
(__ \ "commits").read[List[Any]].map(_.size) and
(__ \ "pusher").read[GithubUser] and
(__ \ "repository" \ "pushed_at").read[String].map(DateTime.parse)
}
reads.tupled map GithubPushEventData.tupled
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment