Last active
May 22, 2017 05:39
-
-
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/
This file contains hidden or 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
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