Sha256: 3a155b8ab7fc2b4db5b09cdfcad9f08cebc27db78c831a89d15effb38c7c1f58

Contents?: true

Size: 989 Bytes

Versions: 3

Compression:

Stored size: 989 Bytes

Contents

# frozen_string_literal: true

require 'active_support/time'
require 'bundle_notification/snippet'

module BundleNotification
  class Deliver
    attr_reader :mailer_class

    def initialize(mailer_class)
      @mailer_class = mailer_class
    end

    def deliver_unsent_snippets
      Snippet.transaction do
        return if unsent_snippets.empty?
        unsent_snippets.group_by(&:recipient).each do |recipient, snippets|
          bundle_notify(recipient, snippets.map(&:data)).deliver_later
        end
        mark_snippets_sent
      end
    end

    private

    def bundle_notify(bundle_data, snippets_data)
      mailer_class.constantize.bundle_notify(bundle_data, snippets_data)
    end

    def unsent_snippets
      @unsent_snippets ||= Snippet.unsent_for(mailer_class).order(:created_at, :id).lock.to_a
    end

    def mark_snippets_sent
      Snippet.where(id: unsent_snippets).update_all(sent_at: (Time.zone || Time).now)
      @unsent_snippets = nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bundle_notification-0.2.2 lib/bundle_notification/deliver.rb
bundle_notification-0.2.1 lib/bundle_notification/deliver.rb
bundle_notification-0.2.0 lib/bundle_notification/deliver.rb