class EtcdDiscovery::Host

Attributes

attributes[RW]

Public Class Methods

new(arg) click to toggle source
# File lib/etcd-discovery/host.rb, line 8
def initialize(arg)
  if arg.is_a? Etcd::Node
    @attributes = JSON.parse arg.value
  elsif arg.is_a? Hash
    @attributes = arg
  else
    raise TypeError, "requires a Etcd::Node or a Hash, not a #{arg.class}"
  end
  if !attributes.has_key?("name") || !attributes.has_key?("ports")
    raise InvalidHost, "attributes 'name' and 'ports' should be defined"
  end
  attributes["user"] = "" if attributes["user"].nil?
  attributes["password"] = "" if attributes["password"].nil?
end

Public Instance Methods

set_credentials(user, password) click to toggle source
# File lib/etcd-discovery/host.rb, line 57
def set_credentials(user, password)
  @attributes["user"] = user
  @attributes["password"] = password
end
to_json() click to toggle source
# File lib/etcd-discovery/host.rb, line 23
def to_json
  attributes.to_json
end
to_private_uri(schemes = ["https", "http"]) click to toggle source
# File lib/etcd-discovery/host.rb, line 40
def to_private_uri(schemes = ["https", "http"])
  a = attributes
  if a["private_hostname"].empty?
    return to_uri(schemes)
  end
  schemes = [schemes] if !schemes.is_a?(Array)
  scheme = schemes.find { |s|
    !a["private_ports"][s].nil?
  }

  if a["user"].nil? || a["user"] == ""
    URI("#{scheme}://#{a["private_hostname"]}:#{a["private_ports"][scheme]}")
  else
    URI("#{scheme}://#{a["user"]}:#{a["password"]}@#{a["private_hostname"]}:#{a["private_ports"][scheme]}")
  end
end
to_s() click to toggle source
# File lib/etcd-discovery/host.rb, line 62
def to_s
  to_uri.to_s
end
to_uri(schemes = ["https", "http"]) click to toggle source
# File lib/etcd-discovery/host.rb, line 27
def to_uri(schemes = ["https", "http"])
  a = attributes # Shorten name
  schemes = [schemes] if !schemes.is_a?(Array)
  scheme = schemes.find { |s|
    !a["ports"][s].nil?
  }
  if a["user"].empty?
    URI("#{scheme}://#{a["name"]}:#{a["ports"][scheme]}")
  else
    URI("#{scheme}://#{a["user"]}:#{a["password"]}@#{a["name"]}:#{a["ports"][scheme]}")
  end
end