LoginSignup
15
10

More than 5 years have passed since last update.

obsolete な URI.escape, URI.unescape を置き換える

Last updated at Posted at 2018-06-19

RuboCopに怒られました。Lint/UriEscapeUnescape

URI.escape(URI.unescape) はURIに関してRFCに準拠していない動作をしており、使用非推奨/廃止されることにになってる。ついては CGI.escapeURL.encode_www_form, URL.encode_www_form_component で書き換えなさい、とのこと。

で、置換しようとしたんだけど機械的にはできない。

# 今までのコード(obsolute)
URI.escape('http://hoge.example.org/search?q=ほげ')
#=> "http://hogehoge.example.org/search?q=%E3%81%BB%E3%81%92"

# '/' などもエスケープされる
CGI.escape('http://hoge.example.org/search?q=ほげ')
#=> "http%3A%2F%2Fhoge.example.org%2Fsearch%3Fq%3D%E3%81%BB%E3%81%92"

# 同様
URI.encode_www_form_component('http://hogehoge.example.org/search?q=ほげ')
#=> "http%3A%2F%2Fhoge.example.org%2Fsearch%3Fq%3D%E3%81%BB%E3%81%92"

# CGI.escape, URI.encode_www_form_component も本来はこういう使い方をする
'http://hoge.example.org/search?' + URI.encode_www_form({q: 'ほげ'})
#=> "http://hoge.example.org/search?q=%E3%81%BB%E3%81%92"

いろいろ書き直すしかないか?と思ってたけど sporkmonger/addressable がやってくれる。

Addressable::URI.encode('http://hoge.example.org/search?q=ほげ')
#=> "http://hoge.example.org/search?q=%E3%81%BB%E3%81%92"

リンク集

15
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
10