Building querystrings with ruby

N

ngw

Hi *, my question is really simple: is there a way in Ruby stdlib or in
external modules to build querystrings without creating the string from
scratch ?
For example something like:

name = "Foo Bar"
p URI::join(base_uri, query_string(name))

-> "http://foobar.org/?name=Foo+Bar"

Obviously this code is totally invented.
If not (haven't found anything in the documentation) how does people
accomplish this task ?

TIA,
ngw
 
A

ara.t.howard

Hi *, my question is really simple: is there a way in Ruby stdlib or in
external modules to build querystrings without creating the string from
scratch ?
For example something like:

name = "Foo Bar"
p URI::join(base_uri, query_string(name))

-> "http://foobar.org/?name=Foo+Bar"

Obviously this code is totally invented.
If not (haven't found anything in the documentation) how does people
accomplish this task ?

TIA,
ngw

harp:~ > cat a.rb
require "cgi"
class Hash
def query_string
"?" << inject([]){|a,kv| a << [CGI.escape(kv.shift), CGI.escape(kv.shift)].join("=") }.join("&")
end
end

q = { "name" => "Foo Bar", "uri" => "http://ruby-lang.org" }

p q.query_string

harp:~ > ruby a.rb
"?name=Foo+Bar&uri=http%3A%2F%2Fruby-lang.org"

regards.

-a
 
P

Paul Battley

require "cgi"
class Hash
def query_string
"?" << inject([]){|a,kv| a << [CGI.escape(kv.shift), CGI.escape(kv=
shift)].join("=3D") }.join("&")

Now here's somewhere I can use the handy trick I learned yesterday:

"?" << inject([]){ |a,(k,v)| a << [CGI.escape(k),
CGI.escape(v)].join("=3D") }.join("&")

But this is shorter:

"?" << inject([]){ |a,kv| a << kv.map{ |e| CGI.escape(e) }.join("=3D") }.jo=
in("&")

And this is shorter still:

"?" << to_hash.map{ |kv| kv.map{ |e| CGI.escape(e) }.join("=3D") }.join("&"=
)

Paul.
 
P

Paul Battley

And this is shorter still:

"?" << to_hash.map{ |kv| kv.map{ |e| CGI.escape(e) }.join("=3D") }.join("=
&")

Ignore my stupidity above; this is shorter and less redundant:

"?" << map{ |kv| kv.map{ |e| CGI.escape(e) }.join("=3D") }.join("&")

Paul.
 
N

ngw

-Paul Battley said:
Ignore my stupidity above; this is shorter and less redundant:

"?" << map{ |kv| kv.map{ |e| CGI.escape(e) }.join("=") }.join("&")

Wow !
Thank you both, great snippets.

ngw
 
E

ebeard

Wow, I have no idea what this does or how it works. Would you mind
taking this apart and describing how it works?
 
P

Paul Battley

Wow, I have no idea what this does or how it works. Would you mind
taking this apart and describing how it works?

"?"<<map{|h|h.map{|e|CGI.escape(e)}*'=3D'}*'&'

This can be spaced out a bit:

"?" << # append what follows to the string '?'
map { |h| # h looks like ['key', 'value'] for each key-value pa=
ir
h.map { |e| # e looks like 'key' or 'value'
CGI.escape(e) # escape each element
} * '=3D' # join key and value with '=3D'
} * '&' # join each key-value pair with '&'

Is that any clearer?

Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top