Created
April 3, 2014 19:29
-
-
Save sirupsen/9961145 to your computer and use it in GitHub Desktop.
Ever-evolving light wrapper on Net::HTTP instead of always reaching for a cheatsheet.
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
require 'net/http' | |
require 'uri' | |
require 'json' | |
class ProperHTTP | |
def self.get(uri) | |
handle_response Net::HTTP.get_response(URI.parse(uri)) | |
end | |
def self.post(uri, payload) | |
handle_response Net::HTTP.post_form(URI.parse(uri), payload) | |
end | |
def self.handle_response(response) | |
return JSON.parse(response.body, symbolize_names: true) if response.content_type == "application/json" | |
response.body | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment