Module: Philiprehberger::HeaderKit::ContentType

Defined in:
lib/philiprehberger/header_kit/content_type.rb

Overview

Parses Content-Type header values into media type, charset, and boundary components.

Class Method Summary collapse

Class Method Details

.parse(header) ⇒ Hash

Parse a Content-Type header string.

Parameters:

  • header (String)

    the Content-Type header value

Returns:

  • (Hash)

    with :media_type, :charset, :boundary keys



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/philiprehberger/header_kit/content_type.rb', line 11

def self.parse(header)
  result = { media_type: nil, charset: nil, boundary: nil }
  return result if header.nil? || header.strip.empty?

  parts = header.split(';').map(&:strip)
  result[:media_type] = parts.shift&.downcase

  parts.each do |param|
    key, value = param.split('=', 2).map(&:strip)
    next if key.nil? || key.empty?

    value = unquote(value) if value

    case key.downcase
    when 'charset'
      result[:charset] = value
    when 'boundary'
      result[:boundary] = value
    end
  end

  result
end