首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替代避免Lint/ URI.escape逃逸警告的URI.escape?

替代避免Lint/ URI.escape逃逸警告的URI.escape?
EN

Stack Overflow用户
提问于 2018-10-17 16:57:19
回答 1查看 1.5K关注 0票数 4

不知道如何修正Rubocop的 林特/UriEscapeUnescape逃亡警告

尝试用URI替换CGI,认为这是“下降”替换,但这破坏了测试套件。

下面是使用URI的代码行后面的错误:

代码语言:javascript
复制
app/models/media_file.rb:76:5: W: Lint/UriEscapeUnescape: URI.escape method is obsolete and should not be used. Instead, use CGI.escape, URI.encode_www_form or URI.encode_www_form_component depending on your specific use case.
    URI ...
    ^^^

    # app/models/media_file.rb
    ...
    def cdn_url(format: nil)
    if format.nil?
      "#{s3_config.cloudfront_endpoint}/#{escape_url(key)}"
    elsif converted_urls.with_indifferent_access[format.to_s]
      filename = converted_urls.with_indifferent_access[format.to_s]
      if URI.parse(escape_url(filename)).host
        filename
      else
        "#{s3_config.cloudfront_endpoint}/#{escape_url(filename)}"
      end
    else
      converted(url)
    end
  end
...
  private

  def escape_url(url)
    URI
      .escape(url)
      .gsub(/\(/, '%28')
      .gsub(/\)/, '%29')
      .gsub(/\[/, '%5B')
      .gsub(/\]/, '%5D')
  end

编辑:添加用URICGI转义的字符串的示例输出

代码语言:javascript
复制
            url: images/medium/test-image.jpg
URI.escape(url): images/medium/test-image.jpg
CGI.escape(url): images%2Fmedium%2Ftest-image.jpg

            url: images/medium/test-image.jpg
URI.escape(url): images/medium/test-image.jpg
CGI.escape(url): images%2Fmedium%2Ftest-image.jpg

CGI似乎并不是URI的替代品,因为清单错误可能会让您相信。有什么想法?

EN

回答 1

Stack Overflow用户

发布于 2020-06-02 18:22:46

遇到同样的问题,使用可寻址库进行修复。

代码语言:javascript
复制
escaped_query = URI.escape(search,
            Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

#W: Lint/UriEscapeUnescape: URI.escape method is obsolete and should not be used. Instead, use CGI.escape, URI.encode_www_form or URI.encode_www_form_component depending on your specific use case.

通过下列方式解决:

  • 宝石addressable宝石在gemspecGemfile中。
代码语言:javascript
复制
gem 'addressable', '~> 2.7'
  • 需要addressable/uri
  • 添加适当的。
代码语言:javascript
复制
escaped_query = Addressable::URI.encode_component(search, Addressable::URI::CharacterClasses::QUERY)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52860045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档