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) => s"rank[0]=$x&"     case None => ""   }) + (o.endRank match {     case Some(x) => s"rank[1]=$x&"     case None => ""   }) + (o.startDate match {     case Some(x) => s"added[0][email protected]${x.getTime / 1000}&"     case None => ""   }) + (o.endDate match {     case Some(x) => s"added[1][email protected]${x.getTime / 1000}&"     case None => ""   }) + (o.startTime match {     case Some(x) => s"time[0][email protected]${x.getTime / 1000}&"     case None => ""   }) + (o.endTime match {     case Some(x) => s"time[1][email protected]${x.getTime / 1000}&"     case None => ""   }) + (o.regions match {     case GameRegions(false, false, false) => ""     case GameRegions(x, y, z) => {       var i: Int = -1       if (x) {         i = i + 1         s"region[$i]=Americas&"       } else {         ""       } + (if (y) {         i = i + 1         s"region[$i]=Europe&"       } else {         ""       }) + (if (z) {         i = i + 1         s"region[$i]=Asia&"       } else {         ""       }) + "&"     }   }) + s"format[0]=${if (o.gameMode) "Standard" else "Wild"}" } 

Replay

Category: strings Time: 2016-07-29 Views: 0
Tags: scala strings

Related post

  • Build StackExchange-style url's 2011-07-10

    I'm looking for a way to build what I call 'StackExchange style' url's with Drupal 7. In my definition, that means: A node has a path alias containing both a unique identifier and the node title. For instance, if I'm writing a story and every chapter

  • Building a query which uses percentages to select from certain requirements 2016-01-18

    I am new to rails and programming in general. I am having a problem building a certain functionality, especially because I don't really know what I am looking/ googling for. Lets say I have restaurants with a column "rating". The scale is from 1

  • How to build a query with MongoDB C# Driver v2? 2016-01-22

    I'm trying to build a query in multiple steps but it looks like fluent interface(LINQ methods defined on IQueryable) is gone in the new version of C# driver. //get the collection IMongoCollection<WorkerSession> workerSessions = Select<WorkerSessi

  • Generate multiple tag query URLs 2012-03-11

    As we know WordPress supports multiple tag queries (',' and '+'). The only problem with this add_query_arg() doesn't handle these methods if a query param is already exists in the URL. From the following url www.domain.com?post_type=ptype&param=value

  • Trying to Build TreeView Query in T-SQL 2012-09-12

    I have parent records in two tables, Table1 and Table2. Some records in Table3 are children of Table1, and some are children of Table2. I would like to build a query that I can use to populate a TreeView control with the data from Table3 displaying e

  • Building CAML query with AND and Order by Clause 2013-07-15

    Hi I am trying to build a query using caml query builder - Below is the query (The query = get the values only whose address = 1234st AND LastName = Doe) - I believe the placement of AND operator is giving me error, tried various permutation by chang

  • building a query that it returns the quantity of contacts modified in a 2 or more range of hours 2014-09-04

    I'm trying to build a query where it returns the quantity of modified contacts between two specific hours today grouped by a hours range, for example, the number of contacts that were modified between 10 and 11 and between 11 to 12 grouped by that tw

  • How to directly view a Google Visualization API Query URL as a human-readable table or web page? 2014-11-21

    What is the correct syntax for the URL (see also a related question about documentation) to view a Google Visualization API Query result as a web page containing the tabulated results of the query? "Directly" in the question subject line implies

  • How to build keyword query language for search contains 2015-03-31

    I want to build a query for searching for a mobilephone in sharepoint. I used: *mobilephone*. It's not working for this situation: Phone: 0123456789 Request: 789 --> Not item found. Can you provide me a search query for this?

  • how do I build a dynamic URL in views? 2015-04-11

    I am using drupal 7, have a content type and two taxonomy categories, size and shape. Im trying to build a dynamic url such as mysite.com/node/nid/tid/tid, which would get rewrote as e.g mysite.com/page-1/large/square. I want the content of the url t

  • build caml query for multiple AND & OR 2015-06-08

    how to build caml query for following condition. All the fields all from single list. select empid, ename, salary from emp where empid=(100 or 107 or 99 or 103 or 105) and deptno=10 and mgrId=23 --------------Solutions------------- Try below: <View>

  • CodeIgniter Building LIKE Query with n Number of Array Elements 2016-01-18

    Consider this code chunk, where the switch statement takes a parameter from an ajax request, then turns it into a column name, and passes the column name to a CI or_like query. Note, however, that if the parameter is "address", I will want to co

  • node-sql-query build complex query 2016-02-04

    I use Node.js with sails.js I'm trying to build a query which have a sub-query inside like this: SELECT `id`, `col1`, `col2`, `col3`, (SELECT COUNT(*) FROM `tablename` WHERE (`col1` = 'someValue') AND (`col2` = `t`.`col2`)) AS `total` FROM `tablename

  • How to build a query/expression to select specific points within a buffer? (in QGIS) 2016-02-05

    I have a point layer in QGIS. Each point represents a tree and each tree has information about its height as an attribute. I would like to build a query/expression to select all those trees that have an other tree higher than 7 meters within their 20

  • Convert Eloquent Builder to Query Builder 2016-02-15

    It seems that everyone is asking how to convert Query\Builder to Eloquent\Builder. I need the opposite - I have a Model with some scopes defined which I create a query from. The result that it returns is an instance of my model. I want it to be a a p

  • Building session array from post to build search query 2016-06-23

    So I am making post requests to a page for search queries. I am turning them into a session array so I can paginate the results. The process kind of feels ugly and long winded. I need code review for two things. Shortening / making this code more ele

  • Building Your Own URL Shortener 2012-09-21

    Most of us are familiar with seeing URLs like bit.ly or t.co on our Twitter or Facebook feeds. These are examples of shortened URLs, which are a short alias or pointer to a longer page link. For example, I can send you the shortened URL http://bit.ly

  • Build Your Own URL Shortener 2009-08-05

    URL shorteners are all the rage-tinyurl.com, tr.im, ow.ly, bit.ly - even Digg are getting in on the action. But wouldn't it be cool to write your own? It turns out that doing so is not so hard. We've just published Kay Smoljak's latest article Get Sh

  • Is there an Elegant solution to rewrite long image?query-urls into cachable_nice_urls, using only apache htaccess? 2010-12-06

    this is a pain in the ass-problem! Whicever genius solves this riddle will receive paypal donation to eat diner for two. Now lets get started. Given: most proxies, do not cache resources with a "?" in their URL even if a Cache-control: public he

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.562 (s). 13 q(s)