Sha256: d27052811a70b5465466a81ac2e4f95e8f753fb903c1ed510a3157bc0ec7eb66

Contents?: true

Size: 1.22 KB

Versions: 14

Compression:

Stored size: 1.22 KB

Contents

# encoding: utf-8

require "net/https"
require "addressable/uri"
require "json"

module Desi

  class HttpClient

    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

    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

    def delete(uri)
      response = @http.request(Net::HTTP::Delete.new(uri))

      case response
        when Net::HTTPSuccess
          response
        else
          raise response.error!
      end
    end

    private

    def to_uri(host_string)
      host_string = "http://#{host_string}" unless host_string.to_s =~ %r[^https?://]
      URI(host_string)
    end
  end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
desi-0.6.9 lib/desi/http_client.rb
desi-0.6.8 lib/desi/http_client.rb
desi-0.6.7 lib/desi/http_client.rb
desi-0.6.6 lib/desi/http_client.rb
desi-0.6.5 lib/desi/http_client.rb
desi-0.6.4 lib/desi/http_client.rb
desi-0.6.2 lib/desi/http_client.rb
desi-0.6.1 lib/desi/http_client.rb
desi-0.6.0 lib/desi/http_client.rb
desi-0.5.0 lib/desi/http_client.rb
desi-0.4.0 lib/desi/http_client.rb
desi-0.3.0 lib/desi/http_client.rb
desi-0.2.9 lib/desi/http_client.rb
desi-0.2.8 lib/desi/http_client.rb