Skip to content

Instantly share code, notes, and snippets.

@alexandru-calinoiu
Last active May 22, 2017 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandru-calinoiu/56fead03c5bf50e4f3de6bb9eea620c7 to your computer and use it in GitHub Desktop.
Save alexandru-calinoiu/56fead03c5bf50e4f3de6bb9eea620c7 to your computer and use it in GitHub Desktop.
My take on resilience inspired by https://johnnunemaker.com/resilience-in-ruby/
class Client
class NotificationsResponse
attr_reader :content, :error
def self.build(&block)
error = false
content = begin
yield
rescue Errno::ECONNREFUSED => exception
error = exception
{} # sensible default
end
self.new(content, error)
end
def initialize(content, error)
@error = error
@content = content
end
def ok?
@error == false
end
end
def notifications
NotificationsResponse.build do
request = Net::HTTP::Get.new('/')
http = Net::HTTP.new('localhost', 9999)
response = http.request(request)
JSON.parse(response.body)
end
end
end
client = Client.new
response = client.notifications
if response.ok?
# do something worderfull
else
p "Failed: #{response.error}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment