<commit_before>/*
 * Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package com.snowplowanalytics.snowplow.hadoop.etl
package enrichments
package web

// Java
import java.net.URI

// Scala
import scala.collection.JavaConversions._

// Scalaz
import scalaz._
import Scalaz._

// Apache URLEncodedUtils
import org.apache.http.NameValuePair
import org.apache.http.client.utils.URLEncodedUtils

// This project
import utils.EtlUtils

/**
 * Holds enrichments related to marketing and campaign
 * attribution.
 */
object AttributionEnrichments {

  /**
   * Immutable case class for a marketing campaign. Any
   * or all of the five fields can be set.
   */
  case class MarketingCampaign(
      val medium:   Option[String],
      val source:   Option[String],
      val term:     Option[String],
      val content:  Option[String],
      val campaign: Option[String])

  /**
   * Extract the marketing fields
   * from a URL.
   *
   * @param uri The URI to extract
   *        marketing fields from
   * @param encoding The encoding of
   *        the URI being parsed
   * @return the MarketingCampaign
   *         or an error message,
   *         boxed in a Scalaz
   *         Validation
   */
  def extractMarketingFields(uri: URI, encoding: String): Validation[String, MarketingCampaign] = {

    val parameters = try {
      Option(URLEncodedUtils parse(uri, encoding))
    } catch {
      case _ => return "Could not parse uri [%s]".format(uri).fail
    }

    // If somebody wants to rewrite this without
    // the mutable variables, please go ahead
    var medium, source, term, content, campaign: Option[String] = None
    for (params <- parameters) {
      for (p <- params.toList) {
        val name  = p.getName
        val value = p.getValue.toLowerCase // Should be lower case anyway

        name match {
          case "utm_medium" =>
            medium = EtlUtils.decodeSafely(value, encoding)
          case "utm_source" =>
            source = EtlUtils.decodeSafely(value, encoding)
          case "utm_term" =>
            term = EtlUtils.decodeSafely(value, encoding)
          case "utm_content" =>
            content = EtlUtils.decodeSafely(value, encoding)
          case "utm_campaign" =>
            campaign = EtlUtils.decodeSafely(value, encoding)
        }
      }
    }

    MarketingCampaign(
      medium,
      source,
      term,
      content,
      campaign).success
  }
}<commit_msg>Simplified the marketing field pattern match code<commit_after>/*
 * Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package com.snowplowanalytics.snowplow.hadoop.etl
package enrichments
package web

// Java
import java.net.URI

// Scala
import scala.collection.JavaConversions._

// Scalaz
import scalaz._
import Scalaz._

// Apache URLEncodedUtils
import org.apache.http.NameValuePair
import org.apache.http.client.utils.URLEncodedUtils

// This project
import utils.EtlUtils

/**
 * Holds enrichments related to marketing and campaign
 * attribution.
 */
object AttributionEnrichments {

  /**
   * Immutable case class for a marketing campaign. Any
   * or all of the five fields can be set.
   */
  case class MarketingCampaign(
      val medium:   Option[String],
      val source:   Option[String],
      val term:     Option[String],
      val content:  Option[String],
      val campaign: Option[String])

  /**
   * Extract the marketing fields
   * from a URL.
   *
   * @param uri The URI to extract
   *        marketing fields from
   * @param encoding The encoding of
   *        the URI being parsed
   * @return the MarketingCampaign
   *         or an error message,
   *         boxed in a Scalaz
   *         Validation
   */
  def extractMarketingFields(uri: URI, encoding: String): Validation[String, MarketingCampaign] = {

    val parameters = try {
      Option(URLEncodedUtils parse(uri, encoding))
    } catch {
      case _ => return "Could not parse uri [%s]".format(uri).fail
    }

    // If somebody wants to rewrite this without
    // the mutable variables, please go ahead
    var medium, source, term, content, campaign: Option[String] = None
    for (params <- parameters) {
      for (p <- params.toList) {
        val name  = p.getName
        lazy val value = EtlUtils.decodeSafely(
          p.getValue.toLowerCase, encoding) // Should actually be lower case anyway

        name match {
          case "utm_medium" => medium = value
          case "utm_source" => source = value
          case "utm_term" => term = value
          case "utm_content" => content = value
          case "utm_campaign" => campaign = value
        }
      }
    }

    MarketingCampaign(
      medium,
      source,
      term,
      content,
      campaign).success
  }
}
{'fraction_non_alphanumeric': 0.08254285238808454, 'fraction_numerical': 0.005325345315360293, 'mean_word_length': 2.6468446601941746, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7326720', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1650, 'n_words': 613}