package scalaz
package http
package request
import Util.Nel._
import Scalaz._
sealed trait Uri {
val path: NonEmptyList[Char]
val queryString: Option[List[Char]]
import Uri.uri
def apply(p: NonEmptyList[Char]) = uri(p, queryString)
def apply(q: Option[List[Char]]) = uri(path, q)
def +++(f: NonEmptyList[Char] => NonEmptyList[Char]) = uri(f(this.path), queryString)
def ++++(f: Option[List[Char]] => Option[List[Char]]) = uri(path, f(this.queryString))
lazy val pathExtension = path.dropWhile(_ != '.').reverse.takeWhile(_ != '.').reverse.mkString
lazy val parts : List[String] = {
path.list.reverse.foldLeft(List[List[Char]](Nil))((rs, ch) => {
if (ch == '/') { Nil :: rs } else { (ch :: rs.head) :: rs.tail }
}).filter(!_.isEmpty).map(_.mkString)
}
import Util.{asHashMap, mapHeads}
lazy val parameters = queryString ∘ (Util.parameters(_))
lazy val parametersMap = parameters ∘ (asHashMap[List, NonEmptyList](_))
lazy val parametersMapHeads = parametersMap ∘ (mapHeads(_))
}
trait Uris {
implicit def ListUri(cs: List[Char]): Option[Uri] = cs match {
case Nil => None
case x :: _ if x == '?' => None
case h :: t => {
val z = t.span(_ != '?')
Some(Uri.uri(nel(h, z._1), z._2 match {
case Nil => None
case _ :: Nil => None
case _ :: k => Some(k)
}))
}
}
}
object Uri extends Uris {
def unapply(uri: Uri): Option[(NonEmptyList[Char], Option[List[Char]])] =
Some(uri.path, uri.queryString)
def uri(p: NonEmptyList[Char], s: Option[List[Char]]) = new Uri {
val path = p
val queryString = s
}
}