Tuesday, 15 September 2015

ruby - Extract data from URL -


In Ruby, I want to get some data from a URL. I have a URL match:

  "http: // localhost: 3000 / car / test / 1 / direction / 2"  

I would like < Remove the value after Code> / test (here it is 1 ). I know how to remove other values.

I created a URI object:

  uri = URI.parse ("http: // localhost: 3000 / cars / test / 1 / direction / 2") Uri.port = 3000 uri.path = / car / test / 1 / direction / 2 uri.host = local host  

However, I do not know how to extract internal parameters.

Do I have to parse this URL or is there an existing solution?

I think it can work well for you because it recognizes the key name too :

  url_parts = uri.path.scan (/ \ / (\ w +) \ / (\ D +) /) # = & gt; You can also create it by using  hash . : 

  hash [url_parts] # ​​= & gt; {"Test" = & gt; "1", "direction" = & gt; "2"}  

No comments:

Post a Comment