def extractMostViewedVideoOverall()

in src/main/scala/com/gu/contentapi/mostviewedvideo/OphanStore.scala [52:73]


  def extractMostViewedVideoOverall(json: String, edition: Option[String]): Either[CustomError, List[MostViewedVideoContainerThrift]] =
    decodeOverallJson(json, edition) flatMap (containers => Right(containers map mostViewedVideoContainerToThrift))

  def extractMostViewedVideoBySection(json: String, edition: Option[String]): Either[CustomError, List[MostViewedVideoContainerThrift]] =
    decodeSectionsJson(json, edition) flatMap (containers => Right(containers map mostViewedVideoContainerToThrift))

  /**
   * Unfortunately, circe cannot easily decode a json string to a thrift object (https://github.com/travisbrown/circe/issues/208).
   * The simplest solution is to decode to intermediary case classes, then construct the thrift objects from these.
   */
  private[this] def decodeOverallJson(json: String, edition: Option[String]): Either[CustomError, List[MostViewedVideoContainerModel]] = {
    import io.circe.generic.auto._
    decode[List[MostViewedVideoModel]](json).fold(
      { error => Left(CustomError(error.getMessage)) },
      { overallVideos =>
        val section = edition match {
          case None => Some("overall")
          case Some(_) => None
        }
        Right(List(MostViewedVideoContainerModel(buildId(edition, section), overallVideos)))
      })
  }