(#27) Handle URLs that do not have HTTP or HTTPS

Prior to this commit, if a user provided a url without HTTP or HTTPS
Faraday would fail to make a connection to the pooler since it doesn't
seem to handle urls without that. This commit adds a simple check to see
what kind of URI the user gave us, and if its missing that protocol add
it to the beginning of the URL before making a request.
This commit is contained in:
Brian Cain 2016-08-26 10:08:38 -07:00
parent 93e842a2aa
commit 9a1d4a15d6

View file

@ -1,11 +1,30 @@
require 'faraday' require 'faraday'
require 'uri'
class Http class Http
def self.is_url(url)
# This method exists because it seems like Farady
# has no handling around if a user gives us a URI
# with no protocol on the beginning of the URL
uri = URI.parse(url)
if uri.kind_of?(URI::HTTP) or uri.kind_of?(URI::HTTPS)
return true
end
return false
end
def self.get_conn(verbose, url) def self.get_conn(verbose, url)
if url.nil? if url.nil?
raise "Did not provide a url to connect to" raise "Did not provide a url to connect to"
end end
unless is_url(url)
url = "https://#{url}"
end
conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday|
faraday.request :url_encoded faraday.request :url_encoded
faraday.response :logger if verbose faraday.response :logger if verbose
@ -24,6 +43,10 @@ class Http
raise "You did not provide a user to authenticate with" raise "You did not provide a user to authenticate with"
end end
unless is_url(url)
url = "https://#{url}"
end
conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday|
faraday.request :url_encoded faraday.request :url_encoded
faraday.request :basic_auth, user, password faraday.request :basic_auth, user, password