RuboCopに怒られました。Lint/UriEscapeUnescape
URI.escape
(URI.unescape
) はURIに関してRFCに準拠していない動作をしており、使用非推奨/廃止されることにになってる。ついては CGI.escape
か URL.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"
リンク集