Class: EtcdDiscovery::Service

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg, shard = nil) ⇒ Service

Returns a new instance of Service.



8
9
10
11
12
13
14
15
16
17
# File 'lib/etcd-discovery/service.rb', line 8

def initialize(arg, shard = nil)
  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
  @shard = shard
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#shardObject

Returns the value of attribute shard.



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

def shard
  @shard
end

Class Method Details

.get(service, shard: nil) ⇒ Object

Raises:

  • (TypeError)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/etcd-discovery/service.rb', line 19

def self.get(service, shard: nil)
  raise TypeError, "service should be a String, is a #{service.class}" unless service.is_a? String

  client = EtcdDiscovery.config.client
  begin
    service = client.get("/services_infos/#{service}")
    new(service.node, shard)
  rescue Etcd::KeyNotFound
    new({"name" => service}, shard)
  end
end

Instance Method Details

#allObject

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/etcd-discovery/service.rb', line 31

def all
  client = EtcdDiscovery.config.client

  begin
    node = client.get("/services/#{attributes["name"]}", recursive: true).node
  rescue Etcd::KeyNotFound
    raise ServiceNotFound, attributes["name"]
  end

  raise ServiceNotFound, attributes["name"] if node.children.empty?

  hosts = node.children.map { |c| Host.new(c) }
  return hosts if @shard.nil?

  hosts = hosts.select { |host| host.attributes["shard"].to_s == @shard.to_s }
  raise ServiceNotFound, attributes["name"] if hosts.empty?

  hosts
end

#oneObject



51
52
53
# File 'lib/etcd-discovery/service.rb', line 51

def one
  all.sample
end

#set_credentials(user, password) ⇒ Object



89
90
91
92
# File 'lib/etcd-discovery/service.rb', line 89

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

#to_jsonObject



85
86
87
# File 'lib/etcd-discovery/service.rb', line 85

def to_json
  attributes.to_json
end

#to_sObject



76
77
78
79
80
81
82
83
# File 'lib/etcd-discovery/service.rb', line 76

def to_s
  uri = to_uri
  if uri.userinfo
    user, _password = uri.userinfo.split(":", 2)
    uri.userinfo = "#{user}:REDACTED"
  end
  uri.to_s
end

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

Raises:

  • (TypeError)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/etcd-discovery/service.rb', line 55

def to_uri(schemes = ["https", "http"], **kwargs)
  unless kwargs.empty?
    raise TypeError, "to_uri does not accept keyword arguments; pass schemes as a String or an Array"
  end

  raise TypeError, "schemes should be a String or an Array" if schemes.is_a? Hash

  schemes = Array(schemes)

  return one.to_uri(schemes) if @shard || !attributes["public"]

  scheme = schemes.find { |s| attributes["ports"][s] }
  raise "No valid scheme found" unless scheme

  if attributes["user"].nil? || attributes["user"].empty?
    return URI("#{scheme}://#{attributes["hostname"]}:#{attributes["ports"][scheme]}")
  end

  URI("#{scheme}://#{attributes["user"]}:#{attributes["password"]}@#{attributes["hostname"]}:#{attributes["ports"][scheme]}")
end