-
-
Save casualjim/5188769 to your computer and use it in GitHub Desktop.
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
implicit class PagePatternHelper(val sc: StringContext) extends AnyVal { | |
def page(args: Any*): PagePattern = { | |
val template = sc.standardInterpolator(treatEscapes, args) | |
val templateUrl = rl.Uri(template) | |
val path = templateUrl.path | |
val routeMatcher = new SinatraRouteMatcher(path) | |
val queryMultiParams = rl.MapQueryString.parseString(templateUrl.query.rawValue) | |
val queryParams = queryMultiParams.mapValues(_.head) | |
PagePattern(routeMatcher, queryParams) | |
} | |
} | |
case class PagePattern(routeMatcher: SinatraRouteMatcher, expectedQueryParams: Map[String, String]) { | |
def unapply(path: String): Option[Map[String,String]] = { | |
val url = rl.Uri(path) | |
for { | |
baseMultiParams <- routeMatcher(url.path) | |
} yield { | |
val baseParams = baseMultiParams.mapValues(_.head) | |
val queryMultiParams = rl.MapQueryString.parseString(url.query.rawValue) | |
val queryParams = queryMultiParams collect {case (k,vh::vt) => k->vh} | |
baseParams ++ queryParams | |
} | |
} | |
} | |
// usage: | |
val SomePagePage = page"""/:tvc/tv/show/:beid/:show_name?_zt=:time&_zp=:ProviderID&_zr=:RegionID&_zs=:ServiceID""" | |
def parsePage(page: String) = page match { | |
case ShowPage(params) => params | |
case _ => ??? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment