Created
October 27, 2014 15:50
-
-
Save txus/3af162558378ca266cee to your computer and use it in GitHub Desktop.
sigh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'verkehr/zookeeper' | |
module Verkehr | |
class Brokers | |
Broker = Struct.new(:id, :host, :port) do | |
def to_s | |
"#{host}:#{port}" | |
end | |
alias inspect to_s | |
end | |
class << self | |
def to_a # Array<Broker> | |
broker_ids.map(&method(:to_broker)).compact | |
end | |
def broker_ids # Either<Array<String>, Exception> | |
Verkehr::Zookeeper.get_children(path: "/brokers/ids")[:children] | |
end | |
def to_broker(id) # String -> Maybe<Broker> | |
data = Verkehr::Zookeeper.get(path: "/brokers/ids/#{id}") # Either<Hash<Any, Any>, Exception> | |
data[:data] # Maybe<String> | |
json = JSON.parse(data[:data]) # Maybe<JSONObject> | |
host = json["host"] # Maybe<String> | |
port = json["port"] # Maybe<Number> | |
if host && port | |
Broker.new(id, host, port) # Broker | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment