LoginSignup
72
76

More than 5 years have passed since last update.

Rubyで日本語のURI(URL)をパーズするなら Addressable::URI を使おう

Posted at

Rubyの標準URIクラスでは日本語を含むURIをparseすることはできません(どのRFCを対応しているかの関係らしいです)。
なので解決方法としては Addressable::URI というgemがあるのでそれを使用することで日本語URIに対応することができます。

ライブラリURL
https://github.com/sporkmonger/addressable

# Gemfile
gem 'addressable', require: 'addressable/uri'

# bundle 実行...

# rails console にて確認

URI.parse('http://ja.wikipedia.org/wiki/日本語')
#=> URI::InvalidURIError: bad URI(is not URI?): http://ja.wikipedia.org/wiki/日本語

Addressable::URI.parse('http://ja.wikipedia.org/wiki/日本語')
#=> #<Addressable::URI:0x3fc05653a740 URI:http://ja.wikipedia.org/wiki/日本語>

■ 参考
本家
https://github.com/sporkmonger/addressable
情報
http://d.hatena.ne.jp/kitamomonga/20100316/ruby_gem_addressable_howto
http://memo.yomukaku.net/entries/220

72
76
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
72
76