All Downloads are FREE. Search and download functionalities are using the official Maven repository.

almond.interpreter.util.DisplayDataOps.scala Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
package almond.interpreter.util

import almond.interpreter.api.DisplayData
import argonaut.Json
import argonaut.Parse.{parse => parseJson}

import scala.language.implicitConversions

final class DisplayDataOps(val displayData: DisplayData) extends AnyVal {

  def jsonData: Map[String, Json] =
    displayData
      .data
      .map {
        case (mimeType, content) =>
          val json =
            if (DisplayDataOps.isJsonMimeType(mimeType))
              parseJson(content)
                .right
                .toOption
                .getOrElse(Json.jString(content))
            else
              Json.jString(content)

          mimeType -> json
      }

  def jsonMetadata: Map[String, Json] =
    displayData
      .metadata
      .map {
        case (key, content) =>
          val json = parseJson(content)
            .right
            .toOption
            .getOrElse(Json.jString(content))

          key -> json
      }
}

object DisplayDataOps {

  private def isJsonMimeType(mimeType: String): Boolean =
    mimeType == "application/json" ||
      (mimeType.startsWith("application/") && mimeType.endsWith("+json"))

  implicit def toDisplayDataOps(displayData: DisplayData): DisplayDataOps =
    new DisplayDataOps(displayData)

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy