Created
May 16, 2017 20:08
-
-
Save paulanunda/6202d668ff058b6b8ad600be14f26e9d to your computer and use it in GitHub Desktop.
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 "net/http" | |
class Client | |
class NotificationsResponse | |
attr_reader :error | |
def initialize(&block) | |
@error = false | |
@notifications = begin | |
yield | |
rescue Errno::ECONNREFUSED => error | |
@error = error | |
{} # sensible default | |
end | |
end | |
def ok? | |
@ok_predicate_checked = true | |
@error == false | |
end | |
def notifications | |
unless @ok_predicate_checked | |
raise "ok? must be checked prior to accessing response data" | |
end | |
@notifications | |
end | |
end | |
def notifications | |
NotificationsResponse.new 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 | |
# response.notifications would raise error because ok? was not checked |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment