http_debug_output-parser 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 21c17e896d8a98fc500c71b6b7a5fcf42328281013e1da0400b76d4b6e5e9e9f
4
+ data.tar.gz: 6ac253909248d0795892fe4c545628697b3b9338153a76f54db37893910c9625
5
+ SHA512:
6
+ metadata.gz: 12987812dcd7db3dd44b05cdd22f3820c5094ed631429797132d94e6f8bc146c3391a42564c3d8ec1f67523f9f58556c7c9c464dc8f77d525e1b0e76cad54a90
7
+ data.tar.gz: 273c6a10ed566070edba9f2e9dd3fcbcceaa38fbc718f974cd2f9aca733bc3ba3e990cfcf08bffea3712dc2ac7b160eb0d7a83b59bc47aab64c8752546a53c93
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.2
4
+ Exclude:
5
+ - 'rails_simple_event_sourcing.gemspec'
6
+ - 'vendor/bundle/**/*'
7
+
8
+ Layout/LineLength:
9
+ Max: 120
10
+ Exclude:
11
+ - spec/**/*
12
+
13
+ Style/Documentation:
14
+ Enabled: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Damian Baćkowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # HttpDebugOutput::Parser ![tests](https://github.com/dbackowski/http_debug_output-parser/actions/workflows/rspec.yml/badge.svg) ![codecheck](https://github.com/dbackowski/http_debug_output-parser/actions/workflows/rubocop.yml/badge.svg)
2
+
3
+ `HttpDebugOutput::Parser` is a Ruby gem designed to parse and transform the debug output from Ruby's Net::HTTP library into a structured, easy-to-read hash. This gem is particularly useful for developers who need to analyze HTTP requests and responses in a more human-readable and programmatically accessible format.
4
+
5
+ ### Features
6
+
7
+ * Parses Debug Output: Converts raw Net::HTTP debug output into a structured Hash.
8
+
9
+ * Request and Response Details: Extracts and organizes details such as HTTP method, path, headers, status codes, and payloads.
10
+
11
+ * Easy Integration: Simple to integrate into existing Ruby projects, making it easier to debug and log HTTP interactions.
12
+
13
+
14
+ ### Example
15
+
16
+ Given the raw debug output from Net::HTTP, the gem transforms it into a structured hash:
17
+
18
+ ```ruby
19
+ {
20
+ :request => {
21
+ :method => "GET",
22
+ :path => "/astros.json",
23
+ :protocol => "HTTP/1.1",
24
+ :headers => [
25
+ "Accept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
26
+ "Accept: */*",
27
+ "User-Agent: Ruby",
28
+ "Connection: close",
29
+ "Host: api.open-notify.org"
30
+ ],
31
+ :payload => nil
32
+ },
33
+ :response => {
34
+ :protocol => "HTTP/1.1",
35
+ :status => "200",
36
+ :message => "OK",
37
+ :headers => [
38
+ "Server: nginx/1.10.3",
39
+ "Date: Thu, 13 Feb 2025 18:58:02 GMT",
40
+ "Content-Type: application/json",
41
+ "Content-Length: 587",
42
+ "Connection: close",
43
+ "access-control-allow-origin: *"
44
+ ],
45
+ :payload => {
46
+ "people" => [
47
+ { "craft" => "ISS", "name" => "Oleg Kononenko" },
48
+ { "craft" => "ISS", "name" => "Nikolai Chub" },
49
+ { "craft" => "ISS", "name" => "Tracy Caldwell Dyson" },
50
+ { "craft" => "ISS", "name" => "Matthew Dominick" },
51
+ { "craft" => "ISS", "name" => "Michael Barratt" },
52
+ { "craft" => "ISS", "name" => "Jeanette Epps" },
53
+ { "craft" => "ISS", "name" => "Alexander Grebenkin" },
54
+ { "craft" => "ISS", "name" => "Butch Wilmore" },
55
+ { "craft" => "ISS", "name" => "Sunita Williams" },
56
+ { "craft" => "Tiangong", "name" => "Li Guangsu" },
57
+ { "craft" => "Tiangong", "name" => "Li Cong" },
58
+ { "craft" => "Tiangong", "name" => "Ye Guangfu" }
59
+ ],
60
+ "number" => 12,
61
+ "message"=>"success"
62
+ }
63
+ }
64
+ }
65
+ ```
66
+
67
+ ## Installation
68
+
69
+ Add this line to your application's Gemfile:
70
+
71
+ ```ruby
72
+ gem 'http_debug_output-parser'
73
+ ```
74
+
75
+ And then execute:
76
+
77
+ ```bash
78
+ $ bundle install
79
+ ```
80
+
81
+ Or install it yourself as:
82
+
83
+ ```bash
84
+ gem install http_debug_output-parser
85
+ ```
86
+
87
+ ## Usage
88
+
89
+ ```ruby
90
+ require 'http_debug_output-parser'
91
+
92
+ debug_output = <<~DEBUG
93
+ opening connection to api.open-notify.org:80...
94
+ opened
95
+ <- "GET /astros.json HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: Ruby\r\nConnection: close\r\nHost: api.open-notify.org\r\n\r\n"
96
+ -> "HTTP/1.1 200 OK\r\n"
97
+ -> "Server: nginx/1.10.3\r\n"
98
+ -> "Date: Thu, 13 Feb 2025 18:58:02 GMT\r\n"
99
+ -> "Content-Type: application/json\r\n"
100
+ -> "Content-Length: 587\r\n"
101
+ -> "Connection: close\r\n"
102
+ -> "access-control-allow-origin: *\r\n"
103
+ -> "\r\n"
104
+ reading 587 bytes...
105
+ -> "{\"people\": [{\"craft\": \"ISS\", \"name\": \"Oleg Kononenko\"}, {\"craft\": \"ISS\", \"name\": \"Nikolai Chub\"}, {\"craft\": \"ISS\", \"name\": \"Tracy Caldwell Dyson\"}, {\"craft\": \"ISS\", \"name\": \"Matthew Dominick\"}, {\"craft\": \"ISS\", \"name\": \"Michael Barratt\"}, {\"craft\": \"ISS\", \"name\": \"Jeanette Epps\"}, {\"craft\": \"ISS\", \"name\": \"Alexander Grebenkin\"}, {\"craft\": \"ISS\", \"name\": \"Butch Wilmore\"}, {\"craft\": \"ISS\", \"name\": \"Sunita Williams\"}, {\"craft\": \"Tiangong\", \"name\": \"Li Guangsu\"}, {\"craft\": \"Tiangong\", \"name\": \"Li Cong\"}, {\"craft\": \"Tiangong\", \"name\": \"Ye Guangfu\"}], \"number\": 12, \"message\": \"success\"}"
106
+ read 587 bytes
107
+ Conn close
108
+ DEBUG
109
+
110
+ pp HttpDebugOutput::Parser.new(debug_output).call
111
+ ```
112
+
113
+ ## Development
114
+
115
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
116
+
117
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
118
+
119
+ ## Contributing
120
+
121
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dbackowski/http_debug_output-parser.
122
+
123
+ ## License
124
+
125
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/http_debug_output/parser/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'http_debug_output-parser'
7
+ spec.version = HttpDebugOutput::Parser::VERSION
8
+ spec.authors = ['Damian Baćkowski']
9
+ spec.email = ['damianbackowski@gmail.com']
10
+
11
+ spec.summary = "Ruby's Net::HTTP debug output parser"
12
+ spec.description = "A gem that parses debug output from Ruby's Net::HTTP."
13
+ spec.homepage = 'https://github.com/dbackowski/http_debug_output-parser'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 3.2.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(__dir__) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (File.expand_path(f) == __FILE__) ||
24
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ spec.metadata['rubygems_mfa_required'] = 'true'
37
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HttpDebugOutput
4
+ class Parser
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'parser/version'
4
+ require 'json'
5
+
6
+ module HttpDebugOutput
7
+ class Parser
8
+ def initialize(debug_output)
9
+ @debug_output = debug_output.split("\n")
10
+ end
11
+
12
+ def call
13
+ {
14
+ request: parse_request,
15
+ response: parse_response
16
+ }
17
+ end
18
+
19
+ private
20
+
21
+ def parse_request # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
22
+ request_lines = @debug_output[find_range_for_start_and_end_strings('<- ', '-> ')]
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
27
+
28
+ {
29
+ method:,
30
+ path:,
31
+ protocol:,
32
+ headers:,
33
+ payload: payload.empty? ? nil : JSON.parse(payload)
34
+ }
35
+ end
36
+
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 = response_lines[headers.count + 2..].join
44
+
45
+ {
46
+ protocol:,
47
+ status:,
48
+ message: message.join(' '),
49
+ headers:,
50
+ payload: payload.empty? ? nil : JSON.parse(payload)
51
+ }
52
+ end
53
+
54
+ def find_range_for_start_and_end_strings(start_index_string, end_index_string)
55
+ start_index = @debug_output.find_index { |line| line.start_with?(start_index_string) }
56
+ end_index = @debug_output.find_index { |line| line.start_with?(end_index_string) }
57
+ start_index..end_index - 1
58
+ end
59
+
60
+ def clean_line(line)
61
+ line.gsub("\r", '').gsub(/\A"/, '').gsub(/"\Z/, '')
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,6 @@
1
+ module HttpDebugOutput
2
+ module Parser
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_debug_output-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Damian Baćkowski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-02-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem that parses debug output from Ruby's Net::HTTP.
14
+ email:
15
+ - damianbackowski@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - http_debug_output-parser.gemspec
26
+ - lib/http_debug_output/parser.rb
27
+ - lib/http_debug_output/parser/version.rb
28
+ - sig/http_debug_output/parser.rbs
29
+ homepage: https://github.com/dbackowski/http_debug_output-parser
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/dbackowski/http_debug_output-parser
34
+ rubygems_mfa_required: 'true'
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.2.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.4.21
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Ruby's Net::HTTP debug output parser
54
+ test_files: []