0
我想自定义记录HTTP请求中的HTTP请求。这个想法是定义一个记录请求的函数。红宝石:录制一个自定义的方法
recorded :get, :post, :patch, :delete do
Net::HTTP.get(URI('http://www.google.com')) #will log
end
Net::HTTP.get(URI('http://news.ycombinator.com')) # will not log
的问题是,我需要alias_method
的Net::HTTP
类的内部,并限定一个方法中的一类是不允许的。
下面是该方法的定义。
require 'net/http'
def recorded(*methods, &block)
# module Net
# class HTTP
# alias_method(:orig_request, :request) unless method_defined?(:orig_request)
# alias_method(:orig_connect, :connect) unless method_defined?(:orig_connect)
#
# def request(req, body = nil, &block)
# if started?
# puts :started
# end
#
# @response = orig_request(req, body, &block)
# puts @response.code
# # LOGGING HERE THE REQUEST
#
# @response
# end
#
# def connect
# orig_connect
# end
# end
# end
yield block
end