OSDN Git Service

refs #1607 携帯/スマホ: 特集ページにアクセスすると例外が発生する不具合を修正
authorYOSHIDA Hiroki <hyoshida@appirits.com>
Wed, 29 May 2013 07:56:37 +0000 (16:56 +0900)
committerYOSHIDA Hiroki <hyoshida@appirits.com>
Wed, 29 May 2013 08:39:22 +0000 (17:39 +0900)
config/initializers/jpmobile_override.rb

index 08c4bcc..063d859 100644 (file)
@@ -65,17 +65,14 @@ module Jpmobile
           when nil
             # 変換先が定義されていない。
             match
-          end
+          end.force_encoding(str.encoding) # 結合先の文字コードに合わせる
         end
       end
 
       # FIXME: unicodecr_to_external メソッドのオーバライドが何故必要か不明なため、
       #        なるべくもとのコードを弄らないよう暫定手段で文字コード問題を回避
       def unicodecr_to_external_with_force_encoding(str, *args)
-        return unicodecr_to_external_without_force_encoding(str, *args) if str.encoding == Encoding.default_external
-        encoding_handler(str) do
-          unicodecr_to_external_without_force_encoding(str, *args)
-        end
+        unicodecr_to_external_without_force_encoding(str.force_encoding(Encoding::ASCII_8BIT), *args)
       end
 
       alias_method_chain :unicodecr_to_external, :force_encoding
@@ -84,24 +81,13 @@ module Jpmobile
       # 理由: 携帯のユーザエージェント判別時に正規表現でSJIS文字列とUnicode文字列の比較をして例外発生するため
       %w( docomo au vodafone jphone ).each do |name|
         define_method "external_to_unicodecr_#{name}_with_force_encoding" do |str|
-          encoding_handler(str, Encoding::Shift_JIS) do
-            self.send "external_to_unicodecr_#{name}_without_force_encoding", str
-          end
+          converted_str = str if str.encoding == Encoding::Shift_JIS
+          converted_str ||= NKF.nkf('-s', str)
+          self.send "external_to_unicodecr_#{name}_without_force_encoding", converted_str
         end
 
         alias_method_chain "external_to_unicodecr_#{name}".to_sym, :force_encoding
       end
-
-      private
-
-      def encoding_handler(text, encoding = Encoding::ASCII_8BIT)
-        raise unless block_given?
-        original_encoding = text.encoding
-        text.force_encoding(encoding)
-        result = yield
-        text.force_encoding(original_encoding)
-        result
-      end
     end
   end
 end