1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

Make log_response a method of Response objects.

It didn't make much sense for log_response to be a method on the
Request object.

Also make start_time completely optional when creating Response objects.

And don't require a tempfile in process_result (only needed when
@raw_response is true).
This commit is contained in:
Andy Brody 2017-05-03 10:29:35 -04:00
parent deeeadf579
commit a892f6467f
2 changed files with 12 additions and 15 deletions

View file

@ -16,6 +16,16 @@ module RestClient
request.log
end
def log_response
return unless log
code = net_http_res.code
res_name = net_http_res.class.to_s.gsub(/\ANet::HTTP/, '')
content_type = (net_http_res['Content-type'] || '').gsub(/;.*\z/, '')
log << "# => #{code} #{res_name} | #{content_type} #{size} bytes, #{sprintf('%.2f', duration)}s\n"
end
# HTTP status code
def code
@code ||= @net_http_res.code.to_i
@ -48,7 +58,6 @@ module RestClient
if @start_time
@duration = @end_time - @start_time
else
warn 'deprecation warning: Response object created without start_time'
@duration = nil
end

View file

@ -553,18 +553,6 @@ module RestClient
log << out.join(', ') + "\n"
end
def log_response(net_http_res, response)
return unless log
duration = response.duration
code = net_http_res.code
res_name = net_http_res.class.to_s.split('::').last
content_type = (net_http_res['Content-type'] || '').gsub(/;.*\z/, '')
log << "# => #{code} #{res_name} | #{content_type} #{response.size} bytes, #{sprintf('%.2f', duration)}s\n"
end
# Return a hash of headers whose keys are capitalized strings
def stringify_headers headers
headers.inject({}) do |result, (key, value)|
@ -824,7 +812,7 @@ module RestClient
# @param res The Net::HTTP response object
# @param start_time [Time] Time of request start
def process_result(res, start_time, tempfile, &block)
def process_result(res, start_time, tempfile=nil, &block)
if @raw_response
# We don't decode raw requests
response = RawResponse.new(tempfile, res, self, start_time)
@ -833,7 +821,7 @@ module RestClient
response = Response.create(decoded, res, self, start_time)
end
log_response(res, response)
response.log_response
if block_given?
block.call(response, self, res, & block)