Class: EsReadModel::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/es_readmodel/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, username = nil, password = nil) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/es_readmodel/connection.rb', line 10

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

Instance Method Details

#get(uri, etag) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/es_readmodel/connection.rb', line 22

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

#to_sObject



38
39
40
# File 'lib/es_readmodel/connection.rb', line 38

def to_s
  @endpoint
end