Sha256: 67e11751b7139aca738fef5de96bf85e1cdae370a49824f586d9b7010942e987

Contents?: true

Size: 1.3 KB

Versions: 29

Compression:

Stored size: 1.3 KB

Contents

# -*- coding: utf-8 -*-
# 出力変換
module Jpmobile
  module Rack
    class Filter
      def initialize(app)
        @app = app
      end

      def call(env)
        # 入力を保存
        mobile = env['rack.jpmobile']

        status, env, response = @app.call(env)

        if mobile and env['Content-Type'] =~ %r!text/html|application/xhtml\+xml!
          type, charset = env['Content-Type'].split(/;\s*charset=/)

          body = response_to_body(response)
          body = body.gsub(/<input name="utf8" type="hidden" value="#{[0x2713].pack("U")}"[^>]*?>/, ' ')
          body = body.gsub(/<input name="utf8" type="hidden" value="&#x2713;"[^>]*?>/, ' ')

          response, charset = mobile.to_external(body, type, charset)

          if type and charset
            env['Content-Type'] = "#{type}; charset=#{charset}"
          end
        end

        new_response = ::Rack::Response.new(response, status, env)
        new_response.finish
      end

      private
      def response_to_body(response)
        if response.respond_to?(:to_str)
          response.to_str
        elsif response.respond_to?(:each)
          body = []
          response.each do |part|
            body << response_to_body(part)
          end
          body.join("\n")
        else
          body
        end
      end
    end
  end
end

Version data entries

29 entries across 21 versions & 1 rubygems

Version Path
jpmobile-2.0.1 lib/jpmobile/rack/filter.rb
jpmobile-1.0.5 lib/jpmobile/rack/filter.rb
jpmobile-2.0.0 lib/jpmobile/rack/filter.rb
jpmobile-1.0.4 lib/jpmobile/rack/filter.rb
jpmobile-1.0.3 lib/jpmobile/rack/filter.rb
jpmobile-1.0.2 lib/jpmobile/rack/filter.rb
jpmobile-1.0.1 lib/jpmobile/rack/filter.rb
jpmobile-2.0.0.pre.2 lib/jpmobile/rack/filter.rb
jpmobile-2.0.0.pre.1 lib/jpmobile/rack/filter.rb
jpmobile-1.0.0 lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.7 lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.6 lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.5 lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.4 test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.4 lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.3 lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.3 test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.2 lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.2 test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/rack/filter.rb
jpmobile-1.0.0.pre.1 test/rails/rails_root/vendor/plugins/jpmobile/lib/jpmobile/rack/filter.rb