Class: Stosp::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/stosp/logger.rb

Class Method Summary collapse

Class Method Details

.log_response(response, show_body: true) ⇒ Object

Raises:

  • (HTTParty::Error)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stosp/logger.rb', line 14

def self.log_response(response, show_body: true)
  verb = response.request.http_method.to_s.split('::').last.upcase
  request_body = response.request.raw_body if verb != 'GET' && show_body
  uri = response.uri
  parsed_response = response.parsed_response
  result = parsed_response['result'] || parsed_response
  error = parsed_response['errors']&.first
  error ||= parsed_response['error']
  if (error.nil? || error.empty?) && parsed_response['data'] &&
     parsed_response['data'].is_a?(Array) && parsed_response['data'].first&.key?('errors')
    error ||= parsed_response['data'].first['errors']
  end
  response_body = parsed_response unless result

  logger.info "#{verb} #{uri}#{request_body} -> result: #{result} #{uri}#{response_body}"
  return if response.fetch('result', true) && (error.nil? || error.empty?)

  raise HTTParty::Error, "#{error} #{uri}#{response_body} <- #{verb} #{uri}#{request_body}"
end

.loggerObject



7
8
9
10
11
12
# File 'lib/stosp/logger.rb', line 7

def self.logger
  @logger ||= begin
    logger = new $stdout
    logger
  end
end