Sha256: b0d15ce013a461dc776a903a4b9a67ca94d2bab1ff055ca99905fefc48984d1a

Contents?: true

Size: 1.74 KB

Versions: 24

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

##############################################################################
#                             Faraday Plugin
##############################################################################

if defined?(::Faraday::Response::Middleware) && defined?(::Faraday::Response)
  module  Rspeckled
  module  Faraday
  module  Middleware
  class   DetailedLogger < ::Faraday::Response::Middleware
    def self.default_logger
      require 'logger'

      ::Logger.new(STDOUT)
    end

    def initialize(app, logger = nil, program_name = nil)
      super(app)

      @logger       = logger || self.class.default_logger
      @program_name = program_name
    end

    def call(env)
      @logger.info(@program_name) { curl_output('request', "#{env[:method].upcase} #{env[:url]}", env[:request_headers], env[:body]) }

      super
    end

    def on_complete(env)
      @logger.info(@program_name) { curl_output('response', "HTTP #{env[:status]}", env[:response_headers], env[:body]) }
    end

    private

    def curl_output(direction, summary, headers, body)
      directional_arrow = (direction == 'request') ? '>' : '<'
      color_code        = (direction == 'request') ? 32 : 33

      curl_formatted_headers = headers.map { |name, value|
        colorize("#{directional_arrow} #{name}: #{value}", color_code)
      }.join("\n")

      [
        '',
        colorize("#{directional_arrow} #{summary}", color_code),
        curl_formatted_headers,
        '',
        colorize(body, 37),
      ].join("\n")
    end

    def colorize(string, color_code)
      "\e[#{color_code}m#{string}\e[0m"
    end
  end
  end
  end
  end

  ::Faraday::Response.register_middleware(:detailed_logger => Rspeckled::Faraday::Middleware::DetailedLogger)
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rspeckled-0.0.54 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.53 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.52 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.51 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.50 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.49 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.48 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.47 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.46 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.45 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.44 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.43 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.42 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.41 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.40 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.39 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.38 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.37 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.36 lib/rspeckled/plugins/faraday.rb
rspeckled-0.0.35 lib/rspeckled/plugins/faraday.rb