Class: Net::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/net-http-spy.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ HTTP

Returns a new instance of HTTP.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/net-http-spy.rb', line 16

def initialize(*args, &block)
  self.class.http_logger_options ||= {}
  defaults =  {:body => false, :trace => false, :verbose => false, :limit => -1}
  self.class.http_logger_options = (self.class.http_logger_options == :default) ? defaults : self.class.http_logger_options
  @logger_options = defaults.merge(self.class.http_logger_options)
  @params_limit = @logger_options[:params_limit] || @logger_options[:limit]
  @body_limit   = @logger_options[:body_limit]   || @logger_options[:limit]
  
  self.class.http_logger.info "CONNECT: #{args.inspect}" if !@logger_options[:verbose]

  old_initialize(*args, &block)
  @debug_output   = self.class.http_logger if @logger_options[:verbose]
end

Class Attribute Details

.http_loggerObject

Returns the value of attribute http_logger.



12
13
14
# File 'lib/net-http-spy.rb', line 12

def http_logger
  @http_logger
end

.http_logger_optionsObject

Returns the value of attribute http_logger_options.



13
14
15
# File 'lib/net-http-spy.rb', line 13

def http_logger_options
  @http_logger_options
end

Instance Method Details

#old_initializeObject



8
# File 'lib/net-http-spy.rb', line 8

alias :old_initialize :initialize

#old_requestObject



9
# File 'lib/net-http-spy.rb', line 9

alias :old_request :request

#request(*args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/net-http-spy.rb', line 31

def request(*args, &block)
  unless started? || @logger_options[:verbose]
    req = args[0].class::METHOD
    self.class.http_logger.info "#{req} #{args[0].path}"
  end

  result = old_request(*args, &block)
  unless started? || @logger_options[:verbose]

    self.class.http_logger.info "PARAMS #{CGI.parse(args[0].body).inspect[0..@params_limit]} " if args[0].body && req != 'CONNECT'
    self.class.http_logger.info "TRACE: #{caller.reverse}" if @logger_options[:trace]
    self.class.http_logger.info "BODY: #{(@logger_options[:body] ? result.body : result.class.name)[0..@body_limit]}"
  end
  result
end