Building a query URL in Scala
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;")