1

I'm sending my rails API requests from an Android app using Volley. This is the search method in question that takes a query string for params (and utf8).

def index
  @items = Item.search(params)
end

My request url looks like this:

"https://www.myapp.com/api/items.json/?utf8=✓&query=" + query;
// query is a string from user input

I'd like to split query into a query string, as currently query is just a string.

I was hoping I could do something like:

def index
  my_params =   # turn query into query string, make new hash with results and utf8 param
  @items = Item.search(my_params)
end

How do I accomplish that? Alternatively, is there a good way to split query up before it's sent from the client side? In this case, query could contain any number of words.

UPDATE

To be clear, I'm asking how to turn the existing param query (which looks like: "large red balloons"), into a proper query string that I can pass to my method.

What my method is expecting in the params would be like:

utf8=✓&query=large+red+balloons
settheline
  • 3,333
  • 8
  • 33
  • 65
  • The above should still be valid: http://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20. However you can get this format as per answer I've posted – BroiSatse Aug 15 '14 at 23:58
  • Have you seen `GCI.escape`? – sethvargo Aug 16 '14 at 23:09
  • @sethvargo, no I hadn't (just to clarify, you did mean `CGI`, not `GCI` right?). That looks like it would work though. I'm not sure how I could be more clear than my last update on the question, which seemed to be clear to you at least judging from your suggestion (though you marked it specifically as unclear). (-; – settheline Aug 17 '14 at 19:03

3 Answers3

1

On the server side, you can use the ruby string method gsub on your user's entered query. The general format as outlined in Ruby-Doc:

your_string.gsub(pattern, replacement) 
#=> a modified version of your_string in which every previous occurrence
#=> of pattern has been "overwritten" by the replacement.

Assuming that your user query is params[:query] = "big red balloon" (since it is a parameter and you are referencing as query above), then you can do

query = params[:query].gsub(' ', '+') 
#=> "big+red+balloon"
"https://www.myapp.com/api/items.json/?utf8=✓&query=" + query
#=> "https://www.myapp.com/api/items.json/?utf8=✓&query=big+red+balloon"

If you are worried about the utf-ness of your query string, you can just replace the spaces in the user's query with %20s:

query = params[:query].gsub(' ','%20')
#=> "big%20red%20balloon"

but according to the post reference provided by @BroiSatse this shouldn't be a problem.

dave_slash_null
  • 1,124
  • 7
  • 16
0

You can do:

"https://www.myapp.com/api/items.json/?utf8=✓&#{URI.encode_www_form(query: query)}"
BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • I'm trying your suggestion for `URI.encode`. This answer won't work though. The request is originating from an Android client, so ruby code is no good on the client side. – settheline Aug 16 '14 at 00:10
0

I figured out a solution on the client side, which I think is probably a better practice than handling this on the Rails server side. Someone who knows more than I do can comment on that if they like, cause I'm no expert. I'll post my solution and leave this answer open to others who have might have a better solution than mine.

String newQueryString = "";
        String queryArr[] = query.split(" ");
        for (int i = 0; i < queryArr.length; i++) {
            newQueryString += queryArr[i];

            // new for loop to add '+' because I dont want a '+' at the end of the new query string
            for (int k = 0; k < queryArr.length - 1; k++) {
                newQueryString += "+";
            }
        }

When query is "big red balloon" this returns:

"https://myapp.com/api/items.json/?utf8=✓&query=big+red+balloon

Again, I'd love to hear more/better solutions so I'll leave open.

settheline
  • 3,333
  • 8
  • 33
  • 65