Class: Soren::Parsers::Response::StatusLine

Inherits:
Object
  • Object
show all
Defined in:
lib/soren/parsers/response/status_line.rb

Instance Method Summary collapse

Constructor Details

#initialize(status_line) ⇒ StatusLine

: (untyped) -> void



9
10
11
# File 'lib/soren/parsers/response/status_line.rb', line 9

def initialize(status_line)
  @status_line = status_line #: untyped
end

Instance Method Details

#parseObject

: -> Hash[Symbol, untyped]



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/soren/parsers/response/status_line.rb', line 14

def parse
  status_line_match = @status_line&.match(%r{\A(HTTP/\d+\.\d+)\s+(\d{3})(?:\s+(.*))?\z})
  unless status_line_match
    raise Soren::Error::ParseError, 'invalid HTTP status line'
  end

  version = status_line_match[1]
  status_text = status_line_match[2]
  message = (status_line_match[3] || '').strip

  if version.blank? || status_text.blank?
    raise Soren::Error::ParseError, 'status line must include version and code'
  end

  {
    version: version,
    code:    Integer(status_text),
    message: message,
  }
end