@@ -33,7 +33,9 @@ import cats.syntax.all._
3333 */
3434object Parser {
3535 def create [F [_]: Sync ](filePath : String ): F [Either [Exception , Parser ]] =
36- Sync [F ].delay { Source .fromFile(filePath).mkString }.map(rawJson =>
36+ Sync [F ]
37+ .delay { Source .fromFile(filePath).mkString }
38+ .map(rawJson =>
3739 ParseReferers .loadJsonFromString(rawJson).map(referers => new Parser (referers)))
3840}
3941
@@ -78,31 +80,34 @@ class Parser private (referers: Map[String, RefererLookup]) {
7880 internalDomains : List [String ]
7981 ): Option [Referer ] = {
8082 val scheme = refererUri.getScheme
81- val host = refererUri.getHost
82- val path = refererUri.getPath
83- val query = Option (refererUri.getQuery)
83+ val host = refererUri.getHost
84+ val path = refererUri.getPath
85+ val query = Option (refererUri.getQuery)
8486
8587 val validUri = (scheme == " http" || scheme == " https" ) && host != null && path != null
8688
8789 if (validUri) {
88- if ( // Check for internal domains
89- pageHost.map(_.equals(host)).getOrElse(false ) ||
90- internalDomains.map(_.trim()).contains(host)
91- ) {
90+ if (// Check for internal domains
91+ pageHost.exists(_.equals(host)) ||
92+ internalDomains.map(_.trim()).contains(host)) {
9293 Some (InternalReferer )
9394 } else {
94- Some (lookupReferer(host, path).map(lookup => {
95- lookup.medium match {
96- case UnknownMedium => UnknownReferer
97- case SearchMedium => SearchReferer (
98- lookup.source,
99- query.flatMap(q => extractSearchTerm(q, lookup.parameters)))
100- case InternalMedium => InternalReferer
101- case SocialMedium => SocialReferer (lookup.source)
102- case EmailMedium => EmailReferer (lookup.source)
103- case PaidMedium => PaidReferer (lookup.source)
104- }
105- }).getOrElse(UnknownReferer ))
95+ Some (
96+ lookupReferer(host, path)
97+ .map(lookup => {
98+ lookup.medium match {
99+ case UnknownMedium => UnknownReferer
100+ case SearchMedium =>
101+ SearchReferer (
102+ lookup.source,
103+ query.flatMap(q => extractSearchTerm(q, lookup.parameters)))
104+ case InternalMedium => InternalReferer
105+ case SocialMedium => SocialReferer (lookup.source)
106+ case EmailMedium => EmailReferer (lookup.source)
107+ case PaidMedium => PaidReferer (lookup.source)
108+ }
109+ })
110+ .getOrElse(UnknownReferer ))
106111 }
107112 } else {
108113 None
@@ -116,7 +121,9 @@ class Parser private (referers: Map[String, RefererLookup]) {
116121 query.split(" &" ).toList.map { pair =>
117122 val equalsIndex = pair.indexOf(" =" )
118123 if (equalsIndex > 0 ) {
119- (decodeUriPart(pair.substring(0 , equalsIndex)), decodeUriPart(pair.substring(equalsIndex+ 1 )))
124+ (
125+ decodeUriPart(pair.substring(0 , equalsIndex)),
126+ decodeUriPart(pair.substring(equalsIndex + 1 )))
120127 } else {
121128 (decodeUriPart(pair), " " )
122129 }
@@ -125,18 +132,18 @@ class Parser private (referers: Map[String, RefererLookup]) {
125132 private def decodeUriPart (part : String ): String = URLDecoder .decode(part, " UTF-8" )
126133
127134 /**
128- * Determine
135+ * Determine
129136 */
130137 private def lookupReferer (refererHost : String , refererPath : String ): Option [RefererLookup ] = {
131138 val hosts = hostsToTry(refererHost)
132139 val paths = pathsToTry(refererPath)
133140
134141 val results : Stream [RefererLookup ] = for {
135- path <- paths.toStream
136- host <- hosts.toStream
142+ path <- paths.toStream
143+ host <- hosts.toStream
137144 result <- referers.get(host + path).toStream
138145 } yield result
139-
146+
140147 // Since streams are lazy we don't calculate past the first element
141148 results.headOption
142149 }
@@ -146,8 +153,11 @@ class Parser private (referers: Map[String, RefererLookup]) {
146153 * For instance, hostsToTry("www.google.com") == List("www.google.com", "google.com", "com")
147154 */
148155 private def hostsToTry (refererHost : String ): List [String ] = {
149- refererHost.split(" \\ ." ).toList
150- .scanRight(" " )((part, full) => s " $part. $full" ).init
156+ refererHost
157+ .split(" \\ ." )
158+ .toList
159+ .scanRight(" " )((part, full) => s " $part. $full" )
160+ .init
151161 .map(s => s.substring(0 , s.length - 1 ))
152162 }
153163
@@ -156,9 +166,9 @@ class Parser private (referers: Map[String, RefererLookup]) {
156166 * For instance, pathsToTry("google.com/images/1/2/3") == List("/images/1/2/3", "/images", "")
157167 */
158168 private def pathsToTry (refererPath : String ): List [String ] = {
159- refererPath.split(" /" ).toList.filter (_ != " " ).headOption match {
169+ refererPath.split(" /" ).find (_ != " " ) match {
160170 case Some (p) => List (refererPath, " /" + p, " " )
161- case None => List (" " )
171+ case None => List (" " )
162172 }
163173 }
164174}
0 commit comments