Skip to content

Instantly share code, notes, and snippets.

@nicklegr
Last active March 13, 2020 08:23
Show Gist options
  • Select an option

  • Save nicklegr/f772ca2f8f861ddf504f13fb2606ce7c to your computer and use it in GitHub Desktop.

Select an option

Save nicklegr/f772ca2f8f861ddf504f13fb2606ce7c to your computer and use it in GitHub Desktop.
Simple net/http wrapper
require "net/https"
class Http
def self.get(url, header, params = {})
uri = URI(url)
uri.query = URI.encode_www_form(params)
http = Net::HTTP.new(uri.hostname, uri.port)
# https.proxy_from_env = true
if uri.port == 443
http.use_ssl = true
# http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
res = http.start do
req = Net::HTTP::Get.new(uri, header)
http.request(req)
end
case res
when Net::HTTPSuccess, Net::HTTPRedirection
res.body
else
# レスポンスが 2xx(成功)でなかった場合に、対応する例外を発生させます。
res.value
end
end
def self.post(url, header, params)
uri = URI(url)
http = Net::HTTP.new(uri.hostname, uri.port)
# https.proxy_from_env = true
if uri.port == 443
http.use_ssl = true
# http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
res = http.start do
req = Net::HTTP::Post.new(uri, header)
req.set_form_data(params)
http.request(req)
end
case res
when Net::HTTPSuccess, Net::HTTPRedirection
res.body
else
# レスポンスが 2xx(成功)でなかった場合に、対応する例外を発生させます。
res.value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment