<commit_before>package com.meetup.iap

import java.util.Date

import org.json4s.{DefaultFormats, JValue}
import org.json4s.JsonAST.JInt
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization.read
import java.text.SimpleDateFormat

/**
 * Handles requests to apple's verify services and parsing of response.
 */
object AppleApi {

  implicit def formats = new DefaultFormats {
    override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss 'Etc/GMT'")
  }

  /**
   * @param receipt Actual receipt info for the request receipt (original)
   * @param latestReceipt Latest receipt charged in a subscription.
   * @param latestExpiredReceiptInfo Latest receipt charged in an expired subscription.
   */
  case class ReceiptResponse(
    latestReceipt: Option[String] = None,
    latestReceiptInfo: List[ReceiptInfo],
    statusCode: Int = 0)

  case class ReceiptInfo(
    originalPurchaseDate: Date,
    originalTransactionId: String,
    transactionId: String,
    purchaseDate: Date,
    expiresDate: Date,
    productId: String,
    isTrialPeriod: Boolean = false,
    cancellationDate: Option[Date] = None,
    quantity: Int = 1)

  def parseResponse(json: String): ReceiptResponse =
    read[ReceiptResponse](compact(render(parse(json).camelizeKeys)))

  /** Safely extract a receipt response from a parsed JSON structure. */
  def parseResponse(j: JValue): Either[Either[String, Status], ReceiptResponse] =
    j \ "status" match {
      case JInt(code) =>
        Status.get(code.toInt) match {
          case ValidReceipt => Right(read[ReceiptResponse](compact(render(j.camelizeKeys))))
          case s => Left(Right(s))
        }
      case _ => Left(Left("Invalid JSON"))
    }
}<commit_msg>refactor healthcheck job (builds, but not done)<commit_after>package com.meetup.iap

import java.util.Date

import org.json4s.{DefaultFormats, JValue}
import org.json4s.JsonAST.JInt
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization.read
import java.text.SimpleDateFormat

/**
 * Handles requests to apple's verify services and parsing of response.
 */
object AppleApi {

  implicit def formats = new DefaultFormats {
    override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss 'Etc/GMT'")
  }

  /**
   * @param receipt Actual receipt info for the request receipt (original)
   * @param latestReceipt Latest receipt charged in a subscription.
   * @param latestExpiredReceiptInfo Latest receipt charged in an expired subscription.
   */
  case class ReceiptResponse(
    latestReceipt: Option[String] = None,
    latestReceiptInfo: List[ReceiptInfo],
    statusCode: Int = 0) {
    val latestInfo: Option[ReceiptInfo] = latestReceiptInfo.reverse.headOption
  }

  case class ReceiptInfo(
    originalPurchaseDate: Date,
    originalTransactionId: String,
    transactionId: String,
    purchaseDate: Date,
    expiresDate: Date,
    productId: String,
    isTrialPeriod: Boolean = false,
    cancellationDate: Option[Date] = None,
    quantity: Int = 1)

  def parseResponse(json: String): ReceiptResponse =
    read[ReceiptResponse](compact(render(parse(json).camelizeKeys)))

  /** Safely extract a receipt response from a parsed JSON structure. */
  def parseResponse(j: JValue): Either[Either[String, Status], ReceiptResponse] =
    j \ "status" match {
      case JInt(code) =>
        Status.get(code.toInt) match {
          case ValidReceipt => Right(read[ReceiptResponse](compact(render(j.camelizeKeys))))
          case s => Left(Right(s))
        }
      case _ => Left(Left("Invalid JSON"))
    }
}
{'fraction_non_alphanumeric': 0.10175634234736548, 'fraction_numerical': 0.003345413994981879, 'mean_word_length': 3.875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2002460', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1060, 'n_words': 286}