Module: OandaAPI::Throttling::ClassMethods

Defined in:
lib/oanda_api/throttling/throttling.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#limit_connection_rate(use_throttling = true) ⇒ void

This method returns an undefined value.

Enables or Disables connection rate limits

Parameters:

  • use_throttling (Boolean) (defaults to: true)

    if false connection rates are NOT limited



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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