Class: Mongo::Address::IPv6
- Inherits:
-
Object
- Object
- Mongo::Address::IPv6
- Defined in:
- lib/mongo/address/ipv6.rb
Overview
Sets up resolution with IPv6 support if the address is an ip address.
Constant Summary collapse
- MATCH =
The regular expression to use to match an IPv6 ip address.
Regexp.new('::').freeze
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
Host The host.
-
#host_name ⇒ String
readonly
Host_name The original host name.
-
#port ⇒ Integer
readonly
Port The port.
Class Method Summary collapse
-
.parse(address) ⇒ Array<String, Integer>
Parse an IPv6 address into its host and port.
Instance Method Summary collapse
-
#initialize(host, port, host_name = nil) ⇒ IPv6
constructor
Initialize the IPv6 resolver.
-
#socket(socket_timeout, ssl_options = {}) ⇒ Mongo::Socket::SSL, Mongo::Socket::TCP
Get a socket for the provided address type, given the options.
Constructor Details
#initialize(host, port, host_name = nil) ⇒ IPv6
Initialize the IPv6 resolver.
85 86 87 88 89 |
# File 'lib/mongo/address/ipv6.rb', line 85 def initialize(host, port, host_name=nil) @host = host @port = port @host_name = host_name end |
Instance Attribute Details
#host ⇒ String (readonly)
Returns host The host.
27 28 29 |
# File 'lib/mongo/address/ipv6.rb', line 27 def host @host end |
#host_name ⇒ String (readonly)
Returns host_name The original host name.
30 31 32 |
# File 'lib/mongo/address/ipv6.rb', line 30 def host_name @host_name end |
#port ⇒ Integer (readonly)
Returns port The port.
33 34 35 |
# File 'lib/mongo/address/ipv6.rb', line 33 def port @port end |
Class Method Details
.parse(address) ⇒ Array<String, Integer>
Parse an IPv6 address into its host and port.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mongo/address/ipv6.rb', line 50 def self.parse(address) # IPAddr's parser handles IP address only, not port. # Therefore we need to handle the port ourselves if address =~ /[\[\]]/ parts = address.match(/\A\[(.+)\](?::(\d+))?\z/) if parts.nil? raise ArgumentError, "Invalid IPv6 address: #{address}" end host = parts[1] port = (parts[2] || 27017).to_i else host = address port = 27017 end # Validate host. # This will raise IPAddr::InvalidAddressError # on newer rubies which is a subclass of ArgumentError # if host is invalid begin IPAddr.new(host) rescue ArgumentError raise ArgumentError, "Invalid IPv6 address: #{address}" end [ host, port ] end |
Instance Method Details
#socket(socket_timeout, ssl_options = {}) ⇒ Mongo::Socket::SSL, Mongo::Socket::TCP
Get a socket for the provided address type, given the options.
102 103 104 105 106 107 108 |
# File 'lib/mongo/address/ipv6.rb', line 102 def socket(socket_timeout, = {}) unless .empty? Socket::SSL.new(host, port, host_name, socket_timeout, Socket::PF_INET6, ) else Socket::TCP.new(host, port, socket_timeout, Socket::PF_INET6) end end |