3

In Ruby 1.8, using the URI standard library, I can parse

http://au.easyroommate.com/content/common/listing_detail.aspx?code=H123456789012&from=L123456789012345

using URI.split to get

["http", nil, "au.easyroommate.com", nil, nil,
"/content/common/listing_detail.aspx", nil, 
"code=H123456789012&from=L123456789012345", nil]

But is it possible to get the H123456789012 bit from the query portion without using my own hackery (eg splitting by & and then getting the bit that matches /code.(.*)/ ?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338

2 Answers2

3

You're looking for the CGI::parse method

params = CGI::parse("query_string")
  # {"name1" => ["value1", "value2", ...],
  #  "name2" => ["value1", "value2", ...], ... }
Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
3

You could use Rack::Utils which has a method called parse_nested_query which you could pass in the query string from the URL:

Rack::Utils.parse_nested_query(uri.query_string)

This will then return a Hash object representing the query string allowing you to gain access to the code.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261