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

com.gravity.gdk.reco.RecoArticle.scala Maven / Gradle / Ivy

The newest version!
package com.gravity.gdk.reco

import com.gravity.gdk.article.ArticleKey
import com.gravity.gdk.util.date
import com.gravity.gdk.util.error.jsErrorToErrors
import org.joda.time.DateTime
import play.api.libs.functional.syntax._
import play.api.libs.json._

import scalaj.http.HttpResponse

/*
              ___...---''
  ___...---'\'___
''       _.-''  _`'.______\\.
        /_.)  )..-  __..--'\\
       (    __..--''
        '-''\@


 Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ Ⓐ
*/

/**
  * An article recommendation.
  *
  * @param title Article title (headline, etc.).
  * @param clickUrl URL that lets Gravity track the article's click.
  * @param displayUrl "Clean" URL that is suitable for displaying to users.
  * @param author Author full name, if available.
  * @param imageUrl URL to article image. This will be a thumbnail URL if you specify image widths and/or heights
  *                 when using [[com.gravity.gdk.placement.Placement.getRecos]].
  * @param publishTimestamp When the article was published.
  * @param articleImpressionSlug This is Gravity internal use only.
  */
case class RecoArticle(
  articleId: ArticleKey,
  title: String,
  clickUrl: String,
  displayUrl: String,
  author: Option[String],
  imageUrl: String,
  publishTimestamp: DateTime,
  articleImpressionSlug: String
)

object RecoArticle {
  def listFromApiResponse(apiResponse: HttpResponse[String]): Either[Seq[Error], List[RecoArticle]] = {
    Json.parse(apiResponse.body).validate[List[RecoArticle]] match {
      case e: JsError => Left(jsErrorToErrors(e))
      case JsSuccess(articleList, _) => Right(articleList)
    }
  }

  private[gdk] implicit val jsonReader: Reads[RecoArticle] = (
    (__ \ "articleId").read[ArticleKey] and
    (__ \ "title").read[String] and
    (__ \ "clickUrl").read[String] and
    (__ \ "displayUrl").read[String] and
    (__ \ "author").readNullable[String] and
    (__ \ "image").read[String] and
    (__ \ "publishTimestamp").read[DateTime](date.unixTimestampJsonReader) and
    (__ \ "articleImpressionSlug").read[String]
  )(RecoArticle.apply _)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy