scala-news-reader

rss/atom news reader in scala

git clone https://9o.is/git/scala-news-reader.git

URLFormatter.scala

(1925B)


      1 package com.joereader.lib
      2 
      3 import java.util.regex.Pattern
      4 import Helper._
      5 
      6 /**
      7  * Extremely useful for formatting url your way.
      8  * @param in URL in string format
      9  */
     10 case class URLFormatter(in: String) {
     11   def url = new java.net.URL(validate(in))
     12 
     13   def ext = "htm" :: "html" :: "php" :: "php3" :: "jsp" :: "asp" :: "phtml" ::
     14     "shtm" :: "shtml" :: "cgi" :: "cfm" :: "cfml" :: Nil
     15 
     16   def urlWithoutQueryParams(p: String*): String = {
     17     val sep = in.split("\\?")
     18     if (sep.size < 2) this.toString
     19     else {
     20       val params = sep.last.split("&")
     21       this.toString + "?" +
     22         params.map {
     23           param =>
     24             val key = param.split("=").head
     25             if (p.exists(_ == key)) ""
     26             else param
     27         }.filterNot(_ == "").mkString("&")
     28     }
     29   }
     30 
     31   def pattern: Pattern = {
     32 
     33     def removeFileExtension(p: String) = {
     34       val arr = p.split( """\.""")
     35       val last = arr.lastOption.getOrElse("")
     36       if (ext.exists(_ == last))
     37         arr.slice(0, arr.length - 1).mkString(".")
     38       else p
     39     }
     40 
     41     def removeWWW(h: String) =
     42       if (h.slice(0, 4) == "www.") h.slice(4, h.length) else h
     43 
     44     val s = removeWWW(url.getHost) + removeFileExtension(path)
     45     Pattern.compile( """(https?://)?(www\.)?(^""" + s.escape +
     46       """$)(\.(""" + ext.mkString("|") + """))?""", Pattern.CASE_INSENSITIVE)
     47   }
     48 
     49   def path = if (url.getPath == "/") "" else url.getPath
     50 
     51   def hostAndPath = url.getHost + path
     52 
     53   /**
     54    * Prepends http:// if it doesn't have it so it can
     55    * validate with java.net.URL.
     56    */
     57   def validate(str: String): String =
     58     if (str.slice(0, 8).matches("https?://.*")) str else "http://" + str
     59 
     60   override def toString = url.getProtocol + "://" + url.getHost +
     61     (if (url.getPort == -1) "" else ":" + url.getPort) + url.getPath
     62 }
     63 
     64 object URLFormatter {
     65   def same(str1: String, str2: String): Boolean = 
     66     URLFormatter(str1).toString == URLFormatter(str2).toString
     67 }