http 요청을 사용자 지정 기록하려고합니다. 아이디어는 요청이 기록되는 기능을 정의하는 것입니다.Ruby : 사용자 지정 메서드 내에서 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