Class: Rack::Ketai::Carrier::Abstract::SjisFilter

Inherits:
Filter show all
Defined in:
lib/egalite/keitai/rack/ketai/carrier/abstract.rb

Constant Summary

Constants inherited from Filter

Filter::EMOJI_DATA

Instance Method Summary collapse

Instance Method Details

#inbound(env) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/egalite/keitai/rack/ketai/carrier/abstract.rb', line 81

def inbound(env)
  request = Rack::Request.new(env)

  # 最低でも1回呼んでないと query_string, form_hash等が未設定
  request.params
  
  # 同一オブジェクトが両方に入ってたりして二重にかかることがあるので
  converted_objects = []
  converter = lambda { |value|
    unless converted_objects.include?(value)
      value = NKF.nkf('-m0 -x -Sw', value)
      converted_objects << value
    end
    value
  }
  
  full_apply(request.env["rack.request.query_hash"],
             request.env["rack.request.form_hash"],
             &converter)

  request.env
end

#outbound(status, headers, body) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/egalite/keitai/rack/ketai/carrier/abstract.rb', line 104

def outbound(status, headers, body)
  if body.is_a?(Array)
    body = body.collect do |str|
      NKF.nkf('-m0 -x -Ws', str)
    end
  else
    body = NKF.nkf('-m0 -x -Ws', body)
  end
  
  case headers['Content-Type']
  when /charset=(\w+)/i
    headers['Content-Type'].sub!(/charset=\w+/, 'charset=shift_jis')
  else
    headers['Content-Type'] << "; charset=shift_jis"
  end

  content = (body.is_a?(Array) ? body[0] : body).to_s
  headers['Content-Length'] = (content.respond_to?(:bytesize) ? content.bytesize : content.size).to_s if headers.member?('Content-Length')
  
  [status, headers, body]
end