Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/moped/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ class Address
# @since 2.0.0
def initialize(address, timeout)
@original = address
@host, port = address.split(":")
@port = (port || 27017).to_i

if (parts = address.match(/\[(.+)\]:?(.+)?/))
@host = parts[1]
@port = (parts[2] || 27017).to_i
else
@host, port = address.split(":")
@port = (port || 27017).to_i
end

@timeout = timeout
end

Expand All @@ -49,7 +56,7 @@ def resolve(node)
return @resolved if @resolved
Timeout::timeout(@timeout) do
Resolv.each_address(host) do |ip|
if ip =~ Resolv::IPv4::Regex
if ip =~ Resolv::IPv4::Regex || ip =~ Resolv::IPv6::Regex
@ip ||= ip
break
end
Expand Down
4 changes: 2 additions & 2 deletions spec/moped/address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@
end

it "sets the resolved address" do
expect(address.resolved).to eq("127.0.0.1:27017")
expect(address.resolved).to satisfy { |a| a == "127.0.0.1:27017" || a == "::1:27017" }
end

it "sets the ip" do
expect(address.ip).to eq("127.0.0.1")
expect(address.ip).to satisfy { |a| a == "127.0.0.1" || a == "::1" }
end
end

Expand Down