Skip to content

Instantly share code, notes, and snippets.

@sergigp
Created April 5, 2016 13:52
Show Gist options
  • Save sergigp/fcbcd8ce0cb8e897be28d363ec59f1f7 to your computer and use it in GitHub Desktop.
Save sergigp/fcbcd8ce0cb8e897be28d363ec59f1f7 to your computer and use it in GitHub Desktop.
implicit class JsObjectPimp(msg: JsObject) {
private def fieldOf[T: Reads](f: JsObject => JsLookupResult): T = f(msg).asOpt[T].value
// @TODO: Return a RequestId instead of a plain String
def requestId: String = fieldOf[String](_ \ "id")
def conversationId: String = fieldOf[String](_ \ "data" \ "conversation_id")
def messageType: String = fieldOf[String](_ \ "type")
def sequenceNumber: Int = fieldOf[Int](_ \ "data" \ "seq")
// @TODO think its not needed
def interlocutor: String = fieldOf[String](_ \ "data" \ "interlocutor_id")
// @TODO think its not needed
def errorMessages: List[String] = fieldOf[List[String]](_ \ "data" \ "messages")
def messageId: String = fieldOf[String](_ \ "data" \ "message_id")
def messageText: String = fieldOf[String](_ \ "data" \ "text")
def data: JsValue = (msg \ "data").get
def sentAt: Int = fieldOf[Int](_ \ "data" \ "sent_at")
def ackedId: String = fieldOf[String](_ \ "acked_id")
def ackedType: String = fieldOf[String](_ \ "acked_type")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment