Sha256: aa11437fc711bea0fa78485615cf4a557697f123a9df45c702e736c42e69c271

Contents?: true

Size: 1010 Bytes

Versions: 2

Compression:

Stored size: 1010 Bytes

Contents

# encoding: utf-8
module LogMe
  class HttpFormatter
    def format_request(request, url)
      message = format_message(request) do
        message =  with_line_break { 'Request:' }
        message << with_line_break { "#{request.method.to_s.upcase} #{url}" }
      end
    end

    def format_response(response)
      message = format_message(response) do
        message =  with_line_break { 'Response:' }
        message << with_line_break { "HTTP/#{response.http_version} #{response.code} #{response.message}" }
      end
    end

    private

    def format_message(http)
      message = yield
      body = extract_body_from(http)
      message << with_line_break { body } unless blank?(body)
      message
    end

    def with_line_break
      "#{yield}\n"
    end

    def extract_body_from(http)
      return http.body if http.respond_to?(:body)
      return http.args[:payload] if http.respond_to?(:args)
      return nil
    end

    def blank?(text)
      text.to_s.strip.empty?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
log-me-0.0.10 lib/logme/http_formatter.rb
log-me-0.0.9 lib/logme/http_formatter.rb