Sha256: 1248d31f0584e534ba1bc3940caedd619c7da5985dc66aaccf70d6474cfe4f9b

Contents?: true

Size: 1.44 KB

Versions: 5

Compression:

Stored size: 1.44 KB

Contents

module HTTParty
  module Logger
    class CurlFormatter #:nodoc:
      TAG_NAME = HTTParty.name
      OUT = ">"
      IN = "<"

      attr_accessor :level, :logger, :current_time

      def initialize(logger, level)
        @logger = logger
        @level  = level.to_sym
      end

      def format(request, response)
        messages        = []
        time            = Time.now.strftime("%Y-%m-%d %H:%M:%S %z")
        http_method     = request.http_method.name.split("::").last.upcase
        path            = request.path.to_s

        messages << print(time, OUT, "#{http_method} #{path}")

        if request.options[:headers] && request.options[:headers].size > 0
          request.options[:headers].each do |k, v|
            messages << print(time, OUT, "#{k}: #{v}")
          end
        end

        messages << print(time, OUT, request.raw_body)
        messages << print(time, OUT, "")
        messages << print(time, IN, "HTTP/#{response.http_version} #{response.code}")

        headers = response.respond_to?(:headers) ? response.headers : response
        response.each_header do |response_header|
          messages << print(time, IN, "#{response_header.capitalize}: #{headers[response_header]}")
        end

        messages << print(time, IN, "\n#{response.body}")

        @logger.send @level, messages.join("\n")
      end

      def print(time, direction, line)
        "[#{TAG_NAME}] [#{time}] #{direction} #{line}"
      end
    end
  end
end

Version data entries

5 entries across 4 versions & 3 rubygems

Version Path
simplenet-client-0.2.0 ./vendor/bundle/ruby/1.9.1/gems/httparty-0.13.7/lib/httparty/logger/curl_formatter.rb
simplenet-client-0.2.0 ./vendor/bundle/ruby/2.0.0/gems/httparty-0.13.7/lib/httparty/logger/curl_formatter.rb
httparty-0.13.7 lib/httparty/logger/curl_formatter.rb
httparty-0.13.6 lib/httparty/logger/curl_formatter.rb
httpserious-0.13.5.lstoll1 lib/httparty/logger/curl_formatter.rb