Sha256: 712e8b03ad58443ec31291757a1ce2c33dc195296f4b51e02eef58d52187b266

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

module SearchFlip
  # @api private
  #
  # The SearchFlip::HTTPClient class wraps the http gem, is for internal use
  # and responsible for the http request/response handling, ie communicating
  # with Elasticsearch.

  class HTTPClient
    attr_accessor :request

    def initialize
      self.request = HTTP
    end

    class << self
      extend Forwardable

      def_delegators :new, :headers, :via, :basic_auth, :auth
      def_delegators :new, :get, :post, :put, :delete, :head
    end

    [:headers, :via, :basic_auth, :auth].each do |method|
      define_method method do |*args|
        dup.tap do |client|
          client.request = request.send(method, *args)
        end
      end
    end

    [:get, :post, :put, :delete, :head].each do |method|
      define_method method do |*args|
        execute(method, *args)
      end
    end

    private

    def execute(method, *args)
      response = request.send(method, *args)

      raise SearchFlip::ResponseError.new(code: response.code, body: response.body.to_s) unless response.status.success?

      response
    rescue HTTP::ConnectionError => e
      raise SearchFlip::ConnectionError, e.message
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
search_flip-3.0.0.beta5 lib/search_flip/http_client.rb
search_flip-3.0.0.beta4 lib/search_flip/http_client.rb
search_flip-3.0.0.beta3 lib/search_flip/http_client.rb
search_flip-3.0.0.beta2 lib/search_flip/http_client.rb
search_flip-3.0.0.beta1 lib/search_flip/http_client.rb
search_flip-3.0.0.beta lib/search_flip/http_client.rb