private def raiseAllThePurges()

in src/main/scala/com/gu/fastly/Lambda.scala [33:86]


  private def raiseAllThePurges(event: Event): Option[Decache] = {
    // For a given content type event purge all of the paths associated with it.
    // Use a Fastly Hard purge if the event is a delete.

    val contentType = extractUpdateContentType(event)
    val purgeType = event.eventType match {
      case EventType.Delete => Hard
      case _                => Soft
    }

    def dotcomAliasPurge(path: String) = sendFastlyPurgeRequest(
      path,
      purgeType,
      config.fastlyDotcomServiceId,
      makeDotcomSurrogateKey(path),
      config.fastlyDotcomApiKey,
      contentType
    )
    def jsonAliasPurge(path: String) =
      sendFastlyPurgeRequestForAjaxFile(path, contentType)
    def mapiAliasPurge(path: String) = sendFastlyPurgeRequest(
      path,
      purgeType,
      config.fastlyMapiServiceId,
      makeMapiSurrogateKey(path),
      config.fastlyMapiApiKey,
      contentType
    )

    val purgesToPerform: Seq[String => Boolean] = purgeType match {
      case Hard => Seq(dotcomAliasPurge)
      case Soft => Seq(dotcomAliasPurge, jsonAliasPurge, mapiAliasPurge)
    }

    val pathsToPurge = Seq(event.payloadId) ++ extractAliasPaths(event)

    val wasSuccessful: Boolean = pathsToPurge
      .flatMap { path =>
        purgesToPerform.map(purge => purge(path))
      }
      .forall(_ == true)

    if (wasSuccessful) {
      Some(
        Decache(
          eventType = event.eventType,
          paths = pathsToPurge,
          contentType = contentType
        )
      )
    } else {
      None
    }
  }