Skip to content

Instantly share code, notes, and snippets.

@txus
Created October 27, 2014 15:50
Show Gist options
  • Save txus/3af162558378ca266cee to your computer and use it in GitHub Desktop.
Save txus/3af162558378ca266cee to your computer and use it in GitHub Desktop.
sigh
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