Skip to content

Instantly share code, notes, and snippets.

@sirupsen
Created April 3, 2014 19:29
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 sirupsen/9961145 to your computer and use it in GitHub Desktop.
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.
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