Sha256: 0abb664055a1208e515e7b749a6b27602591b3b7d3bca7817ecb7d26d83a35b6

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# encoding: utf-8

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

module Desi

  class HttpClient

    def initialize(host_string)
      @uri = to_uri(host_string)

      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
          fetch(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)
      scheme, host, port = ['http', '127.0.0.1', 9200]

      %r{(?<scheme>(https?|))(?:\:\/\/|)(?<host>[^:]*?):?(?<port>\d*)/?$}.match(host_string) do |m|
        scheme = m[:scheme] unless m[:scheme].empty?
        host = m[:host] unless m[:host].empty?
        port = m[:port] unless m[:port].empty?
      end

      Addressable::URI.new(scheme: scheme, host: host, port: port)
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
desi-0.2.1 lib/desi/http_client.rb
desi-0.2.0 lib/desi/http_client.rb