class MaybeClient

Public Class Methods

delay() click to toggle source
# File lib/maybe_client.rb, line 11
def self.delay
  60
end
new(client: nil, client_class: nil, connect_params: nil) click to toggle source
# File lib/maybe_client.rb, line 2
def initialize(client: nil, client_class: nil, connect_params: nil)
  @client = client
  @connect_params = connect_params
  @client_class = client_class
  @should_initialize = !!@client_class

  initialize_client if client_initialization_needed?
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source

Used to delegate everything to @client

Calls superclass method
# File lib/maybe_client.rb, line 20
def method_missing(method, *args, &block)
  return if noop?
  initialize_client if client_initialization_needed?
  return if in_delay?

  # Raises NoMethodError
  super unless @client.respond_to? method

  result = nil

  begin
    @fail_at = nil
    result = @client.send(method, *args)
  rescue Exception => e
    handle_exception(e)
  end

  result
end
respond_to?(method) click to toggle source
# File lib/maybe_client.rb, line 15
def respond_to? method
  !!@client && @client.respond_to?(method)
end

Private Instance Methods

client_initialization_needed?() click to toggle source
# File lib/maybe_client.rb, line 61
def client_initialization_needed?
  !@client && @should_initialize
end
exception_handler(e) click to toggle source
# File lib/maybe_client.rb, line 53
def exception_handler(e)
end
handle_exception(e) click to toggle source
# File lib/maybe_client.rb, line 56
def handle_exception(e)
  @fail_at = Time.now
  exception_handler(e)
end
in_delay?() click to toggle source
# File lib/maybe_client.rb, line 49
def in_delay?
  @fail_at && @fail_at + self.class.delay > Time.now
end
initialize_client() click to toggle source
# File lib/maybe_client.rb, line 65
def initialize_client
  begin
    @client = @client_class.new(@connect_params)
  rescue Exception => e
    handle_exception(e)
  end
end
no_client?() click to toggle source
# File lib/maybe_client.rb, line 45
def no_client?
  !@client && !@should_initialize
end
noop?() click to toggle source
# File lib/maybe_client.rb, line 41
def noop?
  no_client? || in_delay?
end