Sha256: d8139af7a851748e137a430e18950590539ebe1a35872750261f65c0fee186e1

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 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

      ruby2_keywords method
    end

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

      ruby2_keywords method
    end

    private

    ruby2_keywords 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

2 entries across 2 versions & 1 rubygems

Version Path
search_flip-3.0.0 lib/search_flip/http_client.rb
search_flip-3.0.0.beta6 lib/search_flip/http_client.rb