From b9a10f1e940b02c07d8c85f892a4ef6083f83fe1 Mon Sep 17 00:00:00 2001 From: Jirapong Nanta Date: Thu, 28 Apr 2011 14:59:55 +0700 Subject: [PATCH] handle error event on log_connection_exception should be a class method --- generators/apn_migrations_generator.rb | 24 ++++++----- lib/apn_on_rails/app/models/apn/app.rb | 59 +++++++++++++------------- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/generators/apn_migrations_generator.rb b/generators/apn_migrations_generator.rb index 6d9c9298..2130fa8f 100644 --- a/generators/apn_migrations_generator.rb +++ b/generators/apn_migrations_generator.rb @@ -1,31 +1,33 @@ require 'rails_generator' +require 'rails/generators/migration' # Generates the migrations necessary for APN on Rails. -# This should be run upon install and upgrade of the +# This should be run upon install and upgrade of the # APN on Rails gem. -# +# # $ ruby script/generate apn_migrations class ApnMigrationsGenerator < Rails::Generator::Base - + desc "Generates the migrations necessary for APN on Rails." + def manifest # :nodoc: record do |m| timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S") db_migrate_path = File.join('db', 'migrate') - + m.directory(db_migrate_path) - + Dir.glob(File.join(File.dirname(__FILE__), 'templates', 'apn_migrations', '*.rb')).sort.each_with_index do |f, i| f = File.basename(f) f.match(/\d+\_(.+)/) timestamp = timestamp.succ if Dir.glob(File.join(db_migrate_path, "*_#{$1}")).empty? - m.file(File.join('apn_migrations', f), - File.join(db_migrate_path, "#{timestamp}_#{$1}"), + m.file(File.join('apn_migrations', f), + File.join(db_migrate_path, "#{timestamp}_#{$1}"), {:collision => :skip}) end end - + end # record - + end # manifest - -end # ApnMigrationsGenerator \ No newline at end of file + +end # ApnMigrationsGenerator diff --git a/lib/apn_on_rails/app/models/apn/app.rb b/lib/apn_on_rails/app/models/apn/app.rb index a0927374..0c378ccc 100644 --- a/lib/apn_on_rails/app/models/apn/app.rb +++ b/lib/apn_on_rails/app/models/apn/app.rb @@ -1,23 +1,23 @@ class APN::App < APN::Base - + has_many :groups, :class_name => 'APN::Group', :dependent => :destroy has_many :devices, :class_name => 'APN::Device', :dependent => :destroy has_many :notifications, :through => :devices, :dependent => :destroy has_many :unsent_notifications, :through => :devices has_many :group_notifications, :through => :groups has_many :unsent_group_notifications, :through => :groups - + def cert (RAILS_ENV == 'production' ? apn_prod_cert : apn_dev_cert) end - + # Opens a connection to the Apple APN server and attempts to batch deliver # an Array of group notifications. - # - # + # + # # As each APN::GroupNotification is sent the sent_at column will be timestamped, # so as to not be sent again. - # + # def send_notifications if self.cert.nil? raise APN::Errors::MissingCertificateError.new @@ -25,9 +25,9 @@ def send_notifications end APN::App.send_notifications_for_cert(self.cert, self.id) end - + def self.send_notifications - apps = APN::App.all + apps = APN::App.all apps.each do |app| app.send_notifications end @@ -36,12 +36,12 @@ def self.send_notifications send_notifications_for_cert(global_cert, nil) end end - + def self.send_notifications_for_cert(the_cert, app_id) # unless self.unsent_notifications.nil? || self.unsent_notifications.empty? if (app_id == nil) conditions = "app_id is null" - else + else conditions = ["app_id = ?", app_id] end begin @@ -57,15 +57,15 @@ def self.send_notifications_for_cert(the_cert, app_id) rescue Exception => e log_connection_exception(e) end - # end + # end end - + def send_group_notifications - if self.cert.nil? + if self.cert.nil? raise APN::Errors::MissingCertificateError.new return end - unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty? + unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty? APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock| unsent_group_notifications.each do |gnoty| gnoty.devices.find_each do |device| @@ -77,9 +77,9 @@ def send_group_notifications end end end - + def send_group_notification(gnoty) - if self.cert.nil? + if self.cert.nil? raise APN::Errors::MissingCertificateError.new return end @@ -93,14 +93,14 @@ def send_group_notification(gnoty) end end end - + def self.send_group_notifications apps = APN::App.all apps.each do |app| app.send_group_notifications end - end - + end + # Retrieves a list of APN::Device instnces from Apple using # the devices method. It then checks to see if the # last_registered_at date of each APN::Device is @@ -108,7 +108,7 @@ def self.send_group_notifications # accepting notifications then the device is deleted. Otherwise # it is assumed that the application has been re-installed # and is available for notifications. - # + # # This can be run from the following Rake task: # $ rake apn:feedback:process def process_devices @@ -118,7 +118,7 @@ def process_devices end APN::App.process_devices_for_cert(self.cert) end # process_devices - + def self.process_devices apps = APN::App.all apps.each do |app| @@ -129,23 +129,24 @@ def self.process_devices APN::App.process_devices_for_cert(global_cert) end end - + def self.process_devices_for_cert(the_cert) puts "in APN::App.process_devices_for_cert" APN::Feedback.devices(the_cert).each do |device| if device.last_registered_at < device.feedback_at puts "device #{device.id} -> #{device.last_registered_at} < #{device.feedback_at}" device.destroy - else + else puts "device #{device.id} -> #{device.last_registered_at} not < #{device.feedback_at}" end - end + end end - - + + protected def log_connection_exception(ex) - puts ex.message + STDERR.puts ex.message + raise ex end - -end \ No newline at end of file + +end