Sha256: 484eecb5d91687c7c9a5de88ac4af6f3bafc88c4a0dee0c0648e05eee3512517

Contents?: true

Size: 1.36 KB

Versions: 14

Compression:

Stored size: 1.36 KB

Contents

module SearchFlip
  # 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, :plugins

    def initialize(plugins: [])
      self.request = HTTP
      self.plugins = plugins
    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 |uri, options = {}|
        execute(method, uri, options)
      end
    end

    private

    def execute(method, uri, options = {})
      final_request = plugins.inject(self) { |res, cur| cur.call(res, method, uri, options) }
      response = final_request.request.send(method, uri, options)

      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

14 entries across 14 versions & 1 rubygems

Version Path
search_flip-3.3.0 lib/search_flip/http_client.rb
search_flip-4.0.0.beta7 lib/search_flip/http_client.rb
search_flip-4.0.0.beta6 lib/search_flip/http_client.rb
search_flip-4.0.0.beta5 lib/search_flip/http_client.rb
search_flip-4.0.0.beta4 lib/search_flip/http_client.rb
search_flip-3.2.1 lib/search_flip/http_client.rb
search_flip-3.2.0 lib/search_flip/http_client.rb
search_flip-4.0.0.beta3 lib/search_flip/http_client.rb
search_flip-4.0.0.beta2 lib/search_flip/http_client.rb
search_flip-4.0.0.beta1 lib/search_flip/http_client.rb
search_flip-4.0.0.beta lib/search_flip/http_client.rb
search_flip-3.1.2 lib/search_flip/http_client.rb
search_flip-3.1.1 lib/search_flip/http_client.rb
search_flip-3.1.0 lib/search_flip/http_client.rb