Skip to content

Instantly share code, notes, and snippets.

@yock
Last active August 29, 2015 14:09
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 yock/479d656681b45000926d to your computer and use it in GitHub Desktop.
Save yock/479d656681b45000926d to your computer and use it in GitHub Desktop.
Thinking Outside the Block v2
require 'net/http'
response = Net::HTTP.start 'api.example.com', 80 { |http|
http.get '/endpoint'
}
response_base_class = response.class.name.split('::').last
begin
handler_class = Object.const_get("ApiResponse::#{response_base_class}")
handler_class.new(response).handle
rescue NameError => e
ApiResponse::DefaultHandler.new(response).handle
end
module ApiResponse
class DefaultHandler
# ...
end
class HTTPOK
# ...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment