Class: Neo4j::Driver::Internal::BoltServerAddress
- Inherits:
-
Object
- Object
- Neo4j::Driver::Internal::BoltServerAddress
- Defined in:
- lib/neo4j/driver/internal/bolt_server_address.rb
Overview
Holds a host and port pair that denotes a Bolt server address.
Constant Summary collapse
- DEFAULT_PORT =
7687
- LOCAL_DEFAULT =
new(host: 'localhost', port: DEFAULT_PORT)
Instance Attribute Summary collapse
-
#connection_host ⇒ Object
readonly
Returns the value of attribute connection_host.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
- .from(address) ⇒ Object
- .host_from(uri) ⇒ Object
- .host_port_from(address) ⇒ Object
- .invalid_address_format(address) ⇒ Object
- .port_from(uri) ⇒ Object
- .require_valid_port(port) ⇒ Object
- .uri_from(address) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #attributes ⇒ Object
-
#initialize(uri: nil, host: self.class.host_from(uri), port: self.class.port_from(uri), connection_host: host) ⇒ BoltServerAddress
constructor
A new instance of BoltServerAddress.
- #to_s ⇒ Object
-
#unicast_stream ⇒ Object
Stream of unicast addresses.
Constructor Details
#initialize(uri: nil, host: self.class.host_from(uri), port: self.class.port_from(uri), connection_host: host) ⇒ BoltServerAddress
Returns a new instance of BoltServerAddress.
61 62 63 64 65 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 61 def initialize(uri: nil, host: self.class.host_from(uri), port: self.class.port_from(uri), connection_host: host) @host = Validator.require_non_nil!(host) @connection_host = Validator.require_non_nil!(connection_host) @port = self.class.require_valid_port(port) end |
Instance Attribute Details
#connection_host ⇒ Object (readonly)
Returns the value of attribute connection_host.
5 6 7 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 5 def connection_host @connection_host end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
5 6 7 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 5 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
5 6 7 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 5 def port @port end |
Class Method Details
.from(address) ⇒ Object
69 70 71 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 69 def self.from(address) address.is_a?(BoltServerAddress) ? address : new(host: address.host, port: address.port) end |
.host_from(uri) ⇒ Object
11 12 13 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 11 def host_from(uri) uri&.host || (raise invalid_address_format(uri)) end |
.host_port_from(address) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 36 def host_port_from(address) # expected to be an IPv6 address like [::1] or [::1]:7687 return address if address.start_with?('[') contains_single_colon = address.index(':') == address.rindex(':') # expected to be an IPv4 address with or without port like 127.0.0.1 or 127.0.0.1:7687 return address if contains_single_colon # address contains multiple colons and does not start with '[' # expected to be an IPv6 address without brackets "[#{address}]" end |
.invalid_address_format(address) ⇒ Object
50 51 52 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 50 def invalid_address_format(address) ArgumentError.new("Invalid address format #{address}") end |
.port_from(uri) ⇒ Object
15 16 17 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 15 def port_from(uri) uri&.port || DEFAULT_PORT end |
.require_valid_port(port) ⇒ Object
54 55 56 57 58 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 54 def require_valid_port(port) return port if port >= 0 && port <= 65_535 raise ArgumentError, "Illegal port: #{port}" end |
.uri_from(address) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 19 def uri_from(address) scheme_split = address.split('://') if scheme_split.length == 1 # URI can't parse addresses without scheme, prepend fake "bolt://" to reuse the parsing facility scheme = 'bolt://' host_port = host_port_from(scheme_split.first) elsif scheme_split.length == 2 scheme = "#{scheme_split.first}://" host_port = host_port_from(scheme_split.second) else raise invalid_address_format(address) end URI(scheme + host_port) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
73 74 75 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 73 def ==(other) attributes == other&.attributes end |
#attributes ⇒ Object
92 93 94 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 92 def attributes [@host, @connection_host, @port] end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 79 def to_s "#{host}#{"(#{connection_host})" unless host == connection_host}:#{port}" end |
#unicast_stream ⇒ Object
Returns stream of unicast addresses.
88 89 90 |
# File 'lib/neo4j/driver/internal/bolt_server_address.rb', line 88 def unicast_stream Set[self] end |