Created
December 27, 2015 13:50
-
-
Save jagdeepsingh/0e097d4895afc4588481 to your computer and use it in GitHub Desktop.
Ruby service to send http requests
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' | |
class HttpRequestService | |
attr_reader :uri | |
def do_post(url, headers, data) | |
set_uri(url) | |
post = build_post(uri, headers, data) | |
net_http(uri).start { |http| http.request(post) } | |
end | |
private | |
def set_uri(url) | |
@uri = URI.parse(url) | |
end | |
def net_http(uri) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.read_timeout = 500 | |
http | |
end | |
def build_post(uri, headers, data) | |
post = Net::HTTP::Post.new(uri.path, headers) | |
post.body = data.to_json | |
post | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment