rails_api_logger 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '00489e11faa84622f3557560cf9d989b1fb36f6c6d18fdd41f1f1784e59893dd'
4
- data.tar.gz: 54f3202bd8446cce3a32c97a2da3b2f881d317207c511bf1d9ada9250ac751d8
3
+ metadata.gz: 545d9a066433986966d131cd6f874fbe58607fe03f2aae947eec494815e2bc60
4
+ data.tar.gz: 8565825afc3af8704877c534df8e7f334a3d90ddedca4813879a89846e5b745c
5
5
  SHA512:
6
- metadata.gz: 254e3a0054438fa416561f5e34155529f4860d255cb01e80c5ea5a5a65bac694b35bc6b88199775b1fa11bc6532981f6e514b517f703140020c6b9618284ce1e
7
- data.tar.gz: bf5537dfcb5eec1c675b9eb68bee42a8700fa756d1728666c67075a77a9f99baf0f573b5090e36dd6e345c0d5471b923fbcaddd8f689c257e89c496e858722cd
6
+ metadata.gz: 1334657ab5a74509d0a5be48e344b439038207577c80e6ced192043b13fb6005804a2b1275c195b813f5993ee104d0e8ed6b2f8dc575b038a4b5728dcb31f4aa
7
+ data.tar.gz: 9ca93777e0035586de21dbfb19bac23e01b235a1556d78f824a88a2f191a61938e6fc12d44e4990a1de3b546a52d04006591641e4e053229d87d0e326b917316
@@ -19,7 +19,8 @@ blocks:
19
19
  commands:
20
20
  - checkout --use-cache
21
21
  - cache restore
22
- - bundle install --deployment -j 4 --path vendor/bundle
22
+ - bundle config set path 'vendor/bundle'
23
+ - bundle install -j 4
23
24
  - cache store
24
25
  jobs:
25
26
  - name: tests
data/README.md CHANGED
@@ -72,9 +72,9 @@ You can also use the provided logger class to do that in a simpler and safer man
72
72
 
73
73
  ```ruby
74
74
  uri = URI('http://example.com/some_path?query=string')
75
- http = Net::HTTP.start(uri.host, uri.port)
75
+ http = Net::HTTP.new(uri.host, uri.port)
76
76
  request = Net::HTTP::Get.new(uri)
77
- response = RailsApiLogger.call(uri, http, request)
77
+ response = RailsApiLogger.new.call(uri, request) { http.start { |http| http.request(request) } }
78
78
  ```
79
79
 
80
80
  This will guarantee that the log is always persisted, even in case of errors.
@@ -170,12 +170,6 @@ This configuration will give you some nice views, and searches to work with the
170
170
  end
171
171
  ```
172
172
 
173
- you can also add this configuration directly in `rails_admin.rb` with
174
-
175
- ```ruby
176
- include RailsApiLoggerRailsAdminConfiguration
177
- ```
178
-
179
173
  ## Development
180
174
 
181
175
  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.
@@ -3,7 +3,7 @@
3
3
  require "rails/generators/base"
4
4
  require "rails/generators/migration"
5
5
 
6
- module RailsApiLogger
6
+ class RailsApiLogger
7
7
  module Generators
8
8
  class InstallGenerator < Rails::Generators::Base
9
9
  include Rails::Generators::Migration
@@ -1,4 +1,4 @@
1
- class InboundRequestLoggerMiddleware
1
+ class InboundRequestsLoggerMiddleware
2
2
  attr_accessor :only_state_change, :path_regexp
3
3
 
4
4
  def initialize(app, only_state_change: true, path_regexp: /.*/)
@@ -11,7 +11,7 @@ class RequestLog < ActiveRecord::Base
11
11
  validates :method, presence: true
12
12
  validates :path, presence: true
13
13
 
14
- def self.from_request(request)
14
+ def self.from_request(request, loggable: nil)
15
15
  request_body = (request.body.respond_to?(:read) ? request.body.read : request.body)
16
16
  body = request_body ? request_body.dup.force_encoding("UTF-8") : nil
17
17
  begin
@@ -19,7 +19,18 @@ class RequestLog < ActiveRecord::Base
19
19
  rescue JSON::ParserError
20
20
  body
21
21
  end
22
- create(path: request.path, request_body: body, method: request.method, started_at: Time.current)
22
+ create(path: request.path, request_body: body, method: request.method, started_at: Time.current, loggable: loggable)
23
+ end
24
+
25
+ def from_response(response)
26
+ self.response_code = response.code
27
+ body = response.body ? response.body.dup.force_encoding("UTF-8") : nil
28
+ begin
29
+ body = JSON.parse(body) if body.present?
30
+ rescue JSON::ParserError
31
+ body
32
+ end
33
+ self.response_body = body
23
34
  end
24
35
 
25
36
  def formatted_request_body
@@ -1,21 +1,23 @@
1
1
  require "active_record"
2
2
  require "nokogiri"
3
- require "rails_api_logger/version"
4
- require "rails_api_logger/request_log"
5
- require "rails_api_logger/inbound_request_log"
6
- require "rails_api_logger/outbound_request_log"
7
- require "rails_api_logger/inbound_requests_logger"
8
- require "rails_api_logger/inbound_requests_logger_middleware"
3
+ require "zeitwerk"
9
4
 
10
- module RailsApiLogger
5
+ loader = Zeitwerk::Loader.for_gem
6
+ loader.collapse("#{__dir__}/rails_api_logger")
7
+ loader.do_not_eager_load("#{__dir__}/generators")
8
+ loader.setup
9
+
10
+ class RailsApiLogger
11
11
  class Error < StandardError; end
12
12
 
13
- def self.call(uri, http, request)
14
- log = OutboundRequestLog.from_request(request)
13
+ def initialize(loggable = nil)
14
+ @loggable = loggable
15
+ end
15
16
 
16
- http.request(request).tap do |response|
17
- log.response_code = response.code
18
- log.response_body = response.body
17
+ def call(url, request)
18
+ log = OutboundRequestLog.from_request(request, loggable: @loggable)
19
+ yield.tap do |response|
20
+ log.from_response(response)
19
21
  end
20
22
  rescue => e
21
23
  log.response_body = {error: e.message}
@@ -1,8 +1,6 @@
1
- require_relative "lib/rails_api_logger/version"
2
-
3
1
  Gem::Specification.new do |spec|
4
2
  spec.name = "rails_api_logger"
5
- spec.version = RailsApiLogger::VERSION
3
+ spec.version = "0.5.0"
6
4
  spec.authors = ["Alessandro Rodi"]
7
5
  spec.email = ["alessandro.rodi@renuo.ch"]
8
6
 
@@ -28,6 +26,7 @@ Gem::Specification.new do |spec|
28
26
  spec.add_dependency "railties", ">= 4.1.0"
29
27
  spec.add_dependency "activerecord", ">= 4.1.0"
30
28
  spec.add_dependency "nokogiri"
29
+ spec.add_dependency "zeitwerk", ">= 2.0.0"
31
30
 
32
31
  spec.add_development_dependency "sqlite3", "~> 1.4.0"
33
32
  spec.add_development_dependency "standard", "~> 0.13.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_api_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Rodi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-09 00:00:00.000000000 Z
11
+ date: 2022-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: zeitwerk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: sqlite3
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -135,9 +149,7 @@ files:
135
149
  - lib/rails_api_logger/inbound_requests_logger.rb
136
150
  - lib/rails_api_logger/inbound_requests_logger_middleware.rb
137
151
  - lib/rails_api_logger/outbound_request_log.rb
138
- - lib/rails_api_logger/rails_api_logger_rails_admin_configuration.rb
139
152
  - lib/rails_api_logger/request_log.rb
140
- - lib/rails_api_logger/version.rb
141
153
  - rails_api_logger.gemspec
142
154
  homepage: https://github.com/renuo/rails_api_logger
143
155
  licenses:
@@ -146,7 +158,7 @@ metadata:
146
158
  homepage_uri: https://github.com/renuo/rails_api_logger
147
159
  source_code_uri: https://github.com/renuo/rails_api_logger
148
160
  changelog_uri: https://github.com/renuo/rails_api_logger/CHANGELOG.md
149
- post_install_message:
161
+ post_install_message:
150
162
  rdoc_options: []
151
163
  require_paths:
152
164
  - lib
@@ -161,8 +173,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
173
  - !ruby/object:Gem::Version
162
174
  version: '0'
163
175
  requirements: []
164
- rubygems_version: 3.2.16
165
- signing_key:
176
+ rubygems_version: 3.1.4
177
+ signing_key:
166
178
  specification_version: 4
167
179
  summary: "Log API requests like a king \U0001F451"
168
180
  test_files: []
@@ -1,33 +0,0 @@
1
- %w[InboundRequestLog OutboundRequestLog].each do |logging_model|
2
- config.model logging_model do
3
- list do
4
- filters %i[method path response_code request_body response_body created_at]
5
- scopes [nil, :failed]
6
-
7
- include_fields :method, :path, :response_code, :created_at
8
-
9
- field :request_body, :string do
10
- visible false
11
- searchable true
12
- filterable true
13
- end
14
-
15
- field :response_body, :string do
16
- visible false
17
- searchable true
18
- filterable true
19
- end
20
- end
21
-
22
- show do
23
- include_fields :loggable, :method, :path, :response_code
24
- field(:created_at)
25
- field(:request_body) do
26
- formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].request_body)}</pre>".html_safe }
27
- end
28
- field(:response_body) do
29
- formatted_value { "<pre>#{JSON.pretty_generate(bindings[:object].response_body)}</pre>".html_safe }
30
- end
31
- end
32
- end
33
- end
@@ -1,3 +0,0 @@
1
- module RailsApiLogger
2
- VERSION = "0.4.1"
3
- end