Module: Jets::Controller::Response::Compat::Response

Extended by:
ActiveSupport::Concern
Included in:
Jets::Controller::Response
Defined in:
lib/jets/controller/response/compat/response.rb

Defined Under Namespace

Classes: ContentTypeHeader

Constant Summary collapse

NullContentTypeHeader =
ContentTypeHeader.new nil, nil
CONTENT_TYPE =
"Content-Type"
CONTENT_TYPE_PARSER =
/
  \A
  (?<mime_type>[^;\s]+\s*(?:;\s*(?:(?!charset)[^;\s])+)*)?
  (?:;\s*charset=(?<quote>"?)(?<charset>[^;\s]+)\k<quote>)?
/x

Instance Method Summary collapse

Instance Method Details

#charsetObject

The charset of the response. HTML wants to know the encoding of the content you’re giving them, so we need to send that along.



62
63
64
65
# File 'lib/jets/controller/response/compat/response.rb', line 62

def charset
  header_info = parsed_content_type_header
  header_info.charset || self.class.default_charset
end

#charset=(charset) ⇒ Object

Sets the HTTP character set. In case of nil parameter it sets the charset to default_charset.

response.charset = 'utf-16' # => 'utf-16'
response.charset = nil      # => 'utf-8'


51
52
53
54
55
56
57
58
# File 'lib/jets/controller/response/compat/response.rb', line 51

def charset=(charset)
  content_type = parsed_content_type_header.mime_type
  if false == charset
    set_content_type content_type, nil
  else
    set_content_type content_type, charset || self.class.default_charset
  end
end

#parse_content_type(content_type) ⇒ Object

:nodoc:



26
27
28
29
30
31
32
# File 'lib/jets/controller/response/compat/response.rb', line 26

def parse_content_type(content_type)
  if content_type && match = CONTENT_TYPE_PARSER.match(content_type)
    ContentTypeHeader.new(match[:mime_type], match[:charset])
  else
    NullContentTypeHeader
  end
end

#parsed_content_type_headerObject

Small internal convenience method to get the parsed version of the current content type header.



36
37
38
# File 'lib/jets/controller/response/compat/response.rb', line 36

def parsed_content_type_header
  parse_content_type(get_header("Content-Type"))
end

#set_content_type(content_type, charset) ⇒ Object



40
41
42
43
44
# File 'lib/jets/controller/response/compat/response.rb', line 40

def set_content_type(content_type, charset)
  type = content_type || ""
  type = "#{type}; charset=#{charset.to_s.downcase}" if charset
  set_header CONTENT_TYPE, type
end