Sha256: 15f2b4768668904a4588f15a36e508940a5d134db163c1f80bf59941fe952cec

Contents?: true

Size: 1.05 KB

Versions: 12

Compression:

Stored size: 1.05 KB

Contents

require 'faraday'
require 'faraday_middleware'
require 'json'
require 'base64'

module EsReadModel

  class Connection

    def initialize(endpoint, username=nil, password=nil)
      @endpoint = endpoint
      @headers = {
        'Accept'       => 'application/json',
        'Content-Type' => 'application/json'
      }
      if username && password
        token = Base64.encode64("#{username}:#{password}")[0..-2]
        @headers.merge!({ 'Authorization' => "Basic #{token}" })
      end
    end

    def get(uri, etag)
      connection = Faraday.new(url: @endpoint) do |faraday|
        faraday.options[:timeout] = 2
        faraday.response :json, content_type: 'application/json'
        faraday.response :mashify
        faraday.adapter Faraday.default_adapter
      end
      response = connection.get(uri) do |req|
        req.headers = @headers
        req.headers.merge({ 'If-None-Match' => etag }) if etag
        req.body = {}.to_json
        req.params['embed'] = 'body'
      end
      response
    end

    def to_s
      @endpoint
    end

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
es-readmodel-1.0.2 lib/es_readmodel/connection.rb
es-readmodel-1.0.1 lib/es_readmodel/connection.rb
es-readmodel-1.0.0 lib/es_readmodel/connection.rb
es-readmodel-0.1.0 lib/es_readmodel/connection.rb
es-readmodel-0.0.10 lib/es_readmodel/connection.rb
es-readmodel-0.0.9 lib/es_readmodel/connection.rb
es-readmodel-0.0.8 lib/es_readmodel/connection.rb
es-readmodel-0.0.7 lib/es_readmodel/connection.rb
es-readmodel-0.0.6 lib/es_readmodel/connection.rb
es-readmodel-0.0.5 lib/es_readmodel/connection.rb
es-readmodel-0.0.4 lib/es_readmodel/connection.rb
es-readmodel-0.0.1 lib/es_readmodel/connection.rb