class Rackables::DefaultCharset

Adds the specified charset to the Content-Type header, if one isn’t already there.

Ideal for use with Sinatra, which by default doesn’t set charset

Constants

HAS_CHARSET

Public Class Methods

new(app, value = 'utf-8') click to toggle source
# File lib/rackables/default_charset.rb, line 11
def initialize(app, value = 'utf-8')
  @app = app
  @value = value
end

Public Instance Methods

call(env) click to toggle source
# File lib/rackables/default_charset.rb, line 16
def call(env)
  status, headers, body = @app.call(env)
  headers = ::Rack::Utils::HeaderHash.new(headers)
  content_type = headers['Content-Type']
  if content_type && content_type !~ HAS_CHARSET
    headers['Content-Type'] = "#{content_type}; charset=#{@value}"
  end
  [status, headers, body]
end