This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ua-parser enrichment (closes snowplow/snowplow#62)
Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62) Added ua-parser enrichment (closes snowplow/snowplow#62)
- Loading branch information
Showing
8 changed files
with
250 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...om.snowplowanalytics.snowplow.enrich/common/enrichments/registry/UaParserEnrichment.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/*Copyright (c) 2012-2015 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 | ||
package snowplow | ||
package enrich | ||
package common | ||
package enrichments | ||
package registry | ||
|
||
// Maven Artifact | ||
import org.apache.maven.artifact.versioning.DefaultArtifactVersion | ||
|
||
// Scalaz | ||
import scalaz._ | ||
import Scalaz._ | ||
|
||
// ua-parser | ||
import ua_parser.Parser | ||
import ua_parser.Client | ||
import ua_parser.Client | ||
|
||
// json4s | ||
import org.json4s._ | ||
import org.json4s.JValue | ||
import org.json4s.JsonDSL._ | ||
import org.json4s.jackson.JsonMethods._ | ||
|
||
// Iglu | ||
import iglu.client.SchemaKey | ||
import iglu.client.validation.ProcessingMessageMethods._ | ||
import utils.ScalazJson4sUtils | ||
|
||
/** | ||
* Companion object. Lets us create a UaParserEnrichment | ||
* from a JValue. | ||
*/ | ||
object UaParserEnrichmentConfig extends ParseableEnrichment { | ||
|
||
val supportedSchemaKey = SchemaKey("com.snowplowanalytics.snowplow", "ua_parser_config", "jsonschema", "1-0-0") | ||
|
||
def parse(config: JValue, schemaKey: SchemaKey): ValidatedNelMessage[UaParserEnrichment.type] = | ||
isParseable(config, schemaKey).map(_ => UaParserEnrichment) | ||
} | ||
|
||
/** | ||
* Config for an ua_parser_config enrichment | ||
* | ||
* Uses uap-java library to parse client attributes | ||
*/ | ||
case object UaParserEnrichment extends Enrichment { | ||
|
||
val version = new DefaultArtifactVersion("0.1.0") | ||
|
||
/* | ||
* Check's for null for Option Version variable | ||
*/ | ||
def prependDot (versionElement: String) :String = { | ||
if (versionElement != null) { | ||
"." + versionElement | ||
} else { | ||
"" | ||
} | ||
} | ||
|
||
/* | ||
* Prepends space before the versionElement | ||
*/ | ||
def prependSpace (versionElement: String) :String = { | ||
if (versionElement != null) { | ||
" " + versionElement | ||
} else { | ||
"" | ||
} | ||
} | ||
|
||
/* | ||
* Check's for null value in versionElement for family parameter | ||
*/ | ||
def checkNull (versionElement:String) :String = { | ||
if(versionElement == null) { | ||
"" | ||
} else { | ||
versionElement | ||
} | ||
} | ||
/** | ||
* Extracts the client attributes | ||
* from a useragent string, using | ||
* UserAgentEnrichment. | ||
* | ||
* @param useragent The useragent | ||
* String to extract from. | ||
* Should be encoded (i.e. | ||
* not previously decoded). | ||
* @return the json or | ||
* the message of the | ||
* exception, boxed in a | ||
* Scalaz Validation | ||
*/ | ||
def extractUserAgent(useragent: String): Validation[String, JsonAST.JObject] = { | ||
|
||
val uaParser = new Parser() | ||
|
||
val c = try { | ||
uaParser.parse(useragent) | ||
} catch { | ||
case e => return "Exception parsing useragent [%s]: [%s]".format(useragent, e.getMessage).fail | ||
} | ||
// To display useragent version | ||
val useragentVersion = checkNull(c.userAgent.family) + prependSpace(c.userAgent.major) + prependDot(c.userAgent.minor) + prependDot(c.userAgent.patch) | ||
|
||
// To display operating system version | ||
val osVersion = checkNull(c.os.family) + prependSpace(c.os.major) + prependDot(c.os.minor) + prependDot(c.os.patch) + prependDot(c.os.patchMinor) | ||
|
||
val json = | ||
( ("schema" -> "iglu:com.snowplowanalytics.snowplow/ua_parser_context/jsonschema/1-0-0") ~ | ||
("data" -> | ||
("useragentFamily" -> c.userAgent.family) ~ | ||
("useragentMajor" -> c.userAgent.major) ~ | ||
("useragentMinor" -> c.userAgent.minor) ~ | ||
("useragentPatch" -> c.userAgent.patch) ~ | ||
("useragentVersion" -> useragentVersion) ~ | ||
("osFamily" -> c.os.family) ~ | ||
("osMajor" -> c.os.major) ~ | ||
("osMinor" -> c.os.minor) ~ | ||
("osPatch" -> c.os.patch) ~ | ||
("osPatchMinor" -> c.os.patchMinor) ~ | ||
("osVersion" -> osVersion) ~ | ||
("deviceFamily" -> c.device.family) | ||
) | ||
) | ||
|
||
json.success | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...wplowanalytics.snowplow.enrich/common/enrichments/registry/UserAgentUtilsEnrichment.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...nowplowanalytics.snowplow.enrich.common/enrichments/registry/UaParserEnrichmentSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/**Copyright (c) 2012-2015 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.enrich.common | ||
package enrichments | ||
package registry | ||
|
||
// Specs2 | ||
import org.specs2.Specification | ||
import org.specs2.matcher.DataTables | ||
import org.specs2.scalaz._ | ||
|
||
// Scalaz | ||
import scalaz._ | ||
import Scalaz._ | ||
|
||
// Json4s | ||
import org.json4s._ | ||
import org.json4s.JValue | ||
import org.json4s.JsonDSL._ | ||
import org.json4s.jackson.JsonMethods._ | ||
|
||
class UaParserEnrichmentSpec extends org.specs2.mutable.Specification with ValidationMatchers with DataTables { | ||
import UaParserEnrichment._ | ||
val jsonTest = """{"schema":"iglu:com.snowplowanalytics.snowplow/ua_parser_context/jsonschema/1-0-0","data":{"useragentFamily":"Mobile Safari","useragentMajor":"5","useragentMinor":"1","useragentPatch":null,"useragentVersion":"Mobile Safari 5.1","osFamily":"iOS","osMajor":"5","osMinor":"1","osPatch":"1","osPatchMinor":null,"osVersion":"iOS 5.1.1","deviceFamily":"iPhone"}}""" | ||
val jsonTestNew = """{"schema":"iglu:com.snowplowanalytics.snowplow/ua_parser_context/jsonschema/1-0-0","data":{"useragentFamily":"Safari","useragentMajor":"8","useragentMinor":"0","useragentPatch":null,"useragentVersion":"Safari 8.0","osFamily":"Mac OS X","osMajor":"10","osMinor":"10","osPatch":null,"osPatchMinor":null,"osVersion":"Mac OS X 10.10","deviceFamily":"Other"}}""" | ||
|
||
"useragent parser" should { | ||
"parse useragent" in { | ||
"Input UserAgent" | "Parsed UserAgent" | | ||
"Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3" !! jsonTest | | ||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/600.1.25 (KHTML, like Gecko) Version/8.0 Safari/600.1.25" !! jsonTestNew |> { | ||
(input, expected) => { | ||
UaParserEnrichment.extractUserAgent(input) must beSuccessful.like { case a => compact(render(a)) must_== compact(render(parse(expected))) } | ||
} | ||
} | ||
} | ||
} | ||
} |