Module: Callapi::Call::Request::Http::LogHelper

Included in:
Callapi::Call::Request::Http
Defined in:
lib/callapi/call/request/http/log_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_api_host_logObject



23
24
25
26
27
28
29
# File 'lib/callapi/call/request/http/log_helper.rb', line 23

def add_api_host_log
  string = ''
  string << uri.host
  string << ":#{uri.port}" if uri.port

  puts "Sending request to #{string}".center(80, '-').colorize(:white).on_blue
end

#add_request_headers_logObject



39
40
41
42
43
44
# File 'lib/callapi/call/request/http/log_helper.rb', line 39

def add_request_headers_log
  'HEADERS: '.tap do |string|
    string << "#{headers}"
    puts string.colorize(:cyan)
  end
end

#add_request_params_logObject



46
47
48
49
50
51
# File 'lib/callapi/call/request/http/log_helper.rb', line 46

def add_request_params_log
  'PARAMS:  '.tap do |string|
    string << "#{params}"
    puts string.colorize(:green)
  end
end

#add_request_path_logObject



31
32
33
34
35
36
37
# File 'lib/callapi/call/request/http/log_helper.rb', line 31

def add_request_path_log
  'PATH:    '.tap do |string|
    string << "#{request_method.to_s.upcase} "
    string << "#{uri.path}"
    puts string.colorize(:magenta)
  end
end

#add_response_log(response) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/callapi/call/request/http/log_helper.rb', line 53

def add_response_log(response)
  response.tap do |response|
    "RESPONSE: [#{response.code}]\n".tap do |string|
      string << (response.body.nil? ? '[EMPTY BODY]' : response.body)
      puts string.colorize(:light_blue)
    end
  end
end

#add_response_summary_log(start_time) ⇒ Object



62
63
64
# File 'lib/callapi/call/request/http/log_helper.rb', line 62

def add_response_summary_log(start_time)
  puts "request send (#{(Time.now - start_time).round(3)} sec)".center(80, '-').colorize(:white).on_blue
end

#with_loggingObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/callapi/call/request/http/log_helper.rb', line 4

def with_logging
  return yield if Callapi::Config.log_level == :none

  start_time = Time.now

  add_api_host_log
  add_request_path_log
  add_request_headers_log
  add_request_params_log

  yield.tap do |response|
    add_response_log(response)
    add_response_summary_log(start_time)
  end
rescue StandardError => e
  puts 'Exception occured, skipping logs'.center(80, '-').colorize(:red).on_yellow
  raise e
end