Ruby中从URL中删除参数的最优雅方式是什么?

时间:2010-01-27 19:15:01

标签: ruby url parsing

我想从URL中取出一个参数,而不知道它是哪个参数,然后重新组合URL。

我想使用CGI或URI自己写一些东西并不难,但我想这样的功能已经存在。有什么建议吗?

在:

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

输出:

http://example.com/path?param2=2&param3=something3

7 个答案:

答案 0 :(得分:33)

我更喜欢使用:

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"

答案 1 :(得分:26)

也许有点偏离主题,但对于那些试图在rails应用程序环境中执行此操作的人,您可以这样做:

url_for(params.except(:name_of_param_to_delete))

N.B。测试了rails v2.3.9。

答案 2 :(得分:15)

如果你不想包含额外的宝石,如果你不想要讨厌的正则表达式,这是我的首选方式:

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 = CGI.parse(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"

答案 3 :(得分:7)

可寻址的 gem可以很好地完成这项工作;请看 The Tin Man 的优秀答案。但如果你想自己动手,那就是这样的。这个代码唯一的优点就是它隐藏了一个方法中的丑陋:

#!/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"

答案 4 :(得分:5)

我想出了类似的东西

 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(/&amp;/).size > 0
   new_params = uri_parsed.query.gsub(/&amp;/, '&').split('&').reject { |q| params.include?(q.split('=').first) }
   uri = uri.split('?').first
   amp = escaped ? '&amp;' : '&'
   "#{uri}?#{new_params.join(amp)}"
 end

答案 5 :(得分:3)

一行应该足够了:

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

答案 6 :(得分:0)

如果您的网址是字符串,那么这里是干净的

  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

注意:您可以通过以下操作使用此命令删除具有特定值的参数:

def remove_param(url, param_value)

next if param[1] == param_value