Skip to content

Instantly share code, notes, and snippets.

@paulanunda
Created May 16, 2017 20:08
Show Gist options
  • Save paulanunda/6202d668ff058b6b8ad600be14f26e9d to your computer and use it in GitHub Desktop.
Save paulanunda/6202d668ff058b6b8ad600be14f26e9d to your computer and use it in GitHub Desktop.
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