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

com.gravity.gdk.user.GravityUser.scala Maven / Gradle / Ivy

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

import javax.servlet.http.{Cookie, HttpServletRequest}

import com.gravity.gdk.config.DefaultSettings

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


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

/**
  * See [[com.gravity.gdk.user.GravityUser.fromRequest]] and [[com.gravity.gdk.user.GravityUser.fromCookies]].
  *
  * If you have the Gravity beacon installed on the site with the same domain as the server you're using to serve the
  * current request during which you're retrieving recos, then a Gravity cookie will already be in place and
  * available in the current request's cookies.
  */
case class GravityUser(userGuid: String)

object GravityUser {
  val empty = GravityUser("")

  /** Make a GravityUser from the current request your server is handling. */
  def fromRequest(implicit request: HttpServletRequest): GravityUser = fromCookies(request.getCookies)

  /** Make a GravityUser from available HTTP cookies. */
  def fromCookies(cookies: Array[Cookie]): GravityUser = (for {
    cookies <- Option(cookies)
    userGuidCookie <- cookies.find(_.getName == DefaultSettings.thirdPartyCookieName)
  } yield GravityUser(userGuidCookie.getValue)).getOrElse(GravityUser.empty)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy