class Faraday::Conductivity::ExtendedLogging

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/faraday/conductivity/extended_logging.rb, line 10
def initialize(app, options = {})
  @app = app
  @logger = options.fetch(:logger) {
    require 'logger'
    ::Logger.new($stderr)
  }
end

Public Instance Methods

call(env) click to toggle source
# File lib/faraday/conductivity/extended_logging.rb, line 18
def call(env)
  start_time = Time.now
  debug { request_info(env) }
  debug { request_debug(env) }
  @app.call(env).on_complete do
    end_time = Time.now
    response_time = end_time - start_time
    info  { response_info(env, response_time) }
    debug { response_debug(env) }
  end
end

Private Instance Methods

debug_message(name, headers, body) click to toggle source
# File lib/faraday/conductivity/extended_logging.rb, line 48
      def debug_message(name, headers, body)
        <<-MESSAGE.gsub(/^ +([^ ])/m, '\\1')
        #{name} Headers:
        ----------------
        #{format_headers(headers)}

        #{name} Body:
        -------------
        #{body}
        MESSAGE
      end
format_headers(headers) click to toggle source
# File lib/faraday/conductivity/extended_logging.rb, line 60
def format_headers(headers)
  length = headers.map {|k,v| k.to_s.size }.max
  headers.map { |name, value| "#{name.to_s.ljust(length)} : #{value}" }.join("\n")
end
request_debug(env) click to toggle source
# File lib/faraday/conductivity/extended_logging.rb, line 40
def request_debug(env)
  debug_message("Request", env[:request_headers], env[:body])
end
request_info(env) click to toggle source
# File lib/faraday/conductivity/extended_logging.rb, line 32
def request_info(env)
  "Started %s request to: %s" % [ env[:method].to_s.upcase, env[:url] ]
end
response_debug(env) click to toggle source
# File lib/faraday/conductivity/extended_logging.rb, line 44
def response_debug(env)
  debug_message("Response", env[:response_headers], env[:body])
end
response_info(env, response_time) click to toggle source
# File lib/faraday/conductivity/extended_logging.rb, line 36
def response_info(env, response_time)
  "Response from %s %s; Status: %d; Time: %.1fms" % [ env[:method].to_s.upcase, env[:url], env[:status], (response_time * 1_000.0) ]
end