faraday-logging-color_formatter 0.1.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: 4ae45ba949e38fd7d48e52261a5a970a9dbc6364418ec3e9d63390f94ec4f2e5
4
+ data.tar.gz: 9cc5d52fede52c09b37cfdc5cca4a33f6aa8c00787df2a99307782e7025219a7
5
+ SHA512:
6
+ metadata.gz: a7475d9f73f29aab4631bb938cf1d85af1c14d7649c7ffe48157deb4df7c6178eee2e48ae7cb8e4a173eab47f1a0c7d7b94340f7dee579a78751ae21c40d4985
7
+ data.tar.gz: b4063955edb24f02e2494b1d9a661eaef4ca006de74c42346208e1b8e3a32fefa3a4d9dd3a801721c0afecff85ff35d4bbca1e3b3470d2307e480f95035d25f5
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ require:
2
+ - rubocop-rspec
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.7
7
+ NewCops: enable
8
+
9
+ Metrics/AbcSize:
10
+ Enabled: false
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Style/FrozenStringLiteralComment:
16
+ Exclude:
17
+ - exe/**/*
18
+
19
+ Style/StringLiterals:
20
+ Enabled: true
21
+ EnforcedStyle: single_quotes
22
+
23
+ Style/StringLiteralsInInterpolation:
24
+ Enabled: true
25
+ EnforcedStyle: single_quotes
26
+
27
+ Layout/LineLength:
28
+ Max: 160
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Change Log
2
+
3
+ ## 0.1.0
4
+ #### 2023-01-11
5
+ * Initial commit.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in faraday-logging-color_formatter.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Kobus Joubert
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,49 @@
1
+ # Faraday::Logging::ColorFormatter
2
+
3
+ Add some color to your Faraday logging output.
4
+
5
+ Before...
6
+
7
+ ![before](https://user-images.githubusercontent.com/3071529/212807973-e87213da-d687-4db2-af70-5bb67eb81e0a.png)
8
+
9
+ After...
10
+
11
+ ![after](https://user-images.githubusercontent.com/3071529/212807978-609ee381-f445-4d98-9994-31f7a4e5caa5.png)
12
+
13
+ ## Installation
14
+
15
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
16
+
17
+ Install the gem and add to the application's Gemfile by executing:
18
+
19
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
20
+
21
+ If bundler is not being used to manage dependencies, install the gem by executing:
22
+
23
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
24
+
25
+ ## Usage
26
+
27
+ Add the following to your ruby program.
28
+
29
+ require 'faraday/logging/color_formatter'
30
+
31
+ And then add the `Faraday::Logging::ColorFormatter` formatter to your logger middleware config.
32
+
33
+ connection = Faraday.new(url: 'http://httpbingo.org') do |conn|
34
+ conn.response :logger, ActiveSupport::Logger.new($stdout), formatter: Faraday::Logging::ColorFormatter
35
+ end
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests and `rake rubocop` to run the linters. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ 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).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kobusjoubert/faraday-logging-color_formatter.
46
+
47
+ ## License
48
+
49
+ 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,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/faraday/logging/color_formatter/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'faraday-logging-color_formatter'
7
+ spec.version = Faraday::Logging::ColorFormatter::VERSION
8
+ spec.authors = ['Kobus Joubert']
9
+ spec.email = ['kobus@translate3d.com']
10
+
11
+ spec.summary = 'Faraday color logging formatter'
12
+ spec.description = 'Add some color to your Faraday logging output.'
13
+ spec.homepage = 'https://github.com/kobusjoubert/faraday-logging-color_formatter'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.7.0'
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/kobusjoubert/faraday-logging-color_formatter'
18
+ spec.metadata['changelog_uri'] = 'https://github.com/kobusjoubert/faraday-logging-color_formatter/CHANGELOG.md'
19
+ spec.metadata['rubygems_mfa_required'] = 'true'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
26
+ end
27
+ end
28
+
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_runtime_dependency 'faraday', '~> 2.1'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 2.3'
34
+ spec.add_development_dependency 'rake', '~> 13.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.12'
36
+ spec.add_development_dependency 'rubocop', '~> 1.42'
37
+ spec.add_development_dependency 'rubocop-rake', '~> 0.6'
38
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.16'
39
+
40
+ # For more information and examples about making a new gem, check out our
41
+ # guide at: https://bundler.io/guides/creating_gem.html
42
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ module Faraday
6
+ module Logging
7
+ module ColorFormatter
8
+ class Formatter < Faraday::Logging::Formatter
9
+ ANSI_COLORS = { red: "\e[31m", green: "\e[32m", yellow: "\e[33m", blue: "\e[34m", magenta: "\e[35m", cyan: "\e[36m", reset: "\e[0m" }.freeze
10
+
11
+ def request(env)
12
+ http_request_term = in_color(:blue) { 'HTTP Request' }
13
+ http_request_log = in_color(request_log_color(env.method)) { "#{env.method.upcase} #{apply_filters(env.url.to_s)}" }
14
+
15
+ request_log = proc { "#{http_request_term} #{http_request_log}" }
16
+ public_send(log_level, &request_log)
17
+
18
+ log_headers(nil, env.request_headers) if log_headers?(:request)
19
+ log_body(nil, env[:body]) if env[:body] && log_body?(:request)
20
+ end
21
+
22
+ def response(env)
23
+ http_response_term = in_color(:blue) { 'HTTP Response' }
24
+ http_response_log = in_color(response_log_color(env.status)) { "Status #{env.status}" }
25
+
26
+ status = proc { "#{http_response_term} #{http_response_log}" }
27
+ public_send(log_level, &status)
28
+
29
+ log_headers(nil, env.response_headers) if log_headers?(:response)
30
+ log_body(nil, env[:body]) if env[:body] && log_body?(:response)
31
+ end
32
+
33
+ private
34
+
35
+ def in_color(color)
36
+ "#{ANSI_COLORS[color]}#{yield}#{ANSI_COLORS[:reset]}"
37
+ end
38
+
39
+ def request_log_color(request_method)
40
+ case request_method
41
+ when :post
42
+ :green
43
+ when :put, :patch
44
+ :yellow
45
+ when :delete
46
+ :red
47
+ else
48
+ :blue
49
+ end
50
+ end
51
+
52
+ def response_log_color(response_status)
53
+ case response_status
54
+ when 100..199
55
+ :blue
56
+ when 200..299
57
+ :green
58
+ when 300..399
59
+ :yellow
60
+ when 400..599
61
+ :red
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module Logging
5
+ module ColorFormatter
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'color_formatter/version'
4
+ require_relative 'color_formatter/formatter'
5
+
6
+ module Faraday
7
+ module Logging
8
+ module ColorFormatter
9
+ class Error < StandardError; end
10
+
11
+ class << self
12
+ def new(...)
13
+ Faraday::Logging::ColorFormatter::Formatter.new(...)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ module Faraday
2
+ module Logging
3
+ module ColorFormatter
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-logging-color_formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kobus Joubert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.42'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.42'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.16'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.16'
111
+ description: Add some color to your Faraday logging output.
112
+ email:
113
+ - kobus@translate3d.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".rspec"
119
+ - ".rubocop.yml"
120
+ - CHANGELOG.md
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - faraday-logging-color_formatter.gemspec
126
+ - lib/faraday/logging/color_formatter.rb
127
+ - lib/faraday/logging/color_formatter/formatter.rb
128
+ - lib/faraday/logging/color_formatter/version.rb
129
+ - sig/faraday/logging/color_formatter.rbs
130
+ homepage: https://github.com/kobusjoubert/faraday-logging-color_formatter
131
+ licenses:
132
+ - MIT
133
+ metadata:
134
+ homepage_uri: https://github.com/kobusjoubert/faraday-logging-color_formatter
135
+ source_code_uri: https://github.com/kobusjoubert/faraday-logging-color_formatter
136
+ changelog_uri: https://github.com/kobusjoubert/faraday-logging-color_formatter/CHANGELOG.md
137
+ rubygems_mfa_required: 'true'
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: 2.7.0
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubygems_version: 3.4.2
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Faraday color logging formatter
157
+ test_files: []