lib/kamerling/addr.rb in kamerling-0.0.2 vs lib/kamerling/addr.rb in kamerling-0.0.3
- old
+ new
@@ -1,7 +1,17 @@
+require 'socket'
+require 'uri'
+require_relative 'value'
+
module Kamerling
- Addr = Struct.new :host, :port, :prot do
+ class Addr < Value
+ vals host: String, port: Integer, prot: Symbol
+
+ def self.[](host, port, prot)
+ new(host: host, port: port, prot: prot)
+ end
+
def connectable?
TCPSocket.open(*self).close
true
rescue Errno::ECONNREFUSED
false
@@ -10,13 +20,17 @@
def to_a
[host, port]
end
def to_h
- super.merge prot: prot.to_s
+ attributes.merge(prot: prot.to_s)
end
def to_s
- "#{host}:#{port} (#{prot})"
+ uri.to_s
+ end
+
+ def uri
+ URI.parse("#{prot.downcase}://#{host}:#{port}")
end
end
end