errordeck 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6078ce79d1fe4195e34a910d13cc62e7285d98eb6f8f78eb716133ebc690c608
4
+ data.tar.gz: 1bb5a9b64a83a4d2f7c354783da44508cfa86c68944432dd5cb518c80d703331
5
+ SHA512:
6
+ metadata.gz: 2ec4c9676e3bffa37c2f57b875c0409037e041bf54ceba36dd5dd0b68573e0abb51ff55955f4e8adca7a49355ec52405731bd0cedbb5b7270da9ddf943e6f40e
7
+ data.tar.gz: f0a38c4852124b852891b45c219dc42434514ecd6dbc43d7914f309f839acbc546da007b02ce8df55481794564172fd584cc19b2b0ef7f1210ee31d87656f1fb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ SuggestExtensions: false
4
+ NewCops: enable
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 120
16
+
17
+ Style/Documentation:
18
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-01-28
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at hakan@dun.se. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in errordeck-ruby.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ errordeck (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ json (2.6.3)
12
+ parallel (1.22.1)
13
+ parser (3.2.0.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.6.2)
18
+ rexml (3.2.5)
19
+ rspec (3.12.0)
20
+ rspec-core (~> 3.12.0)
21
+ rspec-expectations (~> 3.12.0)
22
+ rspec-mocks (~> 3.12.0)
23
+ rspec-core (3.12.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-expectations (3.12.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-mocks (3.12.3)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-support (3.12.0)
32
+ rubocop (1.44.1)
33
+ json (~> 2.3)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.2.0.0)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml (>= 3.2.5, < 4.0)
39
+ rubocop-ast (>= 1.24.1, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 2.4.0, < 3.0)
42
+ rubocop-ast (1.24.1)
43
+ parser (>= 3.1.1.0)
44
+ ruby-progressbar (1.11.0)
45
+ unicode-display_width (2.4.2)
46
+
47
+ PLATFORMS
48
+ ruby
49
+ x86_64-linux
50
+
51
+ DEPENDENCIES
52
+ errordeck!
53
+ rake (~> 13.0)
54
+ rspec (~> 3.0)
55
+ rubocop (~> 1.21)
56
+
57
+ BUNDLED WITH
58
+ 2.3.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Håkan Nylén
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,112 @@
1
+ # Errordeck
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/errordeck-ruby.svg)](https://badge.fury.io/rb/errordeck-ruby)
4
+
5
+ Ruby client for Errordeck.
6
+
7
+ ## Features
8
+
9
+ - [x] Send errors to Errordeck
10
+ - [x] Send user context to Errordeck
11
+ - [x] Send tags to Errordeck
12
+ - [x] Send environment to Errordeck
13
+ - [x] Send platform to Errordeck
14
+ - [x] Send stacktrace to Errordeck
15
+ - [x] Send context/file content (around the error) to Errordeck
16
+ - [ ] Send breadcrumbs to Errordeck
17
+ - [ ] Send release to Errordeck
18
+ - [ ] Send source maps to Errordeck
19
+ - [x] Send extra data to Errordeck
20
+ - [x] Send fingerprint to Errordeck
21
+ - [x] Send level to Errordeck
22
+ - [x] Send server name to Errordeck
23
+ - [x] Send modules to Errordeck
24
+ - [x] Send request data to Errordeck
25
+ - [x] Send user data to Errordeck
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'errordeck-ruby'
33
+ ```
34
+
35
+ And then execute:
36
+
37
+ $ bundle install
38
+
39
+ Or install it yourself as:
40
+
41
+ $ gem install errordeck-ruby
42
+
43
+ ## Usage
44
+
45
+ You can set up the client in the following ways:
46
+
47
+ Set the token, project_id, environment, release and dist in the configuration:
48
+ ```ruby
49
+ Errordeck.configure do |config|
50
+ config.token = "_r-3A7egL7uMSFASfdRodzxxxAQo"
51
+ config.project_id = "1"
52
+ config.environment = "development" # optional - if not set, it will be set to Rails.env
53
+ config.release = "0.0.0" # optional
54
+ config.dist = "0.0.0" # optional
55
+ end
56
+ ```
57
+
58
+ And then send an error:
59
+ ```ruby
60
+ begin
61
+ raise "test"
62
+ rescue StandardError => e
63
+ Errordeck.capture(e)
64
+ end
65
+ ```
66
+
67
+ ### Send user context
68
+
69
+ ```ruby
70
+ begin
71
+ raise "test"
72
+ rescue StandardError => e
73
+ Errordeck.capture(e, user: { id: 1})
74
+ end
75
+ ```
76
+
77
+ ### Use wrap to wrap errors with the context
78
+
79
+ ```ruby
80
+ begin
81
+ raise "test"
82
+ rescue StandardError => e
83
+ Errordeck.wrap do |wrap|
84
+ wrap.user_context = { id: 1 }
85
+ wrap.tags_context = { tag: "tag" }
86
+ wrap.context = { context: "context" }
87
+ wrap.capture(e) # needs to be last
88
+ end
89
+ end
90
+ ```
91
+
92
+ ### Using with Rails
93
+
94
+ Rails will add the exceptionhandler automatically, so you don't need to do anything. You can still use above example to fill context and such. And the config is needed.
95
+
96
+ ## Development
97
+
98
+ 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.
99
+
100
+ 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).
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/errordeck/errordeck-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/errordeck/errordeck-ruby/blob/master/CODE_OF_CONDUCT.md).
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
109
+
110
+ ## Code of Conduct
111
+
112
+ Everyone interacting in the Errordeck::Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/errordeck/errordeck-ruby/blob/master/CODE_OF_CONDUCT.md).
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]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "errordeck"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/errordeck/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "errordeck"
7
+ spec.version = Errordeck::VERSION
8
+ spec.authors = ["Håkan Nylén"]
9
+ spec.email = ["hakan@dun.se"]
10
+
11
+ spec.summary = "An errordeck client for ruby"
12
+ spec.homepage = "https://www.errordeck.com"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.7.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://www.github.com/errordeck/errordeck-ruby"
18
+ spec.metadata["changelog_uri"] = "https://www.github.com/errordeck/errordeck-ruby/blob/main/changelog.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
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 "os"
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,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Errordeck
4
+ class Configuration
5
+ attr_accessor :token, :project_id, :environment, :release, :dist, :level, :server_name
6
+
7
+ def initialize
8
+ @token = nil
9
+ @project_id = nil
10
+ @environment = nil
11
+ @release = nil
12
+ @dist = nil
13
+ @level = nil
14
+ @server_name = nil
15
+ end
16
+ end
17
+
18
+ class << self
19
+ attr_accessor :configuration
20
+
21
+ def configure
22
+ self.configuration ||= Configuration.new
23
+ yield(configuration)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Errordeck
6
+ class Backtrace
7
+ Line = Struct.new(:file, :line, :method, :module_name, :in_app) do
8
+ RB_EXTENSION = ".rb"
9
+ RUBY_INPUT_FORMAT = /
10
+ ^ \s* (?: [a-zA-Z]: | uri:classloader: )? ([^:]+ | <.*>):
11
+ (\d+)
12
+ (?: :in \s `([^']+)')?$
13
+ /x.freeze
14
+
15
+ def self.parse(unparsed_line)
16
+ file, line, method = unparsed_line.match(RUBY_INPUT_FORMAT).captures
17
+ file.sub!(/\.class$/, RB_EXTENSION)
18
+ module_name = File.basename(file, ".*") + RB_EXTENSION
19
+
20
+ in_app = %w[bin exe app config lib test].any? { |dir| file.include?(dir) }
21
+
22
+ new(file, line.to_i, method, module_name, in_app)
23
+ end
24
+ end
25
+
26
+ attr_reader :lines
27
+
28
+ def self.parse(backtrace)
29
+ return nil if backtrace.nil?
30
+
31
+ lines = backtrace.map(&:chomp).map { |line| Line.parse(line) }
32
+
33
+ new(lines)
34
+ end
35
+
36
+ def initialize(lines)
37
+ @lines = lines
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "etc"
4
+ module Errordeck
5
+ # context to set OS context and runtime context
6
+ class Context
7
+ def self.runtime
8
+ { name: "ruby", version: RUBY_VERSION }
9
+ end
10
+
11
+ def self.os
12
+ uname = Etc.uname
13
+ {
14
+ name: uname[:sysname] || RbConfig::CONFIG["host_os"],
15
+ version: uname[:version],
16
+ build: uname[:build],
17
+ kernel_version: uname[:version]
18
+ }.compact
19
+ end
20
+
21
+ # get context
22
+ def self.context
23
+ { os: os, runtime: runtime }.compact
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+ module Errordeck
5
+ class Event
6
+ SDK = { "name" => "errordeck-ruby", "version" => Errordeck::VERSION }.freeze
7
+
8
+ attr_accessor :id, :timestamp, :platform, :level, :logger, :tags, :transaction, :server_name, :release, :dist,
9
+ :environment, :message, :modules, :extra, :exceptions, :contexts, :request, :sdk, :user
10
+
11
+ def initialize(level:, transaction:, message:, server_name: nil, release: nil, dist: nil,
12
+ environment: "development", modules: nil, extra: nil, tags: nil, exceptions: nil, contexts: nil, request: nil, user: nil)
13
+ @id = ::SecureRandom.uuid.delete("-")
14
+ @timestamp = Time.now.utc
15
+ @platform = :ruby
16
+ @level = level
17
+ @logger = :ruby
18
+ @transaction = transaction
19
+ @server_name = server_name
20
+ @release = release
21
+ @dist = dist
22
+ @environment = environment
23
+ @message = message
24
+ @modules = modules
25
+ @extra = extra
26
+ @exceptions = exceptions
27
+ @contexts = contexts
28
+ @request = request
29
+ @tags = tags
30
+ @sdk = SDK
31
+ @user = user
32
+ end
33
+
34
+ def as_json(*_options)
35
+ {
36
+ event_id: @id,
37
+ timestamp: @timestamp.to_i,
38
+ platform: @platform,
39
+ level: @level,
40
+ logger: @logger,
41
+ transaction: @transaction,
42
+ server_name: @server_name,
43
+ release: @release,
44
+ dist: @dist,
45
+ environment: @environment,
46
+ message: @message,
47
+ modules: @modules,
48
+ extra: @extra,
49
+ exceptions: @exceptions,
50
+ contexts: @contexts,
51
+ request: @request,
52
+ sdk: @sdk,
53
+ user: @user
54
+ }
55
+ end
56
+
57
+ def to_json(*args)
58
+ JSON.generate(as_json, *args)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Errordeck
4
+ class Exception
5
+ attr_accessor :type, :value, :stacktrace, :module, :thread_id
6
+
7
+ def initialize(type, value, stacktrace, module_name, thread_id)
8
+ @type = type
9
+ @value = value
10
+ @stacktrace = stacktrace
11
+ @module = module_name
12
+ @thread_id = thread_id
13
+ end
14
+
15
+ def self.parse_from_exception(exception, project_root)
16
+ exception_type = exception.class.to_s
17
+ exception_value = exception.message
18
+ exception_stacktrace = Errordeck::Stacktrace.parse_from_backtrace(exception.backtrace, project_root)
19
+ exception_module = exception_type.split("::")[0]
20
+ exception_thread_id = Thread.current.object_id
21
+
22
+ [new(exception_type, exception_value, exception_stacktrace, exception_module, exception_thread_id)]
23
+ end
24
+
25
+ def as_json(*_options)
26
+ {
27
+ type: @type,
28
+ value: @value,
29
+ stacktraces: @stacktrace,
30
+ module: @module,
31
+ thread_id: @thread_id
32
+ }
33
+ end
34
+
35
+ def to_json(*options)
36
+ JSON.generate(as_json, *options)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Errordeck
4
+ class Request
5
+ attr_accessor :url, :method, :data, :query_string, :cookies, :headers, :env
6
+
7
+ HEADERS = %w[HOST USER-AGENT ACCEPT ACCEPT_LANGUAGE ACCEPT_ENCODING CONNECTION CACHE_CONTROL
8
+ UPGRADE_INSECURE_REQUESTS DNT SEC_FETCH_SITE SEC_FETCH_MODE SEC_FETCH_USER SEC_FETCH_DEST REFERER].freeze
9
+ COOKIE = %w[COOKIE].freeze
10
+
11
+ def initialize(url, method, data, query_string, cookies, headers, env)
12
+ @url = Scrubber::Url.new(url).scrub
13
+ @method = method
14
+ @data = data
15
+ @query_string = Scrubber::QueryParam.new(query_string).scrub
16
+ @cookies = Scrubber::Cookie.new(cookies).scrub
17
+ @headers = Scrubber::Header.new(headers.transform_keys { |k| k.to_s.gsub("HTTP_", "").gsub("_", "-") }).scrub
18
+ @env = env
19
+ end
20
+
21
+ def self.parse_from_rack_env(env)
22
+ request = RequestHandler.parse_from_rack_env(env)
23
+ new(request.url, request.method, nil, request.query_string, request.cookies, request.headers, env)
24
+ end
25
+
26
+ def self.parse_from_request_handler(request)
27
+ new(request.url, request.method, nil, request.query_string, request.cookies, request.headers, nil)
28
+ end
29
+
30
+ def self.split_to_has_on(resource, split = ";")
31
+ resource.split(split).each_with_object({}) do |item, hash|
32
+ key, value = item.split("=").map(&:strip)
33
+ hash[key] = value
34
+ end
35
+ end
36
+
37
+ def headers
38
+ filtered_headers
39
+ end
40
+
41
+ def as_json(*_options)
42
+ {
43
+ url: @url,
44
+ method: @method,
45
+ query_string: @query_string,
46
+ headers: headers
47
+ }
48
+ end
49
+
50
+ def to_json(*options)
51
+ JSON.generate(as_json, *options)
52
+ end
53
+
54
+ private
55
+
56
+ def filtered_headers
57
+ @headers.select { |k, _v| HEADERS.include?(k) }
58
+ end
59
+ end
60
+ end