Class: RailsApiLogger::RequestLog
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- RailsApiLogger::RequestLog
show all
- Defined in:
- app/models/rails_api_logger/request_log.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.from_request(request, loggable: nil, skip_request_body: false) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'app/models/rails_api_logger/request_log.rb', line 17
def self.from_request(request, loggable: nil, skip_request_body: false)
if skip_request_body
body = "[Skipped]"
else
request_body = (request.body.respond_to?(:read) ? request.body.read : request.body)
body = request_body&.dup&.force_encoding("UTF-8")
begin
body = JSON.parse(body) if body.present?
rescue JSON::ParserError
body
end
end
create(path: request.path, request_body: body, method: request.method, started_at: Time.current, loggable: loggable)
end
|
Instance Method Details
#duration ⇒ Object
63
64
65
66
|
# File 'app/models/rails_api_logger/request_log.rb', line 63
def duration
return if started_at.nil? || ended_at.nil?
ended_at - started_at
end
|
#formatted_body(body) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'app/models/rails_api_logger/request_log.rb', line 46
def formatted_body(body)
if body.is_a?(String) && body.blank?
""
elsif body.is_a?(Hash)
JSON.pretty_generate(body)
else
xml = Nokogiri::XML(body)
if xml.errors.any?
body
else
xml.to_xml(indent: 2)
end
end
rescue
body
end
|
#formatted_request_body ⇒ Object
38
39
40
|
# File 'app/models/rails_api_logger/request_log.rb', line 38
def formatted_request_body
formatted_body(request_body)
end
|
#formatted_response_body ⇒ Object
42
43
44
|
# File 'app/models/rails_api_logger/request_log.rb', line 42
def formatted_response_body
formatted_body(response_body)
end
|
#from_response(response, skip_response_body: false) ⇒ Object
32
33
34
35
36
|
# File 'app/models/rails_api_logger/request_log.rb', line 32
def from_response(response, skip_response_body: false)
self.response_code = response.code
self.response_body = skip_response_body ? "[Skipped]" : manipulate_body(response.body)
self
end
|