Created
          July 5, 2013 16:09 
        
      - 
      
- 
        Save binarytemple/5935557 to your computer and use it in GitHub Desktop. 
    Extract parameters from a URL
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | import java.net.URI | |
| object URLutils { | |
| type LS = List[String] | |
| /** | |
| * Extract a map of parameters from a URI | |
| * @param url | |
| * @return | |
| */ | |
| def paramsToMap(url: URI): Map[String, LS] = { | |
| import java.net.URLDecoder | |
| def mkMap(s: String): Map[String, LS] = { | |
| val x = s.split('&').map(_.split('=').toList).collect { | |
| case k :: v :: Nil => Some(URLDecoder.decode(k, "utf-8") -> URLDecoder.decode(v, "utf-8")) | |
| }.flatten | |
| var m = Map.empty[String, LS].withDefaultValue(List.empty[String]) | |
| x.foreach { | |
| t => | |
| m = m + (t._1 -> (t._2 :: m(t._1))) | |
| } | |
| m | |
| } | |
| url.toString.dropWhile(_ != '?').toList match { | |
| case head :: tail => mkMap(tail.mkString) | |
| case _ => Map.empty[String, LS] | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment