class ElasticSearchClient

Attributes

index[R]
server[R]

Public Class Methods

new() click to toggle source
# File lib/mas/elastic_search_client.rb, line 4
def initialize
  @index  = "rad_#{Rails.env}"
  @server = ENV.fetch('BONSAI_URL', 'http://localhost:9200')
end

Public Instance Methods

delete(path) click to toggle source
# File lib/mas/elastic_search_client.rb, line 28
def delete(path)
  log("DELETE /#{path}")

  res = http.delete(uri_for(path))
  res.ok?
end
find(path) click to toggle source
# File lib/mas/elastic_search_client.rb, line 22
def find(path)
  log("GET /#{path}")

  http.get(uri_for(path))
end
store(path, json) click to toggle source
# File lib/mas/elastic_search_client.rb, line 9
def store(path, json)
  log("PUT /#{path}\nRequest Body: #{json}")

  res = http.put(uri_for(path), JSON.generate(json))
  res.ok?
end

Private Instance Methods

authenticate?() click to toggle source
# File lib/mas/elastic_search_client.rb, line 49
def authenticate?
  username && password
end
http() click to toggle source
# File lib/mas/elastic_search_client.rb, line 37
def http
  @http ||= begin
    HTTPClient.new.tap do |c|
      c.set_auth(server, username, password) if authenticate?
    end
  end
end
log(message) click to toggle source
# File lib/mas/elastic_search_client.rb, line 45
def log(message)
  Rails.logger.debug("ElasticSearch Request: #{message}")
end
password() click to toggle source
# File lib/mas/elastic_search_client.rb, line 57
def password
  ENV['BONSAI_PASSWORD']
end
uri_for(path) click to toggle source
# File lib/mas/elastic_search_client.rb, line 61
def uri_for(path)
  "#{server}/#{index}/#{path}"
end
username() click to toggle source
# File lib/mas/elastic_search_client.rb, line 53
def username
  ENV['BONSAI_USERNAME']
end