<commit_before>class Gcm::Notification < Gcm::Base
  self.table_name = "gcm_notifications"

  include ::ActionView::Helpers::TextHelper
  extend ::ActionView::Helpers::TextHelper
  serialize :data

  belongs_to :device, :class_name => 'Gcm::Device'
  validates_presence_of :collapse_key if :time_to_live?

  class << self
    # Opens a connection to the Google GCM server and attempts to batch deliver
    # an Array of notifications.
    #
    # This method expects an Array of Gcm::Notifications. If no parameter is passed
    # in then it will use the following:
    #   Gcm::Notification.all(:conditions => {:sent_at => nil})
    #
    # As each Gcm::Notification is sent the <tt>sent_at</tt> column will be timestamped,
    # so as to not be sent again.
    #
    # This can be run from the following Rake task:
    #   $ rake gcm:notifications:deliver
    def send_notifications(notifications = Gcm::Notification.all(:conditions => {:sent_at => nil}, :joins => :device, :readonly => false))

      if configatron.gcm_on_rails.delivery_format and configatron.gcm_on_rails.delivery_format == 'plain_text'
        format = "plain_text"
      else
        format = "json"
      end

      unless notifications.nil? || notifications.empty?
        api_key = Gcm::Connection.open
        if api_key
          notifications.each do |notification|
            response = Gcm::Connection.send_notification(notification, api_key, format)
            if response[:code] == 200
              if format == "json"
                error = ""
                puts "Response is #{response.inspect}"
                message_data = JSON.parse response[:message]
                success = message_data['success']
                error = message_data['results'][0]['error']  if success == 0
              else   #format is plain text
                message_data = response[:message]
                error = response[:message].split('=')[1]
              end


              case error
                when "MissingRegistration"
                  ex = Gcm::Errors::MissingRegistration.new(response[:message])
                  logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
                  notification.device.destroy
                when "InvalidRegistration"
                  ex = Gcm::Errors::InvalidRegistration.new(response[:message])
                  logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
                  notification.device.destroy
                when "MismatchedSenderId"
                  ex = Gcm::Errors::MismatchSenderId.new(response[:message])
                  logger.warn(ex.message)
                when "NotRegistered"
                  ex = Gcm::Errors::NotRegistered.new(response[:message])
                  logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")<commit_msg>Added null check for message<commit_after>class Gcm::Notification < Gcm::Base
  self.table_name = "gcm_notifications"

  include ::ActionView::Helpers::TextHelper
  extend ::ActionView::Helpers::TextHelper
  serialize :data

  belongs_to :device, :class_name => 'Gcm::Device'
  validates_presence_of :collapse_key if :time_to_live?

  class << self
    # Opens a connection to the Google GCM server and attempts to batch deliver
    # an Array of notifications.
    #
    # This method expects an Array of Gcm::Notifications. If no parameter is passed
    # in then it will use the following:
    #   Gcm::Notification.all(:conditions => {:sent_at => nil})
    #
    # As each Gcm::Notification is sent the <tt>sent_at</tt> column will be timestamped,
    # so as to not be sent again.
    #
    # This can be run from the following Rake task:
    #   $ rake gcm:notifications:deliver
    def send_notifications(notifications = Gcm::Notification.all(:conditions => {:sent_at => nil}, :joins => :device, :readonly => false))

      if configatron.gcm_on_rails.delivery_format and configatron.gcm_on_rails.delivery_format == 'plain_text'
        format = "plain_text"
      else
        format = "json"
      end

      unless notifications.nil? || notifications.empty?
        api_key = Gcm::Connection.open
        if api_key
          notifications.each do |notification|
            response = Gcm::Connection.send_notification(notification, api_key, format)
            if response[:code] == 200 && !response[:message].nil?
              if format == "json"
                error = ""
                puts "Response is #{response.inspect}"
                message_data = JSON.parse response[:message]
                success = message_data['success']
                error = message_data['results'][0]['error']  if success == 0
              else   #format is plain text
                message_data = response[:message]
                error = response[:message].split('=')[1]
              end


              case error
                when "MissingRegistration"
                  ex = Gcm::Errors::MissingRegistration.new(response[:message])
                  logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
                  notification.device.destroy
                when "InvalidRegistration"
                  ex = Gcm::Errors::InvalidRegistration.new(response[:message])
                  logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
                  notification.device.destroy
                when "MismatchedSenderId"
                  ex = Gcm::Errors::MismatchSenderId.new(response[:message])
                  logger.warn(ex.message)
                when "NotRegistered"
                  ex = Gcm::Errors::NotRegistered.new(response[:message])
                  logger.warn("#{ex.message}, destroying gcm_device with id #{notification.device.id}")
{'fraction_non_alphanumeric': 0.10562297043240472, 'fraction_numerical': 0.0020509314647068877, 'mean_word_length': 2.390498261877173, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1208065', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1516, 'n_words': 390}