Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #28 from Jirapong/master

Rails 3 support - thanks!
  • Loading branch information...
commit 70366a774c30be0fe0e3be4b3d03f9fe95844b87 2 parents 5327611 + b9a10f1
@kookster kookster authored
View
24 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
+
+end # ApnMigrationsGenerator
View
38 lib/apn_on_rails/apn_on_rails.rb
@@ -3,13 +3,13 @@
require 'configatron'
rails_root = File.join(FileUtils.pwd, 'rails_root')
-if defined?(RAILS_ROOT)
- rails_root = RAILS_ROOT
+if defined?(Rails.root)
+ rails_root = Rails.root.to_s
end
rails_env = 'development'
-if defined?(RAILS_ENV)
- rails_env = RAILS_ENV
+if defined?(Rails.env)
+ rails_env = Rails.env
end
configatron.apn.set_default(:passphrase, '')
@@ -21,38 +21,38 @@
if rails_env == 'production'
configatron.apn.set_default(:host, 'gateway.push.apple.com')
configatron.apn.set_default(:cert, File.join(rails_root, 'config', 'apple_push_notification_production.pem'))
-
+
configatron.apn.feedback.set_default(:host, 'feedback.push.apple.com')
configatron.apn.feedback.set_default(:cert, configatron.apn.cert)
else
configatron.apn.set_default(:host, 'gateway.sandbox.push.apple.com')
configatron.apn.set_default(:cert, File.join(rails_root, 'config', 'apple_push_notification_development.pem'))
-
+
configatron.apn.feedback.set_default(:host, 'feedback.sandbox.push.apple.com')
configatron.apn.feedback.set_default(:cert, configatron.apn.cert)
end
module APN # :nodoc:
-
+
module Errors # :nodoc:
-
+
# Raised when a notification message to Apple is longer than 256 bytes.
class ExceededMessageSizeError < StandardError
-
+
def initialize(message) # :nodoc:
super("The maximum size allowed for a notification payload is 256 bytes: '#{message}'")
end
-
+
end
-
+
class MissingCertificateError < StandardError
def initialize
super("This app has no certificate")
end
end
-
+
end # Errors
-
+
end # APN
base = File.join(File.dirname(__FILE__), 'app', 'models', 'apn', 'base.rb')
@@ -62,20 +62,20 @@ def initialize
require f
end
-%w{ models controllers helpers }.each do |dir|
+%w{ models controllers helpers }.each do |dir|
path = File.join(File.dirname(__FILE__), 'app', dir)
- $LOAD_PATH << path
+ $LOAD_PATH << path
# puts "Adding #{path}"
begin
if ActiveSupport::Dependencies.respond_to? :autoload_paths
ActiveSupport::Dependencies.autoload_paths << path
ActiveSupport::Dependencies.autoload_once_paths.delete(path)
else
- ActiveSupport::Dependencies.load_paths << path
- ActiveSupport::Dependencies.load_once_paths.delete(path)
+ ActiveSupport::Dependencies.load_paths << path
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
end
rescue NameError
- Dependencies.load_paths << path
- Dependencies.load_once_paths.delete(path)
+ Dependencies.load_paths << path
+ Dependencies.load_once_paths.delete(path)
end
end
View
59 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 <tt>sent_at</tt> 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 <tt>devices</tt> method. It then checks to see if the
# <tt>last_registered_at</tt> 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
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.