Skip to content

Instantly share code, notes, and snippets.

@jagdeepsingh
Created December 27, 2015 13:50
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 jagdeepsingh/0e097d4895afc4588481 to your computer and use it in GitHub Desktop.
Save jagdeepsingh/0e097d4895afc4588481 to your computer and use it in GitHub Desktop.
Ruby service to send http requests
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