Class: WhatsAppNotifier::Bulk::Dispatcher
- Inherits:
-
Object
- Object
- WhatsAppNotifier::Bulk::Dispatcher
- Defined in:
- lib/whatsapp_notifier/bulk/dispatcher.rb
Instance Method Summary collapse
- #deliver(messages, provider: nil) ⇒ Object
-
#initialize(client:, configuration:, sleeper: ->(seconds) { sleep(seconds) }, rng: Random.new) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
Constructor Details
#initialize(client:, configuration:, sleeper: ->(seconds) { sleep(seconds) }, rng: Random.new) ⇒ Dispatcher
Returns a new instance of Dispatcher.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/whatsapp_notifier/bulk/dispatcher.rb', line 6 def initialize(client:, configuration:, sleeper: ->(seconds) { sleep(seconds) }, rng: Random.new) @client = client @configuration = configuration @rate_limiter = RateLimiter.new( base_delay: configuration.bulk_base_delay_seconds, jitter: configuration.bulk_jitter_seconds, sleeper: sleeper, rng: rng ) @retry_policy = RetryPolicy.new( max_attempts: configuration.bulk_max_attempts, retryable_error_codes: configuration.bulk_retryable_error_codes ) @sleeper = sleeper end |
Instance Method Details
#deliver(messages, provider: nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/whatsapp_notifier/bulk/dispatcher.rb', line 22 def deliver(, provider: nil) raise ConfigurationError, "messages must be an array" unless .is_a?(Array) raise ConfigurationError, "bulk_max_recipients exceeded" if .length > @configuration.bulk_max_recipients sent_keys = Set.new results = .map.with_index do |, idx| @rate_limiter.wait_before_next if idx.positive? deliver_one(, sent_keys: sent_keys, provider: provider) end { total: results.length, success: results.count(&:success?), failed: results.count(&:failure?), results: results } end |