Building a query URL in Scala I'm working on a webapp in ScalaJS and want to create a query url for requesting some JSON.

Right now I'm using a method called urlBuilder to take the query options from an Options case class and return a String that represents a usable query URL. The urlBuilder method, despite my best efforts, has become somewhat ugly. 

Am I missing out on any syntactic sugar or language features that could make this a little easier to understand/cleaner looking? Is pattern matching + string interpolation the best strategy here?

case class GameRegions(americas: Boolean, europe: Boolean, asia: Boolean)

case class Options(startRank: Option[Int], endRank: Option[Int], startDate: Option[Date], endDate: Option[Date], startTime: Option[Date], endTime: Option[Date], regions: GameRegions, gameMode: Boolean)

//Builds the URL based on the provided options object
def urlBuilder(baseUrl: String, o: Options): String = {
  baseUrl + "?" + (o.startRank match {
    case Some(x) =&gt; s"rank[0]=$x&amp;"
    case None =&gt; ""
  }) + (o.endRank match {
    case Some(x) =&gt; s"rank[1]=$x&amp;"
    case None =&gt; ""
  }) + (o.startDate match {
    case Some(x) =&gt; s"added[0]=@${x.getTime / 1000}&amp;"
    case None =&gt; ""
  }) + (o.endDate match {
    case Some(x) =&gt; s"added[1]=@${x.getTime / 1000}&amp;"
    case None =&gt; ""
  }) + (o.startTime match {
    case Some(x) =&gt; s"time[0]=@${x.getTime / 1000}&amp;"
    case None =&gt; ""
  }) + (o.endTime match {
    case Some(x) =&gt; s"time[1]=@${x.getTime / 1000}&amp;"
    case None =&gt; ""
  }) + (o.regions match {
    case GameRegions(false, false, false) =&gt; ""
    case GameRegions(x, y, z) =&gt; {
      var i: Int = -1
      if (x) {
        i = i + 1
        s"region[$i]=Americas&amp;"
      } else {
        ""
      } + (if (y) {
        i = i + 1
        s"region[$i]=Europe&amp;"
      } else {
        ""
      }) + (if (z) {
        i = i + 1
        s"region[$i]=Asia&amp;"
      } else {
        ""
      }) + "&amp;"
    }
  }) + s"format[0]=${if (o.gameMode) "Standard" else "Wild"}"
}
Sometimes pattern matching over Option could be replaced with fold or map/getOrElse

o.startRank.fold("")(x =&gt; s"rank[0]=$x&amp;")

o.startRank.map(x =&gt; s"rank[0]=$x&amp;").getOrElse("")




You could notice that url parameters have a pattern key=value joined with &amp;. To leverage this pattern url builder could be split into separate steps:


create a map Map[String,String] of parameters i.e.

Map(
  "rank[0]" -&gt; o.startRank.fold("")(_.toString),      
  ...
  "time[0]" -&gt; o.startTime.map(time =&gt; time.getTime/1000).map(_.toString).getOrElse("")

join parameters into string

map.filter(_._2.nonEmpty).map { 
  case (key, value) =&gt; s"$key=$value" 
}.mkString("&amp;")