Method: Gem::Net::HTTP.new

Defined in:
lib/rubygems/vendor/net-http/lib/net/http.rb

.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil) ⇒ Object

Returns a new Gem::Net::HTTP object http (but does not open a TCP connection or HTTP session).

With only string argument address given (and ENV['http_proxy'] undefined or nil), the returned http:

  • Has the given address.

  • Has the default port number, Gem::Net::HTTP.default_port (80).

  • Has no proxy.

Example:

http = Gem::Net::HTTP.new(hostname)
# => #<Gem::Net::HTTP jsonplaceholder.typicode.com:80 open=false>
http.address # => "jsonplaceholder.typicode.com"
http.port    # => 80
http.proxy?  # => false

With integer argument port also given, the returned http has the given port:

http = Gem::Net::HTTP.new(hostname, 8000)
# => #<Gem::Net::HTTP jsonplaceholder.typicode.com:8000 open=false>
http.port # => 8000

For proxy-defining arguments p_addr through p_no_proxy, see Proxy Server.



1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'lib/rubygems/vendor/net-http/lib/net/http.rb', line 1106

def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil)
  http = super address, port

  if proxy_class? then # from Gem::Net::HTTP::Proxy()
    http.proxy_from_env = @proxy_from_env
    http.proxy_address  = @proxy_address
    http.proxy_port     = @proxy_port
    http.proxy_user     = @proxy_user
    http.proxy_pass     = @proxy_pass
    http.proxy_use_ssl  = @proxy_use_ssl
  elsif p_addr == :ENV then
    http.proxy_from_env = true
  else
    if p_addr && p_no_proxy && !Gem::URI::Generic.use_proxy?(address, address, port, p_no_proxy)
      p_addr = nil
      p_port = nil
    end
    http.proxy_address = p_addr
    http.proxy_port    = p_port || default_port
    http.proxy_user    = p_user
    http.proxy_pass    = p_pass
    http.proxy_use_ssl = p_use_ssl
  end

  http
end