Project

General

Profile

Feature #14615

Consider introducing shortcut to check Net::HTTP response type

Added by artur86 (Artur Beljajev) about 1 month ago. Updated about 1 month ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:86192]

Description

Currently you need to either use kind_of? or HTTPResponse#code to find out the type of a response.

response = Net::HTTP.start(uri.hostname, 
                           uri.port,
                           use_ssl: (uri.scheme == 'https'),
                           cert: OpenSSL::X509::Certificate.new(client_cert),
                           key: OpenSSL::PKey::RSA.new(client_key)) do |http|
  http.request(request)
end

if response.code || response.kind_of?(Net::HTTPSuccess)
  ...
end

How about using data from either Rack::Utils::SYMBOL_TO_STATUS_CODE or Net::HTTPResponse::CODE_TO_OBJ to simply it to:

if response.ok?
...
end

in the same way as Rails does https://github.com/rails/rails/blob/2090615d39c071c9eb25e715275eb79f3f9b6266/actionpack/lib/action_dispatch/testing/assertions/response.rb#L18?

History

#1 Updated by artur86 (Artur Beljajev) about 1 month ago

  • Subject changed from Checking HTTP::Net response to Consider introducing shortcut to check Net::HTTP response type

#2 Updated by artur86 (Artur Beljajev) about 1 month ago

  • Description updated (diff)

#3 Updated by artur86 (Artur Beljajev) about 1 month ago

  • Description updated (diff)

#4 Updated by artur86 (Artur Beljajev) about 1 month ago

  • Description updated (diff)

#5 Updated by artur86 (Artur Beljajev) about 1 month ago

  • Backport deleted (2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN)
  • Tracker changed from Bug to Feature

#6 Updated by artur86 (Artur Beljajev) about 1 month ago

  • Description updated (diff)

#7 [ruby-core:86193] Updated by shevegen (Robert A. Heiler) about 1 month ago

I'd say it is ok to add .ok?

:D

What is missing is a description as to what the method ok? exactly
does. In the link that you provided it is unclear to which part
exactly you refer the "#ok?" method. It may be easier for whoever
adds the code to Net::HTTP to have a description ready what the
method does exactly, such as when to return which Boolean. (Here
provided that the ruby core team/matz is fine with this method).

#8 Updated by artur86 (Artur Beljajev) about 1 month ago

  • Description updated (diff)

Also available in: Atom PDF