Browse files

Updated README

  • Loading branch information...
1 parent 811b9d7 commit 525710c0625de066a95ff1744dccfe442ccb53e8 @markbates markbates committed Jul 24, 2009
View
38 README
@@ -44,8 +44,8 @@ If you like to use the built in Rails gem management:
Or, if you like to live on the edge:
config.gem 'markbates-apn_on_rails', :lib => 'apn_on_rails', :source => 'http://gems.github.com'
-Setup
------
+Setup and Configuration
+-----------------------
Once you have the gem installed you need to add the following to your Rakefile so you can use the
Rake tasks that ship with APN on Rails:
begin
@@ -57,20 +57,32 @@ Rake tasks that ship with APN on Rails:
Now, to create the tables you need for APN on Rails, run the following task:
$ rake apn:db:migrate
+APN on Rails uses the Configatron gem, http://github.com/markbates/configatron/tree/master,
+to configure itself. APN on Rails has the following default configurations that you change as you
+see fit:
+
+ configatron.apn.passphrase # => ''
+ configatron.apn.port # => 2195
+ configatron.apn.host # => 'gateway.sandbox.push.apple.com'
+ configatron.apn.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_development.pem')
+
+ # production:
+ configatron.apn.host # => 'gateway.push.apple.com'
+ configatron.apn.cert #=> File.join(RAILS_ROOT, 'config', 'apple_push_notification_production.pem')
+
That's it, now you're ready to start creating notifications.
Example
-------
-*Note: the spaces in `device_token` are optional.*
-
$ ./script/console
- >> a = ApplePushNotification.new
- >> a.device_token = "XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX"
- >> a.badge = 5
- >> a.sound = true
- >> a.alert = "foobar"
- >> a.send_notification
- => nil
-
-Copyright (c) 2009 Fabien Penso. Released under the MIT license. Modified by [Sam Soffes](http://samsoff.es).
+ >> device = APN::Device.create(:token => "XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX")
+ >> notification = APN::Notification.new
+ >> notification.device = device
+ >> notification.badge = 5
+ >> notification.sound = true
+ >> notification.alert = "foobar"
+ >> notification.save
+ >> APN::Notification.send_notifications
+
+Released under the MIT license.
View
2 Rakefile
@@ -4,7 +4,7 @@ require 'gemstub'
Gemstub.test_framework = :rspec
Gemstub.gem_spec do |s|
- s.version = "0.0.1"
+ s.version = "0.1.0"
s.rubyforge_project = "magrathea"
s.add_dependency('configatron')
end
View
6 content_o_matic.gemspec → apn_on_rails.gemspec
@@ -2,15 +2,15 @@
Gem::Specification.new do |s|
s.name = %q{apn_on_rails}
- s.version = "0.0.1.20090723180641"
+ s.version = "0.1.0.20090724114507"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["markbates"]
- s.date = %q{2009-07-23}
+ s.date = %q{2009-07-24}
s.description = %q{apn_on_rails was developed by: markbates}
s.email = %q{}
s.extra_rdoc_files = ["README", "LICENSE"]
- s.files = ["lib/apn_on_rails/apn_on_rails.rb", "lib/apn_on_rails/app/models/apn/device.rb", "lib/apn_on_rails/app/models/apn/notification.rb", "lib/apn_on_rails/app/models/apple_push_notification.rb", "lib/apn_on_rails/db/migrate/20090723132058_create_apn_devices.rb", "lib/apn_on_rails/db/migrate/20090723132059_create_apn_notifications.rb", "lib/apn_on_rails/tasks/apn.rake", "lib/apn_on_rails/tasks/db.rake", "lib/apn_on_rails.rb", "lib/apn_on_rails_tasks.rb", "README", "LICENSE"]
+ s.files = ["lib/apn_on_rails/apn_on_rails.rb", "lib/apn_on_rails/app/models/apn/device.rb", "lib/apn_on_rails/app/models/apn/notification.rb", "lib/apn_on_rails/db/migrate/20090723132058_create_apn_devices.rb", "lib/apn_on_rails/db/migrate/20090723132059_create_apn_notifications.rb", "lib/apn_on_rails/tasks/apn.rake", "lib/apn_on_rails/tasks/db.rake", "lib/apn_on_rails.rb", "lib/apn_on_rails_tasks.rb", "README", "LICENSE"]
s.homepage = %q{}
s.require_paths = ["lib"]
s.rubyforge_project = %q{magrathea}
View
37 lib/apn_on_rails/app/models/apn/notification.rb
@@ -1,5 +1,7 @@
class APN::Notification < ActiveRecord::Base
include ::ActionView::Helpers::TextHelper
+ extend ::ActionView::Helpers::TextHelper
+
set_table_name 'apn_notifications'
belongs_to :device, :class_name => 'APN::Device'
@@ -53,25 +55,28 @@ def message_for_sending
class << self
- def send_notifications(notifications)
- cert = File.read(configatron.apn.cert)
- ctx = OpenSSL::SSL::SSLContext.new
- ctx.key = OpenSSL::PKey::RSA.new(cert, configatron.apn.passphrase)
- ctx.cert = OpenSSL::X509::Certificate.new(cert)
+ def send_notifications(notifications = APN::Notification.all(:conditions => {:sent_at => nil}))
+ unless notifications.nil? || notifications.empty?
+ logger.info "APN: Attempting to deliver #{pluralize(notifications.size, 'notification')}."
+ cert = File.read(configatron.apn.cert)
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.key = OpenSSL::PKey::RSA.new(cert, configatron.apn.passphrase)
+ ctx.cert = OpenSSL::X509::Certificate.new(cert)
- s = TCPSocket.new(configatron.apn.host, configatron.apn.port)
- ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
- ssl.sync = true
- ssl.connect
+ s = TCPSocket.new(configatron.apn.host, configatron.apn.port)
+ ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
+ ssl.sync = true
+ ssl.connect
- notifications.each do |noty|
- ssl.write(noty.message_for_sending)
- noty.sent_at = Time.now
- noty.save
- end
+ notifications.each do |noty|
+ ssl.write(noty.message_for_sending)
+ noty.sent_at = Time.now
+ noty.save
+ end
- ssl.close
- s.close
+ ssl.close
+ s.close
+ end
end
end # class << self
View
94 lib/apn_on_rails/app/models/apple_push_notification.rb
@@ -1,94 +0,0 @@
-# #
-# # Fabien Penso <fabien.penso@conovae.com>
-# # April 6th, 2009.
-# #
-#
-# require 'socket'
-# require 'openssl'
-#
-# class ApplePushNotification < ActiveRecord::Base
-#
-# HOST = "gateway.sandbox.push.apple.com"
-# PATH = '/'
-# PORT = 2195
-# path = File.join(RAILS_ROOT, 'config', 'apple_push_notification_development.pem')
-# CERT = File.read(path) if File.exists?(path)
-# PASSPHRASE = "foobar"
-# CACERT = File.expand_path(File.dirname(__FILE__) + "certs/ca.gateway.sandbox.push.apple.com.crt")
-# USERAGENT = 'Mozilla/5.0 (apple_push_notification Ruby on Rails 0.1)'
-#
-# # attr_accessor :paylod, :sound, :badge, :alert, :appdata
-# attr_accessible :device_token
-#
-# # validates_uniqueness_of :device_token
-#
-# # def send_notification
-# #
-# # ctx = OpenSSL::SSL::SSLContext.new
-# # ctx.key = OpenSSL::PKey::RSA.new(CERT, PASSPHRASE)
-# # ctx.cert = OpenSSL::X509::Certificate.new(CERT)
-# #
-# # s = TCPSocket.new(HOST, PORT)
-# # ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
-# # ssl.sync = true
-# # ssl.connect
-# #
-# # ssl.write(self.apn_message_for_sending)
-# #
-# # ssl.close
-# # s.close
-# #
-# # rescue SocketError => error
-# # raise "Error while sending notifications: #{error}"
-# # end
-#
-# def self.send_notifications(notifications)
-# ctx = OpenSSL::SSL::SSLContext.new
-# ctx.key = OpenSSL::PKey::RSA.new(CERT, PASSPHRASE)
-# ctx.cert = OpenSSL::X509::Certificate.new(CERT)
-#
-# s = TCPSocket.new(HOST, PORT)
-# ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
-# ssl.sync = true
-# ssl.connect
-#
-# for notif in notifications do
-# ssl.write(notif.apn_message_for_sending)
-# notif.sent_at = Time.now
-# notif.save
-# end
-#
-# ssl.close
-# s.close
-# rescue SocketError => error
-# raise "Error while sending notifications: #{error}"
-# end
-#
-# def to_apple_json
-# logger.debug "Sending #{self.apple_hash.to_json}"
-# self.apple_hash.to_json
-# end
-#
-# def apn_message_for_sending
-# json = self.to_apple_json
-# message = "\0\0 #{self.device_token_hexa}\0#{json.length.chr}#{json}"
-# raise "The maximum size allowed for a notification payload is 256 bytes." if message.size.to_i > 256
-# message
-# end
-#
-# def device_token_hexa
-# [self.device_token.delete(' ')].pack('H*')
-# end
-#
-# def apple_hash
-# result = {}
-# result['aps'] = {}
-# result['aps']['alert'] = alert if alert
-# result['aps']['badge'] = badge.to_i if badge
-# result['aps']['sound'] = sound if sound and sound.is_a? String
-# result['aps']['sound'] = "1.aiff" if sound and sound.is_a?(TrueClass)
-# result.merge appdata if appdata
-#
-# result
-# end
-# end
View
7 lib/apn_on_rails/tasks/apn.rake
@@ -4,12 +4,7 @@ namespace :apn do
desc "Deliver all unsent APN notifications."
task :deliver => [:environment] do
- notifications = APN::Notification.all(:conditions => {:sent_at => nil})
- unless notifications.empty?
- include ActionView::Helpers::TextHelper
- RAILS_DEFAULT_LOGGER.info "APN: Attempting to deliver #{pluralize(notifications.size, 'notification')}."
- APN::Notification.send_notifications(notifications)
- end
+ APN::Notification.send_notifications
end
end # notifications

0 comments on commit 525710c

Please sign in to comment.