Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 411 Vote(s) - 3.39 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the most elegant way in Ruby to remove a parameter from a URL?

#1
I would like to take out a parameter from a URL by its name without knowing which parameter it is, and reassemble the URL again.

I guess it is not that hard to write something on my own using CGI or URI, but I imagine such functionality exists already. Any suggestions?

In:

[To see links please register here]


Out:

[To see links please register here]

Reply

#2
Here's a clean one if your url is a string:
```
def remove_param(url, param_name)
uri = URI(url)
params = []
URI.decode_www_form(uri.query || '').each do |param|
next if param[0] == param_name

params << param
end
uri.query = URI.encode_www_form(params)
uri.to_s
end
```

note: you could use this to remove a param with a specific value by changing:
```
def remove_param(url, param_value)
```
and
```
next if param[1] == param_value
```
Reply

#3
If you don't want to include an extra Gem and if you don't want nasty Regex, here's my prefered way:

require 'cgi'
require 'uri'

url = "http://example.com/path?param1=one&param2=2&param3=something3"

uri = URI(url) #=> #<URI::HTTP:0x007fbe25141a78 URL:http://example.com/path?param1=one&param2=2&param3=something3>
params = Rack::Utils.parse_nested_query(uri.query || "") #=> {"param1"=>["one"], "param2"=>["2"], "param3"=>["something3"]}
params.delete('param1') #=> ["one"]
uri.query = URI.encode_www_form(params) #=> "param2=2&param3=something3"

uri.to_s #=> "http://example.com/path?param2=2&param3=something3"

**Note,** choose wisely:

CGI.parse('a=b&a=c') #=> {"a"=>["b", "c"]}
Rack::Utils.parse_nested_query('a=b&a=c') #=> {"a"=>"c"}

And:

URI.encode_www_form({ a: :b, c: { d: 2 }}) #=> "a=b&c=%7B%3Ad%3D%3E2%7D"
{ a: :b, c: { d: 2 }}.to_query #=> "a=b&c%5Bd%5D=2"
Reply

#4
The _addressable_ gem will do this nicely; please see the superior answer by _The Tin Man_. But if you want to roll your own, here's how. The only claim this code has to elegance is that it hides the ugly in a method:

#!/usr/bin/ruby1.8

def reject_param(url, param_to_reject)
# Regex from RFC3986
url_regex = %r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$"
raise "Not a url: #{url}" unless url =~ url_regex
scheme_plus_punctuation = $1
authority_with_punctuation = $3
path = $5
query = $7
fragment = $9
query = query.split('&').reject do |param|
param_name = param.split(/[=;]/).first
param_name == param_to_reject
end.join('&')
[scheme_plus_punctuation, authority_with_punctuation, path, '?', query, fragment].join
end

url = "http://example.com/path?param1=one&param2=2&param3=something3"
p url
p reject_param(url, 'param2')

# => "http://example.com/path?param1=one&param2=2&param3=something3"
# => "http://example.com/path?param1=one&param3=something3"

Reply

#5
One line should be enough:

url.sub(/\?param_to_remove=[^&]*/, '?').sub(/\&param_to_remove=[^&]*/, '').sub(/\?$/,'')
Reply

#6
I prefer to use:

require 'addressable/uri'

uri = Addressable::URI.parse('http://example.com/path?param1=one&param2=2&param3=something3')

params = uri.query_values #=> {"param1"=>"one", "param2"=>"2", "param3"=>"something3"}
params.delete('param1') #=> "one"
uri.query_values = params #=> {"param2"=>"2", "param3"=>"something3"}

uri.to_s #=> "http://example.com/path?param2=2&param3=something3"

Reply

#7
Maybe a little off-topic, but for anyone who's attempting to do this in the context of a rails app you can simply do:

url_for(params.except(:name_of_param_to_delete))

N.B. Tested in rails v2.3.9.
Reply

#8
I came up with something like this

def uri_remove_param(uri, params = nil)
return uri unless params
params = Array(params)
uri_parsed = URI.parse(uri)
return uri unless uri_parsed.query
escaped = uri_parsed.query.grep(/&/).size > 0
new_params = uri_parsed.query.gsub(/&/, '&').split('&').reject { |q| params.include?(q.split('=').first) }
uri = uri.split('?').first
amp = escaped ? '&' : '&'
"#{uri}?#{new_params.join(amp)}"
end
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through