Class: HttpMimic::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/http_mimic/response_parser.rb

Constant Summary collapse

HTTP_STATUS_LINE_REGEX =
/\AHTTP\/(?<version>[\d\.]+)\s+(?<code>\d{3})(?:\s+(?<message>.*))?/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_output, exit_status: nil, stderr: nil, command: nil, request_url: nil) ⇒ ResponseParser

Returns a new instance of ResponseParser.



11
12
13
14
15
16
17
# File 'lib/http_mimic/response_parser.rb', line 11

def initialize(raw_output, exit_status: nil, stderr: nil, command: nil, request_url: nil)
  @raw_output   = raw_output.to_s
  @exit_status  = exit_status
  @stderr       = stderr.to_s
  @command      = command
  @request_url  = request_url
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



9
10
11
# File 'lib/http_mimic/response_parser.rb', line 9

def command
  @command
end

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



9
10
11
# File 'lib/http_mimic/response_parser.rb', line 9

def exit_status
  @exit_status
end

#raw_outputObject (readonly)

Returns the value of attribute raw_output.



9
10
11
# File 'lib/http_mimic/response_parser.rb', line 9

def raw_output
  @raw_output
end

#request_urlObject (readonly)

Returns the value of attribute request_url.



9
10
11
# File 'lib/http_mimic/response_parser.rb', line 9

def request_url
  @request_url
end

#stderrObject (readonly)

Returns the value of attribute stderr.



9
10
11
# File 'lib/http_mimic/response_parser.rb', line 9

def stderr
  @stderr
end

Instance Method Details

#parseObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/http_mimic/response_parser.rb', line 19

def parse
  # Split header blocks and body
  header_blocks, body = split_headers_and_body(@raw_output)

  final_header_block = header_blocks.last || ''
  history_header_blocks = header_blocks.size > 1 ? header_blocks[0...-1] : []

  # Parse final status line and headers
  code, http_version, status_message, headers, cookies = parse_header_block(final_header_block)

  # Parse redirect history
  history = history_header_blocks.map do |block|
    h_code, h_version, h_msg, h_headers, h_cookies = parse_header_block(block)
    {
      code: h_code,
      http_version: h_version,
      status_message: h_msg,
      headers: h_headers,
      cookies: h_cookies,
      raw_headers: block
    }
  end

  # Parse body (auto-detect JSON)
  parsed_body = parse_body(body, headers)

  Response.new(
    code: code,
    http_version: http_version,
    status_message: status_message,
    headers: headers,
    cookies: cookies,
    body: body,
    parsed_response: parsed_body,
    raw_headers: final_header_block,
    history: history,
    request_url: request_url,
    stderr: stderr,
    exit_code: exit_status ? exit_status.exitstatus : 0,
    command: command
  )
end