Sha256: ed48f9f2d21b11d8df7d8ae27cf81b3d2431dbfc2fd34635b40b3e8b0e6ed7ae

Contents?: true

Size: 1007 Bytes

Versions: 1

Compression:

Stored size: 1007 Bytes

Contents

module Rastreioz
  class Log

    def with_log
      response = yield
      Rastreioz.log(format_request_message(response))
      Rastreioz.log(format_response_message(response))
      response
    end

    def format_request_message(response)
      message =  with_line_break { "RastreioZ Request:" }
      message << with_line_break { "#{response.uri}" }
    end

    def format_response_message(response)
      message =  with_line_break { "RastreioZ Response:" }
      message << with_line_break { "HTTP/#{response.http_version} #{response.code} #{response.message}" }
      message << with_line_break { format_headers_for(response) } if Rastreioz.log_level == :debug
      message << with_line_break { response.body }
    end

    def format_headers_for(http)
      # I'm using an empty block in each_header method for Ruby 1.8.7 compatibility.
      http.each_header{}.map { |name, values| "#{name}: #{values.first}" }.join("\n")
    end

    def with_line_break
      "#{yield}\n"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rastreioz-0.1.7 lib/rastreioz/log.rb