class Jpmobile::Rack::Filter

Public Class Methods

new(app) click to toggle source
# File lib/jpmobile/rack/filter.rb, line 7
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/jpmobile/rack/filter.rb, line 11
def call(env)
  # 入力を保存
  mobile = env['rack.jpmobile']

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

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

      body = response_to_body(response)
      body = body.gsub(%r<input name="utf8" type="hidden" value="#{[0x2713].pack("U")}"[^>]*?>/, ' ')
      body = body.gsub(%r<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
    elsif Jpmobile::Emoticon.pc_emoticon?
      body = response_to_body(response)

      response = Jpmobile::Emoticon.emoticons_to_image(body)
    end
  end

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