fireinc-apn_on_rails 0.4.2.9 → 0.4.2.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fireinc-apn_on_rails}
8
- s.version = "0.4.2.9"
8
+ s.version = "0.4.2.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["markbates", "Rebecca Nesson", "Caleb Adam Haye"]
@@ -1,16 +1,16 @@
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
16
  #
@@ -25,9 +25,9 @@ class APN::App < APN::Base
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,66 +36,51 @@ class APN::App < APN::Base
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
- if (app_id == nil)
43
- conditions = "app_id is null"
44
- else
45
- conditions = ["app_id = ?", app_id]
46
- end
47
- begin
48
- APN::Connection.open_for_delivery({:cert => the_cert}) do |conn, sock|
42
+ if (app_id == nil)
43
+ conditions = "app_id is null"
44
+ else
45
+ conditions = ["app_id = ?", app_id]
46
+ end
47
+ begin
49
48
  APN::Device.find_each(:conditions => conditions) do |dev|
50
- dev.unsent_notifications.each do |noty|
51
- begin
49
+ APN::Connection.open_for_delivery({:cert => the_cert}) do |conn, sock|
50
+ dev.unsent_notifications.each do |noty|
52
51
  conn.write(noty.message_for_sending)
53
- rescue => e
54
- Rails.logger.error "Cannot send notification ##{noty.id}: " + e.message
55
- return if e.message == "Broken pipe"
52
+ noty.sent_at = Time.now
53
+ noty.save
56
54
  end
57
-
58
- noty.sent_at = Time.now
59
- noty.save
60
55
  end
56
+ sleep(1)
61
57
  end
58
+ rescue Exception => e
59
+ log_connection_exception(e)
62
60
  end
63
- rescue Exception => e
64
- log_connection_exception(e)
65
- end
66
61
  # end
67
62
  end
68
-
63
+
69
64
  def send_group_notifications
70
- if self.cert.nil?
65
+ if self.cert.nil?
71
66
  raise APN::Errors::MissingCertificateError.new
72
67
  return
73
68
  end
74
- unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty?
75
- unsent_group_notifications.each do |gnoty|
76
- failed = 0
77
- devices_to_send = gnoty.devices.count
78
- gnoty.devices.find_in_batches(:batch_size => 100) do |devices|
79
- APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock|
80
- devices.each do |device|
81
- begin
82
- conn.write(gnoty.message_for_sending(device))
83
- rescue Exception => e
84
- puts e.message
85
- failed += 1
86
- end
87
- end
69
+ unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty?
70
+ APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock|
71
+ unsent_group_notifications.each do |gnoty|
72
+ gnoty.devices.find_each do |device|
73
+ conn.write(gnoty.message_for_sending(device))
88
74
  end
75
+ gnoty.sent_at = Time.now
76
+ gnoty.save
89
77
  end
90
- puts "Sent to: #{devices_to_send - failed}/#{devices_to_send} "
91
- gnoty.sent_at = Time.now
92
- gnoty.save
93
78
  end
94
79
  end
95
80
  end
96
-
81
+
97
82
  def send_group_notification(gnoty)
98
- if self.cert.nil?
83
+ if self.cert.nil?
99
84
  raise APN::Errors::MissingCertificateError.new
100
85
  return
101
86
  end
@@ -109,14 +94,14 @@ class APN::App < APN::Base
109
94
  end
110
95
  end
111
96
  end
112
-
97
+
113
98
  def self.send_group_notifications
114
99
  apps = APN::App.all
115
100
  apps.each do |app|
116
101
  app.send_group_notifications
117
102
  end
118
- end
119
-
103
+ end
104
+
120
105
  # Retrieves a list of APN::Device instnces from Apple using
121
106
  # the <tt>devices</tt> method. It then checks to see if the
122
107
  # <tt>last_registered_at</tt> date of each APN::Device is
@@ -133,10 +118,8 @@ class APN::App < APN::Base
133
118
  return
134
119
  end
135
120
  APN::App.process_devices_for_cert(self.cert)
136
- end
137
-
138
- # process_devices
139
-
121
+ end # process_devices
122
+
140
123
  def self.process_devices
141
124
  apps = APN::App.all
142
125
  apps.each do |app|
@@ -147,23 +130,23 @@ class APN::App < APN::Base
147
130
  APN::App.process_devices_for_cert(global_cert)
148
131
  end
149
132
  end
150
-
133
+
151
134
  def self.process_devices_for_cert(the_cert)
152
135
  puts "in APN::App.process_devices_for_cert"
153
136
  APN::Feedback.devices(the_cert).each do |device|
154
137
  if device.last_registered_at < device.feedback_at
155
138
  puts "device #{device.id} -> #{device.last_registered_at} < #{device.feedback_at}"
156
139
  device.destroy
157
- else
140
+ else
158
141
  puts "device #{device.id} -> #{device.last_registered_at} not < #{device.feedback_at}"
159
142
  end
160
- end
143
+ end
161
144
  end
162
-
163
-
145
+
146
+
164
147
  protected
165
148
  def self.log_connection_exception(ex)
166
149
  puts ex.message
167
150
  end
168
-
151
+
169
152
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fireinc-apn_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 117
4
+ hash: 115
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
9
  - 2
10
- - 9
11
- version: 0.4.2.9
10
+ - 10
11
+ version: 0.4.2.10
12
12
  platform: ruby
13
13
  authors:
14
14
  - markbates