<commit_before>class C2dm::App < C2dm::Base  
  set_table_name "c2dm_apps"
  
  has_many :devices, :class_name => "C2dm::Device", :dependent => :destroy
  has_many :notifications, :through => :devices, :dependent => :destroy
  
  def deliver_notifications
    connection = C2dm::Connection.new(username, password, source)
    
    self.notifications.deliverable.each do |notification|
      if (notification.sent_at + (10 ** notification.tries).seconds) < Time.now
        deliver_notification(notification, connection)
    end
    end
      
  end
  
  def deliver_notification (notification, connection = nil)
    connection ||= C2dm::Connection.new(username, password, source)
    
    options = {
      :registration_id => notification.device.registration_id,
      :data => notification.data,
      :collapse_key => notification.collapse_key,
      :delay_while_idle => notification.delay_while_idle
    }
    
    response = connection.send_notification(options)
        
    answer = Hash[response.body.scan(/(\w+)=(\w+)/).map{ |a| [a[0].downcase.to_sym, a[1]]}]
    
    notification.message_id = answer[:id]
    notification.sent_at = Time.now
    notification.deliver = false
    
    if response.response.code == 200 # request executed correctly
      if answer.has_key? :error        
        notification.error = answer[:error]
        
        # these are recoverable errors
        if notification.error == "QuotaExceeded" || notification.error == "DeviceQuotaExceeded"
          notification.try += 1
          if notification.try <= 4
            notification.deliver = true
          else
            notification.error = "TriesExceeded"
          end
        end
      end          
    elsif response.response.code == 503 # service not available, redeliver message later
      notification.error = "Service temporarily unavailable"
      
      notification.try += 1
      
      if notification.try <= 4
        notification.deliver = true
      else
        notification.error = "TriesExceeded"
      end
      
    elsif response.response.code == 401 # wrong api credentials
      notification.error = "wrong authtoken, check corresponding login"   
    else
      notification.error = "unknown error"
    end
    
    notification.save
  end
  
end<commit_msg>deleting devices not connected anymore<commit_after>class C2dm::App < C2dm::Base  
  set_table_name "c2dm_apps"
  
  has_many :devices, :class_name => "C2dm::Device", :dependent => :destroy
  has_many :notifications, :through => :devices, :dependent => :destroy
  
  def deliver_notifications
    connection = C2dm::Connection.new(username, password, source)
    
    self.notifications.deliverable.each do |notification|
      if (notification.sent_at + (10 ** notification.tries).seconds) < Time.now
        deliver_notification(notification, connection)
    end
    end
      
  end
  
  def deliver_notification (notification, connection = nil)
    connection ||= C2dm::Connection.new(username, password, source)
    
    options = {
      :registration_id => notification.device.registration_id,
      :data => notification.data,
      :collapse_key => notification.collapse_key,
      :delay_while_idle => notification.delay_while_idle
    }
    
    response = connection.send_notification(options)
        
    answer = Hash[response.body.scan(/(\w+)=(\w+)/).map{ |a| [a[0].downcase.to_sym, a[1]]}]
    
    notification.message_id = answer[:id]
    notification.sent_at = Time.now
    notification.deliver = false
    
    if response.response.code == 200 # request executed correctly
      if answer.has_key? :error        
        notification.error = answer[:error]
        
        # these are recoverable errors
        if notification.error == "QuotaExceeded" || notification.error == "DeviceQuotaExceeded"
          notification.try += 1
          if notification.try <= 4
            notification.deliver = true
          else
            notification.error = "TriesExceeded"
          end
        end
        
        # not a device connected with this app, delete it
        if notification.error == "InvalidRegistration" || notification.error == "NotRegistered"
          notification.device.delete
        end
        
      end          
    elsif response.response.code == 503 # service not available, redeliver message later
      notification.error = "Service temporarily unavailable"
      
      notification.try += 1
      
      if notification.try <= 4
        notification.deliver = true
      else
        notification.error = "TriesExceeded"
      end
      
    elsif response.response.code == 401 # wrong api credentials
      notification.error = "wrong authtoken, check corresponding login"   
    else
      notification.error = "unknown error"
    end
    
    notification.save
  end
  
end
{'fraction_non_alphanumeric': 0.09513917739925218, 'fraction_numerical': 0.009555463232239303, 'mean_word_length': 2.543046357615894, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '3476516', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1306, 'n_words': 307}