【发布时间】:2016-12-05 05:44:24
【问题描述】:
我正在尝试自定义记录 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
问题是我需要在Net::HTTP 类中使用alias_method,并且不允许在方法中定义类。
这是方法定义。
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
【问题讨论】:
标签: ruby-on-rails ruby http request net-http