Class: EtcdDiscovery::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/etcd-discovery/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Host

Returns a new instance of Host.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/etcd-discovery/host.rb', line 6

def attributes
  @attributes
end

Instance Method Details

#set_credentials(user, password) ⇒ Object



57
58
59
60
# File 'lib/etcd-discovery/host.rb', line 57

def set_credentials(user, password)
  @attributes["user"] = user
  @attributes["password"] = password
end

#to_jsonObject



23
24
25
# File 'lib/etcd-discovery/host.rb', line 23

def to_json
  attributes.to_json
end

#to_private_uri(schemes = ["https", "http"]) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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_sObject



62
63
64
# File 'lib/etcd-discovery/host.rb', line 62

def to_s
  to_uri.to_s
end

#to_uri(schemes = ["https", "http"]) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# 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