class Sweatpants::Client

Attributes

actions_to_trap[R]
client[R]
flush_frequency[R]
queue[R]

Public Class Methods

new(es_params=nil) click to toggle source
# File lib/sweatpants/client.rb, line 8
def initialize es_params=nil
  @client = es_params.nil? ? Sweatpants.configuration.client : Elasticsearch::Client.new(es_params)
  @queue = Sweatpants.configuration.queue
  @flush_frequency = Sweatpants.configuration.flush_frequency
  @actions_to_trap = Sweatpants.configuration.actions_to_trap
  @timer = Sweatpants::Timer.new(@flush_frequency)
  @timer.on_tick { flush }
end

Public Instance Methods

flush() click to toggle source
# File lib/sweatpants/client.rb, line 17
def flush
  begin
    @client.bulk @queue.dequeue
  rescue Exception => e
    $stderr.puts e # use a Logger, maybe @client's?
  end
end
method_missing(method_name, *args, &block) click to toggle source
# File lib/sweatpants/client.rb, line 25
def method_missing(method_name, *args, &block)
  if trap_request?(method_name, *args)
    delay(method_name, *args)
  else
    @client.send(method_name, args[0])
  end
end

Private Instance Methods

delay(method_name, *args) click to toggle source
# File lib/sweatpants/client.rb, line 35
def delay method_name, *args
  request = Sweatpants::QueuedRequest.new method_name, args.first
  @queue.enqueue request.to_bulk
end
trap_request?(action, *args) click to toggle source
# File lib/sweatpants/client.rb, line 40
def trap_request? action, *args
  sweatpants_arguments = args[1] || {}
  !sweatpants_arguments[:immediate] && @actions_to_trap.include?(action) 
end