[前][次][番号順一覧][スレッド一覧]

ruby-changes:48118

From: akr <ko1@a...>
Date: Sat, 21 Oct 2017 15:22:57 +0900 (JST)
Subject: [ruby-changes:48118] akr:r60232 (trunk): lib/open-uri.rb: accept :encoding option as well as encoding in mode string.

akr	2017-10-21 15:22:53 +0900 (Sat, 21 Oct 2017)

  New Revision: 60232

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60232

  Log:
    lib/open-uri.rb: accept :encoding option as well as encoding in mode string.

  Modified files:
    trunk/lib/open-uri.rb
    trunk/test/open-uri/test_open-uri.rb
Index: lib/open-uri.rb
===================================================================
--- lib/open-uri.rb	(revision 60231)
+++ lib/open-uri.rb	(revision 60232)
@@ -108,6 +108,7 @@ module OpenURI https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb#L108
     :ssl_verify_mode => nil,
     :ftp_active_mode => false,
     :redirect => true,
+    :encoding => nil,
   }
 
   def OpenURI.check_options(options) # :nodoc:
@@ -141,6 +142,12 @@ module OpenURI https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb#L142
       encoding, = $1,Encoding.find($1) if $1
       mode = nil
     end
+    if options.has_key? :encoding
+      if !encoding.nil?
+        raise ArgumentError, "encoding specified twice"
+      end
+      encoding = Encoding.find(options[:encoding])
+    end
 
     unless mode == nil ||
            mode == 'r' || mode == 'rb' ||
Index: test/open-uri/test_open-uri.rb
===================================================================
--- test/open-uri/test_open-uri.rb	(revision 60231)
+++ test/open-uri/test_open-uri.rb	(revision 60232)
@@ -604,6 +604,24 @@ class TestOpenURI < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/open-uri/test_open-uri.rb#L604
         assert_equal(content_ej, f.read)
         assert_equal("text/plain", f.content_type)
         assert_equal("euc-jp", f.charset)
+        assert_equal(Encoding::EUC_JP, f.read.encoding)
+      }
+      open("#{url}/ej/", 'r:utf-8') {|f|
+        # override charset with encoding option
+        assert_equal(content_ej.dup.force_encoding('utf-8'), f.read)
+        assert_equal("text/plain", f.content_type)
+        assert_equal("euc-jp", f.charset)
+        assert_equal(Encoding::UTF_8, f.read.encoding)
+      }
+      open("#{url}/ej/", :encoding=>'utf-8') {|f|
+        # override charset with encoding option
+        assert_equal(content_ej.dup.force_encoding('utf-8'), f.read)
+        assert_equal("text/plain", f.content_type)
+        assert_equal("euc-jp", f.charset)
+        assert_equal(Encoding::UTF_8, f.read.encoding)
+      }
+      assert_raise(ArgumentError) {
+        open("#{url}/ej/", 'r:utf-8', :encoding=>'utf-8') {|f| }
       }
       open("#{url}/nc/") {|f|
         assert_equal("aa", f.read)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]