Sha256: c3dbc3e5d6e48d9e75117504cd3438b9e3089f9c3360e69ce79ce2d24b7e9c77

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

# Code courtesty https://github.com/envylabs/faraday-detailed_logger - MIT licence

module Spektrix
  class DebugMiddleware < Faraday::Response::Middleware

    def self.default_logger
      require "logger"
      ::Logger.new(STDOUT)
    end

    # Public: Initialize a new Logger middleware.
    #
    # app - A Faraday-compatible middleware stack or application.
    # logger - A Logger-compatible object to which the log information will
    #          be recorded.
    # progname - A String containing a program name to use when logging.
    #
    # Returns a Logger instance.
    #
    def initialize(app, logger = nil, progname = nil)
      super(app)
      @logger = logger || self.class.default_logger
      @progname = progname
    end

    # Public: Used by Faraday to execute the middleware during the
    # request/response cycle.
    #
    # env - A Faraday-compatible request environment.
    #
    # Returns the result of the parent application execution.
    #
    def call(env)
      if Spektrix.debug_request
        @logger.info(@progname) { "#{env[:method].upcase} #{env[:url]}" }
        @logger.debug(@progname) { curl_output(env[:request_headers], env[:body]).inspect }
      end

      super
    end

    private
    def curl_output(headers, body)
      string = headers.collect { |k,v| "#{k}: #{v}" }.join("\n")
      string + "\n\n#{body}"
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spektrix-0.0.2.5 lib/spektrix/debug_middleware.rb
spektrix-0.0.2.4 lib/spektrix/debug_middleware.rb
spektrix-0.0.2.3 lib/spektrix/debug_middleware.rb
spektrix-0.0.2.1 lib/spektrix/debug_middleware.rb