http_debug_output-parser 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ca64ebb1c13e162e889a0d93e2b04bc3f2b363244599b9b35abf7d7130d1661
4
- data.tar.gz: f4fe194df36fe913ef28e691fa62ff336e84bcdd85e0887e8ceed26ac3637290
3
+ metadata.gz: 063f0170d11d7a6afecffe9865366d314020679cb3f112aaceb928e76a3372d5
4
+ data.tar.gz: 9284c52632ab7981f27f0bf76fea3270e3aa63289059f069410e85e14d0de409
5
5
  SHA512:
6
- metadata.gz: e6eebe21ee196d94f96893ed8448f838688f601a260fcbd7aef1142400659829403e7b10d85487c564b04804b433a99bd83f4ee9f75576472d357bb05b58694e
7
- data.tar.gz: b48684cc04a33dd6c31c9aefe0bb4c03e91432e930f88e06ab13410cd84cd852f412b90c5c84750f1500de8a7656f2e748fd639dfa41f702e5359135e706ce07
6
+ metadata.gz: 5a932934697fe37ddc83275c41a28daa620c3f0acf0f79b398314099b07e9a3b80b6ea3d614308e725a2017b78e2a7a09b917d194a8b91031f626b80eed9b129
7
+ data.tar.gz: 1662a6879e5d92b671b42f576af0de3765fc02408c075637cf9bba4591cc615574400160671f41bc057da73dcea037fcf09da11f223c343233d572a9ebf3e106
@@ -2,6 +2,6 @@
2
2
 
3
3
  module HttpDebugOutput
4
4
  class Parser
5
- VERSION = '1.0.2'
5
+ VERSION = '1.0.3'
6
6
  end
7
7
  end
@@ -5,8 +5,16 @@ require 'json'
5
5
 
6
6
  module HttpDebugOutput
7
7
  class Parser
8
+ ESCAPE_SEQUENCES = {
9
+ '\\\\' => '\\',
10
+ '\\r' => "\r",
11
+ '\\n' => "\n",
12
+ '\\t' => "\t",
13
+ '\\"' => '"'
14
+ }.freeze
15
+
8
16
  def initialize(debug_output)
9
- @debug_output = debug_output.split("\n")
17
+ @debug_output = normalize(debug_output).split("\n")
10
18
  end
11
19
 
12
20
  def call
@@ -18,40 +26,83 @@ module HttpDebugOutput
18
26
 
19
27
  private
20
28
 
21
- def parse_request # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
22
- request_lines = @debug_output[find_range_for_start_and_end_strings('<- ', '-> ', last_occurence_of_end: false)]
23
- .map { |line| clean_line(line.gsub('<- ', '')) }
24
- method, path, protocol = request_lines.first.split
25
- headers = request_lines[1..].take_while { |line| line != '' }
26
- payload = request_lines[headers.count + 1..].join
29
+ def parse_request
30
+ lines = request_lines
31
+ method, path, protocol = lines.first.split
32
+ headers = headers_from(lines) { |line| line == '' }
27
33
 
28
34
  {
29
35
  method:,
30
36
  path:,
31
37
  protocol:,
32
38
  headers:,
33
- payload: payload.empty? ? nil : JSON.parse(payload)
39
+ payload: parse_payload(lines[headers.count + 1..])
34
40
  }
35
41
  end
36
42
 
37
- def parse_response # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
38
- response_lines = @debug_output[find_range_for_start_and_end_strings('-> ', 'read ')]
39
- .map { |line| clean_line(line.gsub('-> ', '')) }
40
- .filter { |line| line != '' }
41
- protocol, status, *message = response_lines.first.split
42
- headers = response_lines[1..].take_while { |line| !line.start_with?('reading') }
43
- payload_end_index = response_lines.rindex { |line| line.start_with?('read ') } || response_lines.count
44
- payload = response_lines[headers.count + 2..payload_end_index - 1].join
43
+ def parse_response
44
+ lines = response_lines
45
+ protocol, status, *message = lines.first.split
46
+ headers = headers_from(lines) { |line| line.start_with?('reading') }
45
47
 
46
48
  {
47
49
  protocol:,
48
50
  status:,
49
51
  message: message.join(' '),
50
52
  headers:,
51
- payload: payload.empty? ? nil : JSON.parse(payload)
53
+ payload: parse_payload(lines[headers.count + 2..payload_end_index(lines) - 1])
52
54
  }
53
55
  end
54
56
 
57
+ def request_lines
58
+ @debug_output[find_range_for_start_and_end_strings('<- ', '-> ', last_occurence_of_end: false)]
59
+ .map { |line| clean_line(line.gsub('<- ', '')) }
60
+ end
61
+
62
+ def response_lines
63
+ @debug_output[find_range_for_start_and_end_strings('-> ', 'read ')]
64
+ .map { |line| clean_line(line.gsub('-> ', '')) }
65
+ .filter { |line| line != '' }
66
+ end
67
+
68
+ def headers_from(lines)
69
+ lines[1..].take_while { |line| !yield(line) }
70
+ end
71
+
72
+ def payload_end_index(lines)
73
+ lines.rindex { |line| line.start_with?('read ') } || lines.count
74
+ end
75
+
76
+ def parse_payload(lines)
77
+ payload = lines.join
78
+ payload.empty? ? nil : JSON.parse(payload)
79
+ end
80
+
81
+ def normalize(debug_output)
82
+ return debug_output unless escaped?(debug_output)
83
+
84
+ lines = debug_output.split("\n").reject { |line| line.strip.empty? }
85
+ join_markers(lines).map { |line| unescape(line) }.join("\n")
86
+ end
87
+
88
+ def escaped?(debug_output)
89
+ !debug_output.include?("\r") && debug_output.include?('\r\n')
90
+ end
91
+
92
+ def join_markers(lines)
93
+ lines.each_with_object([]) do |line, joined|
94
+ if joined.last&.match?(/\A(?:<-|->)\s*\z/)
95
+ joined[-1] = "#{joined.last.strip} #{line}"
96
+ else
97
+ joined << line
98
+ end
99
+ end
100
+ end
101
+
102
+ def unescape(line)
103
+ line.gsub(/\\(?:\\|r|n|t|")/) { |sequence| ESCAPE_SEQUENCES[sequence] }
104
+ end
105
+
55
106
  def find_range_for_start_and_end_strings(start_index_string, end_index_string, last_occurence_of_end: true)
56
107
  start_index = @debug_output.find_index { |line| line.start_with?(start_index_string) }
57
108
  end_index_method = last_occurence_of_end ? :rindex : :find_index
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_debug_output-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Baćkowski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-02-22 00:00:00.000000000 Z
11
+ date: 2026-08-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A gem that parses debug output from Ruby's Net::HTTP.
14
14
  email:
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  - !ruby/object:Gem::Version
48
48
  version: '0'
49
49
  requirements: []
50
- rubygems_version: 3.4.21
50
+ rubygems_version: 3.4.19
51
51
  signing_key:
52
52
  specification_version: 4
53
53
  summary: Ruby's Net::HTTP debug output parser