class Olelo::Middleware::ForceEncoding

Public Class Methods

new(app, encoding = Encoding.default_external) click to toggle source
# File lib/olelo/middleware/force_encoding.rb, line 4
def initialize(app, encoding = Encoding.default_external)
  @app, @encoding = app, encoding
end

Public Instance Methods

call(env) click to toggle source
# File lib/olelo/middleware/force_encoding.rb, line 8
def call(env)
  request = Rack::Request.new(env)
  encode(env)
  encode(request.params)
  @app.call(env)
end

Private Instance Methods

encode(x) click to toggle source
# File lib/olelo/middleware/force_encoding.rb, line 17
def encode(x)
  case x
  when Hash
    y = x.frozen? ? {} : x
    x.each { |k,v| y[k] = encode(v) }
    y
  when Array
    y = x.frozen? ? [] : x
    x.each_with_index {|v,i| y[i] = encode(v) }
    y
  when String
    # Try to force encoding and revert to old encoding if this doesn't work
    if x.encoding != @encoding
      x = x.dup if x.frozen?
      x.try_encoding(@encoding)
    else
    x
    end
  else
    x
  end
end