Created
January 7, 2017 15:47
-
-
Save mwiegant/1146f003eb9d98502acf792cd7b8b92e to your computer and use it in GitHub Desktop.
Ruby HTTP API for making web requests
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 'net/http' | |
require 'json' | |
require 'uri' | |
=begin | |
@about: API for making web requests | |
@author: Max Wiegant | |
@revisions: | |
[20161223] - First Draft | |
[20170107] - v1.0.0 completed | |
=end | |
class Request | |
# @about: Function for making HTTP GET requests | |
# | |
# @param url - fully formed string representing the request url | |
# @param headers - hash object with all headers in the form: "" => "" | |
def self.HttpGet(url, headers, debug=false) | |
uri = URI.parse(url) | |
request = Net::HTTP::Get.new(uri) | |
request.initialize_http_header( headers ) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if debug | |
response = http.set_debug_output( $stdout ) | |
end | |
response = http.request(request) | |
if response.code.to_i == 200 | |
return response.code.to_i, response.body | |
else | |
return response.code.to_i, response.message | |
end | |
end | |
# @about: Function for making HTTP PUT requests | |
# | |
# @param url - fully formed string representing the request url | |
# @param headers - hash object with all headers in the form: "" => "" | |
# @param body - hash object with all body parameters in the form: "" => "" | |
def self.HttpPut(url, headers, body, debug=false) | |
uri = URI.parse(url) | |
request = Net::HTTP::Put.new(uri) | |
request.initialize_http_header( headers ) | |
request.body = JSON.generate(body) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if debug | |
response = http.set_debug_output( $stdout ) | |
end | |
response = http.request(request) | |
if response.code.to_i == 200 | |
return response.code.to_i, response.body | |
else | |
return response.code.to_i, response.message | |
end | |
end | |
# @about: Function for making HTTP POST requests | |
# | |
# @param url - fully formed string representing the request url | |
# @param headers - hash object with all headers in the form: "" => "" | |
# @param body - hash object with all body parameters in the form: "" => "" | |
def self.HttpPost(url, headers, body, debug=false) | |
uri = URI.parse(url) | |
request = Net::HTTP::Post.new(uri) | |
request.initialize_http_header( headers ) | |
request.body = JSON.generate(body) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if debug | |
response = http.set_debug_output( $stdout ) | |
end | |
response = http.request(request) | |
if response.code.to_i == 200 | |
return response.code.to_i, response.body | |
else | |
return response.code.to_i, response.message | |
end | |
end | |
# @about: Function for making HTTP DELETE requests | |
# | |
# @param url - fully formed string representing the request url | |
# @param headers - hash object with all headers in the form: "" => "" | |
def self.HttpDelete(url, headers, debug=false) | |
uri = URI.parse(url) | |
request = Net::HTTP::Delete.new(uri) | |
request.initialize_http_header( headers ) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if debug | |
response = http.set_debug_output( $stdout ) | |
end | |
response = http.request(request) | |
if response.code.to_i == 200 | |
return response.code.to_i, response.body | |
else | |
return response.code.to_i, response.message | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment