Skip to content
This repository
Browse code

handle error event on log_connection_exception should be a class method

  • Loading branch information...
commit b9a10f1e940b02c07d8c85f892a4ef6083f83fe1 1 parent e364b6f
Jirapong Nanta authored April 28, 2011
24  generators/apn_migrations_generator.rb
... ...
@@ -1,31 +1,33 @@
1 1
 require 'rails_generator'
  2
+require 'rails/generators/migration'
2 3
 # Generates the migrations necessary for APN on Rails.
3  
-# This should be run upon install and upgrade of the 
  4
+# This should be run upon install and upgrade of the
4 5
 # APN on Rails gem.
5  
-# 
  6
+#
6 7
 #   $ ruby script/generate apn_migrations
7 8
 class ApnMigrationsGenerator < Rails::Generator::Base
8  
-  
  9
+  desc "Generates the migrations necessary for APN on Rails."
  10
+
9 11
   def manifest # :nodoc:
10 12
     record do |m|
11 13
       timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
12 14
       db_migrate_path = File.join('db', 'migrate')
13  
-      
  15
+
14 16
       m.directory(db_migrate_path)
15  
-      
  17
+
16 18
       Dir.glob(File.join(File.dirname(__FILE__), 'templates', 'apn_migrations', '*.rb')).sort.each_with_index do |f, i|
17 19
         f = File.basename(f)
18 20
         f.match(/\d+\_(.+)/)
19 21
         timestamp = timestamp.succ
20 22
         if Dir.glob(File.join(db_migrate_path, "*_#{$1}")).empty?
21  
-          m.file(File.join('apn_migrations', f), 
22  
-                 File.join(db_migrate_path, "#{timestamp}_#{$1}"), 
  23
+          m.file(File.join('apn_migrations', f),
  24
+                 File.join(db_migrate_path, "#{timestamp}_#{$1}"),
23 25
                  {:collision => :skip})
24 26
         end
25 27
       end
26  
-      
  28
+
27 29
     end # record
28  
-    
  30
+
29 31
   end # manifest
30  
-  
31  
-end # ApnMigrationsGenerator
  32
+
  33
+end # ApnMigrationsGenerator
59  lib/apn_on_rails/app/models/apn/app.rb
... ...
@@ -1,23 +1,23 @@
1 1
 class APN::App < APN::Base
2  
-  
  2
+
3 3
   has_many :groups, :class_name => 'APN::Group', :dependent => :destroy
4 4
   has_many :devices, :class_name => 'APN::Device', :dependent => :destroy
5 5
   has_many :notifications, :through => :devices, :dependent => :destroy
6 6
   has_many :unsent_notifications, :through => :devices
7 7
   has_many :group_notifications, :through => :groups
8 8
   has_many :unsent_group_notifications, :through => :groups
9  
-    
  9
+
10 10
   def cert
11 11
     (RAILS_ENV == 'production' ? apn_prod_cert : apn_dev_cert)
12 12
   end
13  
-  
  13
+
14 14
   # Opens a connection to the Apple APN server and attempts to batch deliver
15 15
   # an Array of group notifications.
16  
-  # 
17  
-  # 
  16
+  #
  17
+  #
18 18
   # As each APN::GroupNotification is sent the <tt>sent_at</tt> column will be timestamped,
19 19
   # so as to not be sent again.
20  
-  # 
  20
+  #
21 21
   def send_notifications
22 22
     if self.cert.nil?
23 23
       raise APN::Errors::MissingCertificateError.new
@@ -25,9 +25,9 @@ def send_notifications
25 25
     end
26 26
     APN::App.send_notifications_for_cert(self.cert, self.id)
27 27
   end
28  
-  
  28
+
29 29
   def self.send_notifications
30  
-    apps = APN::App.all 
  30
+    apps = APN::App.all
31 31
     apps.each do |app|
32 32
       app.send_notifications
33 33
     end
@@ -36,12 +36,12 @@ def self.send_notifications
36 36
       send_notifications_for_cert(global_cert, nil)
37 37
     end
38 38
   end
39  
-  
  39
+
40 40
   def self.send_notifications_for_cert(the_cert, app_id)
41 41
     # unless self.unsent_notifications.nil? || self.unsent_notifications.empty?
42 42
       if (app_id == nil)
43 43
         conditions = "app_id is null"
44  
-      else 
  44
+      else
45 45
         conditions = ["app_id = ?", app_id]
46 46
       end
47 47
       begin
@@ -57,15 +57,15 @@ def self.send_notifications_for_cert(the_cert, app_id)
57 57
       rescue Exception => e
58 58
         log_connection_exception(e)
59 59
       end
60  
-    # end   
  60
+    # end
61 61
   end
62  
-  
  62
+
63 63
   def send_group_notifications
64  
-    if self.cert.nil? 
  64
+    if self.cert.nil?
65 65
       raise APN::Errors::MissingCertificateError.new
66 66
       return
67 67
     end
68  
-    unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty? 
  68
+    unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty?
69 69
       APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock|
70 70
         unsent_group_notifications.each do |gnoty|
71 71
           gnoty.devices.find_each do |device|
@@ -77,9 +77,9 @@ def send_group_notifications
77 77
       end
78 78
     end
79 79
   end
80  
-  
  80
+
81 81
   def send_group_notification(gnoty)
82  
-    if self.cert.nil? 
  82
+    if self.cert.nil?
83 83
       raise APN::Errors::MissingCertificateError.new
84 84
       return
85 85
     end
@@ -93,14 +93,14 @@ def send_group_notification(gnoty)
93 93
       end
94 94
     end
95 95
   end
96  
-  
  96
+
97 97
   def self.send_group_notifications
98 98
     apps = APN::App.all
99 99
     apps.each do |app|
100 100
       app.send_group_notifications
101 101
     end
102  
-  end          
103  
-  
  102
+  end
  103
+
104 104
   # Retrieves a list of APN::Device instnces from Apple using
105 105
   # the <tt>devices</tt> method. It then checks to see if the
106 106
   # <tt>last_registered_at</tt> date of each APN::Device is
@@ -108,7 +108,7 @@ def self.send_group_notifications
108 108
   # accepting notifications then the device is deleted. Otherwise
109 109
   # it is assumed that the application has been re-installed
110 110
   # and is available for notifications.
111  
-  # 
  111
+  #
112 112
   # This can be run from the following Rake task:
113 113
   #   $ rake apn:feedback:process
114 114
   def process_devices
@@ -118,7 +118,7 @@ def process_devices
118 118
     end
119 119
     APN::App.process_devices_for_cert(self.cert)
120 120
   end # process_devices
121  
-  
  121
+
122 122
   def self.process_devices
123 123
     apps = APN::App.all
124 124
     apps.each do |app|
@@ -129,23 +129,24 @@ def self.process_devices
129 129
       APN::App.process_devices_for_cert(global_cert)
130 130
     end
131 131
   end
132  
-  
  132
+
133 133
   def self.process_devices_for_cert(the_cert)
134 134
     puts "in APN::App.process_devices_for_cert"
135 135
     APN::Feedback.devices(the_cert).each do |device|
136 136
       if device.last_registered_at < device.feedback_at
137 137
         puts "device #{device.id} -> #{device.last_registered_at} < #{device.feedback_at}"
138 138
         device.destroy
139  
-      else 
  139
+      else
140 140
         puts "device #{device.id} -> #{device.last_registered_at} not < #{device.feedback_at}"
141 141
       end
142  
-    end 
  142
+    end
143 143
   end
144  
-  
145  
-  
  144
+
  145
+
146 146
   protected
147 147
   def log_connection_exception(ex)
148  
-    puts ex.message
  148
+    STDERR.puts ex.message
  149
+    raise ex
149 150
   end
150  
-    
151  
-end
  151
+
  152
+end

0 notes on commit b9a10f1

Please sign in to comment.
Something went wrong with that request. Please try again.