class Flubber

Why Flubber?

Public Class Methods

new(a) click to toggle source
# File lib/flubber.rb, line 14
def initialize(a)
   @attr = { url: ENV['ELASTICSEARCH_URL'], index: 'test', type: 'test' }
   if !a.nil? then
      self.default(a) if a.class == Hash
      @attr[:url] = a if a.class == String
   end
   @client = Elasticsearch::Client.new url: @attr[:url]
end

Public Instance Methods

client() click to toggle source
# File lib/flubber.rb, line 23
def client
   return @client
end
create(a, j) click to toggle source
# File lib/flubber.rb, line 55
def create(a, j)
   q = setup(a)
   j = j.to_s if j.respond_to?(:to_s)
   q[:body] = j
   @client.index q
end
default(a) click to toggle source

Replace defaults

# File lib/flubber.rb, line 37
def default(a)
   a.keys.each { |k| self.setval(@attr, a, k) }
end
delete(a) click to toggle source
# File lib/flubber.rb, line 72
def delete(a)
  @client.delete setup(a)
end
info() click to toggle source
# File lib/flubber.rb, line 41
def info
  @client.info 
end
read(a) click to toggle source
# File lib/flubber.rb, line 62
def read(a)
  @client.get setup(a)
end
setup(a) click to toggle source
# File lib/flubber.rb, line 45
def setup(a)
  q = { index: @attr[:index], type: @attr[:type] }
  if a.class == Hash then
    [:index, :type, :id].each { |k| self.setval(q, a, k) }
  else
    q[:id] = a.to_s
  end
  return q
end
setval(dest, source, name) click to toggle source

Copy key value from one hash to another, forcing key to sym, only if it exists

# File lib/flubber.rb, line 28
def setval(dest, source, name)
   return unless source.class == Hash
   return if source[name].nil? and source[name.to_sym].nil?
   dest[name.to_sym] = source[name].to_s unless source[name].nil?
   dest[name.to_sym] = source[name.to_sym].to_s unless source[name.to_sym].nil?
   dest[name.to_sym] = source[name.to_s].to_s unless source[name.to_s].nil?
end
update(a, j) click to toggle source
# File lib/flubber.rb, line 66
def update(a, j) # maybe add modify() that only changes some fields? hmmm.
   q = setup(a)
   q[:body] = j
   @client.index q
end