module OandaAPI::Throttling::ClassMethods

Methods in this module are mixed into the including class as singletons.

Public Instance Methods

limit_connection_rate(use_throttling=true) click to toggle source

Enables or Disables connection rate limits @param use_throttling [Boolean] if `false` connection rates

are NOT limited

@return [void]

Calls superclass method
# File lib/oanda_api/throttling/throttling.rb, line 70
def limit_connection_rate(use_throttling=true)
  klass = self
  Throttling.save_original_new_method klass

  unless use_throttling
    Throttling.restore_original_new_method klass
    return
  end

  # Use an anonymous module, so that it can be prepended into
  # the including class. Prepending is important because we're
  # monkey-patching the including class's `.new` method, and want
  # to be able to call `super` to invoke that original `.new`.
  connection_rate_limiter = Module.new do
    klass.define_singleton_method :new do |*args, &block|
      Throttling.throttle_connection_rate
      #
      # `super` works here because we use prepend to mix this module
      # into the including class.
      super *args, &block
    end
  end
  prepend connection_rate_limiter
end