class Desi::HttpClient
Public Class Methods
new(host)
click to toggle source
# File lib/desi/http_client.rb, line 11 def initialize(host) @uri = to_uri(host) case @uri.scheme when 'https' @http = ::Net::HTTP.new(@uri.host, 443) @http.use_ssl = true @http.verify_mode = ::OpenSSL::SSL::VERIFY_PEER when 'http' @http = ::Net::HTTP.new(@uri.host, @uri.port) else raise ArgumentError, "Won't process scheme #{@uri.scheme}" end end
Public Instance Methods
delete(uri)
click to toggle source
# File lib/desi/http_client.rb, line 52 def delete(uri) response = @http.request(Net::HTTP::Delete.new(uri)) case response when Net::HTTPSuccess response else raise response.error! end end
get(uri, limit = 5)
click to toggle source
# File lib/desi/http_client.rb, line 26 def get(uri, limit = 5) raise "Too many HTTP redirects!" if limit <= 0 response = @http.request(Net::HTTP::Get.new(uri)) case response when Net::HTTPSuccess response when Net::HTTPRedirection get(response['location'], limit - 1) else raise response.error! end end
get_file(path, destination)
click to toggle source
# File lib/desi/http_client.rb, line 63 def get_file(path, destination) Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == 'https') do |http| request = Net::HTTP::Get.new path http.request request do |response| open destination, 'w' do |io| response.read_body do |chunk| io.write chunk end end end end end
post(uri)
click to toggle source
# File lib/desi/http_client.rb, line 41 def post(uri) response = @http.request(Net::HTTP::Post.new(uri)) case response when Net::HTTPSuccess response else raise response.error! end end
Private Instance Methods
to_uri(host_string)
click to toggle source
# File lib/desi/http_client.rb, line 79 def to_uri(host_string) host_string = "http://#{host_string}" unless host_string.to_s =~ %r[^https?://] URI(host_string) end