Sha256: bbed277b9ff778534732c5c335745c2d6987ea0552f6dd31165ac705f338850f

Contents?: true

Size: 1.53 KB

Versions: 15

Compression:

Stored size: 1.53 KB

Contents

module Notifiable
  class Batch
    
    def initialize(app)
      raise "Must specify Notifiable::App" unless app
      @app = app
      @notifiers = {}
      @notification_ids = []
    end
    
    def add_notifiable(notification, notifiable)
      notifiable.device_tokens.each do |d|
        self.add_device_token(notification, d)
      end
    end
    
    def add_device_token(notification, d)
      provider = d.provider.to_sym

      unless @notifiers[provider]
        clazz = Notifiable.notifier_classes[provider]          
        raise "Notifier #{provider} not configured" unless clazz
        @notifiers[provider] = clazz.new
        @notifiers[provider].env = Rails.env
        
        if @app.configuration && @app.configuration[provider]
          @app.configuration[provider].each_pair {|key, value| @notifiers[provider].send("#{key}=", value) if @notifiers[provider].methods.include?("#{key}=".to_sym) } 
        end
      end
      
      notifier = @notifiers[provider]
      if d.is_valid? && !notifier.nil? 
  		  notifier.send_notification(notification, d)
        @notification_ids << notification.id
      end
    end
    
    def close
      @notifiers.each_value {|n| n.close}
      @notifiers = nil
      summarise
    end
    
    private
    def summarise
      notifications = Notification.where(:id => @notification_ids)
      notifications.each do |n|
        n.sent_count = n.notification_statuses.count
        n.gateway_accepted_count = n.notification_statuses.where(:status => 0).count
        n.save
      end
    end 
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
notifiable-rails-0.16.0 lib/notifiable/batch.rb
notifiable-rails-0.15.3 lib/notifiable/batch.rb
notifiable-rails-0.15.2 lib/notifiable/batch.rb
notifiable-rails-0.15.1 lib/notifiable/batch.rb
notifiable-rails-0.15.0 lib/notifiable/batch.rb
notifiable-rails-0.14.1 lib/notifiable/batch.rb
notifiable-rails-0.14.0 lib/notifiable/batch.rb
notifiable-rails-0.13.0 lib/notifiable/batch.rb
notifiable-rails-0.12.1 lib/notifiable/batch.rb
notifiable-rails-0.12.0 lib/notifiable/batch.rb
notifiable-rails-0.11.0 lib/notifiable/batch.rb
notifiable-rails-0.10.1 lib/notifiable/batch.rb
notifiable-rails-0.10.0 lib/notifiable/batch.rb
notifiable-rails-0.9.0 lib/notifiable/batch.rb
notifiable-rails-0.8.0 lib/notifiable/batch.rb