Sha256: 30ceca96850c032617d00338c6de27d4abe68a0e8905c4e5fc696b60eeb540da

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 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
    class Request
      attr_accessor :headers_hash

      def headers(hash = {})
        dup.tap do |request|
          request.headers_hash = (request.headers_hash || {}).merge(hash)
        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 = HTTP.headers(headers_hash || {}).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

    def self.request
      Request.new
    end

    class << self
      extend Forwardable

      def_delegators :request, :headers, :get, :post, :put, :delete, :head
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
search_flip-2.0.0.beta2 lib/search_flip/http_client.rb
search_flip-2.0.0.beta lib/search_flip/http_client.rb
search_flip-1.1.0 lib/search_flip/http_client.rb
search_flip-1.0.0 lib/search_flip/http_client.rb