Method: Addressable::URI#port=

Defined in:
lib/addressable/uri.rb

#port=(new_port) ⇒ Object

Sets the port component for this URI.

Parameters:

  • new_port (String, Integer, #to_s)

    The new port component.



1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
# File 'lib/addressable/uri.rb', line 1408

def port=(new_port)
  if new_port != nil && new_port.respond_to?(:to_str)
    new_port = Addressable::URI.unencode_component(new_port.to_str)
  end

  if new_port.respond_to?(:valid_encoding?) && !new_port.valid_encoding?
    raise InvalidURIError, "Invalid encoding in port"
  end

  if new_port != nil && !(new_port.to_s =~ /^\d+$/)
    raise InvalidURIError,
      "Invalid port number: #{new_port.inspect}"
  end

  @port = new_port.to_s.to_i
  @port = nil if @port == 0

  # Reset dependent values
  @authority = nil
  @normalized_port = NONE
  remove_composite_values

  # Ensure we haven't created an invalid URI
  validate()
end