jirapong-apn_on_rails 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +2 -0
  3. data/.specification +80 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +47 -0
  6. data/LICENSE +21 -0
  7. data/README +179 -0
  8. data/README.textile +224 -0
  9. data/Rakefile +35 -0
  10. data/apn_on_rails.gemspec +32 -0
  11. data/autotest/discover.rb +1 -0
  12. data/generators/apn_migrations_generator.rb +33 -0
  13. data/generators/templates/apn_migrations/001_create_apn_devices.rb +13 -0
  14. data/generators/templates/apn_migrations/002_create_apn_notifications.rb +23 -0
  15. data/generators/templates/apn_migrations/003_alter_apn_devices.rb +23 -0
  16. data/generators/templates/apn_migrations/004_create_apn_apps.rb +25 -0
  17. data/generators/templates/apn_migrations/005_create_groups.rb +23 -0
  18. data/generators/templates/apn_migrations/006_alter_apn_groups.rb +11 -0
  19. data/generators/templates/apn_migrations/007_create_device_groups.rb +27 -0
  20. data/generators/templates/apn_migrations/008_create_apn_group_notifications.rb +23 -0
  21. data/generators/templates/apn_migrations/009_create_pull_notifications.rb +16 -0
  22. data/generators/templates/apn_migrations/010_alter_apn_notifications.rb +21 -0
  23. data/generators/templates/apn_migrations/011_make_device_token_index_nonunique.rb +11 -0
  24. data/generators/templates/apn_migrations/012_add_launch_notification_to_apn_pull_notifications.rb +9 -0
  25. data/lib/apn_on_rails.rb +4 -0
  26. data/lib/apn_on_rails/apn_on_rails.rb +81 -0
  27. data/lib/apn_on_rails/app/models/apn/app.rb +151 -0
  28. data/lib/apn_on_rails/app/models/apn/base.rb +11 -0
  29. data/lib/apn_on_rails/app/models/apn/device.rb +49 -0
  30. data/lib/apn_on_rails/app/models/apn/device_grouping.rb +16 -0
  31. data/lib/apn_on_rails/app/models/apn/group.rb +12 -0
  32. data/lib/apn_on_rails/app/models/apn/group_notification.rb +79 -0
  33. data/lib/apn_on_rails/app/models/apn/notification.rb +93 -0
  34. data/lib/apn_on_rails/app/models/apn/pull_notification.rb +28 -0
  35. data/lib/apn_on_rails/libs/connection.rb +70 -0
  36. data/lib/apn_on_rails/libs/feedback.rb +39 -0
  37. data/lib/apn_on_rails/rails/railtie.rb +15 -0
  38. data/lib/apn_on_rails/tasks/apn.rake +30 -0
  39. data/lib/apn_on_rails/tasks/db.rake +23 -0
  40. data/lib/apn_on_rails/version.rb +3 -0
  41. data/lib/apn_on_rails_tasks.rb +3 -0
  42. data/lib/generators/apn_on_rails/install/USAGE +8 -0
  43. data/lib/generators/apn_on_rails/install/install_generator.rb +38 -0
  44. data/lib/generators/apn_on_rails/install/templates/001_create_apn_devices.rb +13 -0
  45. data/lib/generators/apn_on_rails/install/templates/002_create_apn_notifications.rb +23 -0
  46. data/lib/generators/apn_on_rails/install/templates/003_alter_apn_devices.rb +25 -0
  47. data/lib/generators/apn_on_rails/install/templates/004_create_apn_apps.rb +18 -0
  48. data/lib/generators/apn_on_rails/install/templates/005_create_groups.rb +23 -0
  49. data/lib/generators/apn_on_rails/install/templates/006_alter_apn_groups.rb +11 -0
  50. data/lib/generators/apn_on_rails/install/templates/007_create_device_groups.rb +27 -0
  51. data/lib/generators/apn_on_rails/install/templates/008_create_apn_group_notifications.rb +23 -0
  52. data/lib/generators/apn_on_rails/install/templates/009_create_pull_notifications.rb +16 -0
  53. data/lib/generators/apn_on_rails/install/templates/010_alter_apn_notifications.rb +21 -0
  54. data/lib/generators/apn_on_rails/install/templates/011_make_device_token_index_nonunique.rb +11 -0
  55. data/lib/generators/apn_on_rails/install/templates/012_add_launch_notification_to_apn_pull_notifications.rb +9 -0
  56. data/spec/active_record/setup_ar.rb +20 -0
  57. data/spec/apn_on_rails/app/models/apn/app_spec.rb +230 -0
  58. data/spec/apn_on_rails/app/models/apn/device_spec.rb +61 -0
  59. data/spec/apn_on_rails/app/models/apn/group_notification_spec.rb +66 -0
  60. data/spec/apn_on_rails/app/models/apn/notification_spec.rb +71 -0
  61. data/spec/apn_on_rails/app/models/apn/pull_notification_spec.rb +100 -0
  62. data/spec/apn_on_rails/libs/connection_spec.rb +40 -0
  63. data/spec/apn_on_rails/libs/feedback_spec.rb +43 -0
  64. data/spec/extensions/string.rb +10 -0
  65. data/spec/factories/app_factory.rb +27 -0
  66. data/spec/factories/device_factory.rb +29 -0
  67. data/spec/factories/device_grouping_factory.rb +22 -0
  68. data/spec/factories/group_factory.rb +27 -0
  69. data/spec/factories/group_notification_factory.rb +22 -0
  70. data/spec/factories/notification_factory.rb +22 -0
  71. data/spec/factories/pull_notification_factory.rb +22 -0
  72. data/spec/fixtures/hexa.bin +1 -0
  73. data/spec/fixtures/message_for_sending.bin +0 -0
  74. data/spec/rails_root/config/apple_push_notification_development.pem +19 -0
  75. data/spec/spec_helper.rb +64 -0
  76. metadata +242 -0
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require "bundler/gem_tasks"
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require 'rake'
14
+ require 'rspec/core'
15
+ require 'rspec/core/rake_task'
16
+ RSpec::Core::RakeTask.new(:spec) do |spec|
17
+ spec.pattern = FileList['spec/**/*_spec.rb']
18
+ end
19
+
20
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
21
+ spec.pattern = 'spec/**/*_spec.rb'
22
+ spec.rcov = true
23
+ end
24
+
25
+ task :default => :spec
26
+
27
+ require 'rake/rdoctask'
28
+ Rake::RDocTask.new do |rdoc|
29
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
30
+
31
+ rdoc.rdoc_dir = 'rdoc'
32
+ rdoc.title = "apn #{version}"
33
+ rdoc.rdoc_files.include('README*')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "apn_on_rails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{jirapong-apn_on_rails}
7
+ s.version = ApnOnRails::VERSION
8
+ s.authors = ["markbates", "Rebecca Nesson"]
9
+ s.email = %q{tech-team@prx.org}
10
+ s.homepage = %q{http://github.com/PRX/apn_on_rails}
11
+ s.summary = %q{Apple Push Notifications on Rails}
12
+ s.description = %q{APN on Rails is a Ruby on Rails gem that allows you to
13
+ easily add Apple Push Notification (iPhone) support to your Rails application.
14
+ }
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency('configatron')
22
+ s.add_runtime_dependency('activerecord')
23
+ s.add_runtime_dependency('actionpack')
24
+
25
+ s.add_development_dependency('rake')
26
+ s.add_development_dependency('autotest')
27
+ s.add_development_dependency('sqlite3')
28
+ s.add_development_dependency('rspec')
29
+ s.add_development_dependency('bundler')
30
+ s.add_development_dependency('rcov')
31
+ end
32
+
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,33 @@
1
+ require 'rails_generator'
2
+ require 'rails/generators/migration'
3
+ # Generates the migrations necessary for APN on Rails.
4
+ # This should be run upon install and upgrade of the
5
+ # APN on Rails gem.
6
+ #
7
+ # $ ruby script/generate apn_migrations
8
+ class ApnMigrationsGenerator < Rails::Generator::Base
9
+ desc "Generates the migrations necessary for APN on Rails."
10
+
11
+ def manifest # :nodoc:
12
+ record do |m|
13
+ timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
14
+ db_migrate_path = File.join('db', 'migrate')
15
+
16
+ m.directory(db_migrate_path)
17
+
18
+ Dir.glob(File.join(File.dirname(__FILE__), 'templates', 'apn_migrations', '*.rb')).sort.each_with_index do |f, i|
19
+ f = File.basename(f)
20
+ f.match(/\d+\_(.+)/)
21
+ timestamp = timestamp.succ
22
+ if Dir.glob(File.join(db_migrate_path, "*_#{$1}")).empty?
23
+ m.file(File.join('apn_migrations', f),
24
+ File.join(db_migrate_path, "#{timestamp}_#{$1}"),
25
+ {:collision => :skip})
26
+ end
27
+ end
28
+
29
+ end # record
30
+
31
+ end # manifest
32
+
33
+ end # ApnMigrationsGenerator
@@ -0,0 +1,13 @@
1
+ class CreateApnDevices < ActiveRecord::Migration # :nodoc:
2
+ def self.up
3
+ create_table :apn_devices do |t|
4
+ t.text :token, :size => 71, :null => false
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :apn_devices
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ class CreateApnNotifications < ActiveRecord::Migration # :nodoc:
2
+
3
+ def self.up
4
+
5
+ create_table :apn_notifications do |t|
6
+ t.integer :device_id, :null => false
7
+ t.integer :errors_nb, :default => 0 # used for storing errors from apple feedbacks
8
+ t.string :device_language, :size => 5 # if you don't want to send localized strings
9
+ t.string :sound
10
+ t.string :alert, :size => 150
11
+ t.integer :badge
12
+ t.datetime :sent_at
13
+ t.timestamps
14
+ end
15
+
16
+ add_index :apn_notifications, :device_id
17
+ end
18
+
19
+ def self.down
20
+ drop_table :apn_notifications
21
+ end
22
+
23
+ end
@@ -0,0 +1,23 @@
1
+ class AlterApnDevices < ActiveRecord::Migration # :nodoc:
2
+
3
+ module APN # :nodoc:
4
+ class Device < ActiveRecord::Base # :nodoc:
5
+ set_table_name 'apn_devices'
6
+ end
7
+ end
8
+
9
+ def self.up
10
+ add_column :apn_devices, :last_registered_at, :datetime
11
+
12
+ execute 'update apn_devices set last_registered_at = created_at'
13
+
14
+ change_column :apn_devices, :token, :string, :size => 100, :null => false
15
+ add_index :apn_devices, :token, :unique => true
16
+ end
17
+
18
+ def self.down
19
+ change_column :apn_devices, :token, :string
20
+ remove_index :apn_devices, :column => :token
21
+ remove_column :apn_devices, :last_registered_at
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ class CreateApnApps < ActiveRecord::Migration # :nodoc:
2
+
3
+ module APN # :nodoc:
4
+ class Device < ActiveRecord::Base # :nodoc:
5
+ set_table_name 'apn_devices'
6
+ end
7
+ end
8
+
9
+ def self.up
10
+ create_table :apn_apps do |t|
11
+ t.text :apn_dev_cert
12
+ t.text :apn_prod_cert
13
+
14
+ t.timestamps
15
+ end
16
+
17
+ add_column :apn_devices, :app_id, :integer
18
+
19
+ end
20
+
21
+ def self.down
22
+ drop_table :apn_apps
23
+ remove_column :apn_devices, :app_id
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ class CreateGroups < ActiveRecord::Migration # :nodoc:
2
+ def self.up
3
+ create_table :apn_groups do |t|
4
+ t.column :name, :string
5
+
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :apn_devices_apn_groups, :id => false do |t|
10
+ t.column :group_id, :integer
11
+ t.column :device_id, :integer
12
+ end
13
+
14
+ add_index :apn_devices_apn_groups, [:group_id, :device_id]
15
+ add_index :apn_devices_apn_groups, :device_id
16
+ add_index :apn_devices_apn_groups, :group_id
17
+ end
18
+
19
+ def self.down
20
+ drop_table :apn_groups
21
+ drop_table :apn_devices_apn_groups
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ class AlterApnGroups < ActiveRecord::Migration # :nodoc:
2
+
3
+ def self.up
4
+ add_column :apn_groups, :app_id, :integer
5
+ end
6
+
7
+ def self.down
8
+ remove_column :apn_groups, :app_id
9
+ end
10
+
11
+ end
@@ -0,0 +1,27 @@
1
+ class CreateDeviceGroups < ActiveRecord::Migration # :nodoc:
2
+ def self.up
3
+ drop_table :apn_devices_apn_groups
4
+
5
+ create_table :apn_device_groupings do |t|
6
+ t.column :group_id, :integer
7
+ t.column :device_id, :integer
8
+ end
9
+
10
+ add_index :apn_device_groupings, [:group_id, :device_id]
11
+ add_index :apn_device_groupings, :device_id
12
+ add_index :apn_device_groupings, :group_id
13
+ end
14
+
15
+ def self.down
16
+ drop_table :apn_device_groupings
17
+
18
+ create_table :apn_devices_apn_groups, :id => false do |t|
19
+ t.column :group_id, :integer
20
+ t.column :device_id, :integer
21
+ end
22
+
23
+ add_index :apn_devices_apn_groups, [:group_id, :device_id]
24
+ add_index :apn_devices_apn_groups, :device_id
25
+ add_index :apn_devices_apn_groups, :group_id
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ class CreateApnGroupNotifications < ActiveRecord::Migration # :nodoc:
2
+
3
+ def self.up
4
+
5
+ create_table :apn_group_notifications do |t|
6
+ t.integer :group_id, :null => false
7
+ t.string :device_language, :size => 5 # if you don't want to send localized strings
8
+ t.string :sound
9
+ t.string :alert, :size => 150
10
+ t.integer :badge
11
+ t.text :custom_properties
12
+ t.datetime :sent_at
13
+ t.timestamps
14
+ end
15
+
16
+ add_index :apn_group_notifications, :group_id
17
+ end
18
+
19
+ def self.down
20
+ drop_table :apn_group_notifications
21
+ end
22
+
23
+ end
@@ -0,0 +1,16 @@
1
+ class CreatePullNotifications < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :apn_pull_notifications do |t|
4
+ t.integer :app_id
5
+ t.string :title
6
+ t.string :content
7
+ t.string :link
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :apn_pull_notifications
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ class AlterApnNotifications < ActiveRecord::Migration # :nodoc:
2
+
3
+ module APN # :nodoc:
4
+ class Notification < ActiveRecord::Base # :nodoc:
5
+ set_table_name 'apn_notifications'
6
+ end
7
+ end
8
+
9
+ def self.up
10
+ # unless APN::Notification.column_names.include?("custom_properties")
11
+ add_column(:apn_notifications, :custom_properties, :text) rescue nil
12
+ # end
13
+ end
14
+
15
+ def self.down
16
+ # if APN::Notification.column_names.include?("custom_properties")
17
+ remove_column(:apn_notifications, :custom_properties) rescue nil
18
+ # end
19
+ end
20
+
21
+ end
@@ -0,0 +1,11 @@
1
+ class MakeDeviceTokenIndexNonunique < ActiveRecord::Migration
2
+ def self.up
3
+ remove_index :apn_devices, :column => :token
4
+ add_index :apn_devices, :token
5
+ end
6
+
7
+ def self.down
8
+ remove_index :apn_devices, :column => :token
9
+ add_index :apn_devices, :token, :unique => true
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class AddLaunchNotificationToApnPullNotifications < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :apn_pull_notifications, :launch_notification, :boolean
4
+ end
5
+
6
+ def self.down
7
+ remove_column :apn_pull_notifications, :launch_notification
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ Dir.glob(File.join(File.dirname(__FILE__), 'apn_on_rails', '**/*.rb')).sort.each do |f|
2
+ require File.expand_path(f)
3
+ end
4
+
@@ -0,0 +1,81 @@
1
+ require 'socket'
2
+ require 'openssl'
3
+ require 'configatron'
4
+
5
+ rails_root = File.join(FileUtils.pwd, 'rails_root')
6
+ if defined?(Rails.root)
7
+ rails_root = Rails.root.to_s
8
+ end
9
+
10
+ rails_env = 'development'
11
+ if defined?(Rails.env)
12
+ rails_env = Rails.env
13
+ end
14
+
15
+ configatron.apn.set_default(:passphrase, '')
16
+ configatron.apn.set_default(:port, 2195)
17
+
18
+ configatron.apn.feedback.set_default(:passphrase, configatron.apn.passphrase)
19
+ configatron.apn.feedback.set_default(:port, 2196)
20
+
21
+ if rails_env == 'production'
22
+ configatron.apn.set_default(:host, 'gateway.push.apple.com')
23
+ configatron.apn.set_default(:cert, File.join(rails_root, 'config', 'apple_push_notification_production.pem'))
24
+
25
+ configatron.apn.feedback.set_default(:host, 'feedback.push.apple.com')
26
+ configatron.apn.feedback.set_default(:cert, configatron.apn.cert)
27
+ else
28
+ configatron.apn.set_default(:host, 'gateway.sandbox.push.apple.com')
29
+ configatron.apn.set_default(:cert, File.join(rails_root, 'config', 'apple_push_notification_development.pem'))
30
+
31
+ configatron.apn.feedback.set_default(:host, 'feedback.sandbox.push.apple.com')
32
+ configatron.apn.feedback.set_default(:cert, configatron.apn.cert)
33
+ end
34
+
35
+ module APN # :nodoc:
36
+
37
+ module Errors # :nodoc:
38
+
39
+ # Raised when a notification message to Apple is longer than 256 bytes.
40
+ class ExceededMessageSizeError < StandardError
41
+
42
+ def initialize(message) # :nodoc:
43
+ super("The maximum size allowed for a notification payload is 256 bytes: '#{message}'")
44
+ end
45
+
46
+ end
47
+
48
+ class MissingCertificateError < StandardError
49
+ def initialize
50
+ super("This app has no certificate")
51
+ end
52
+ end
53
+
54
+ end # Errors
55
+
56
+ end # APN
57
+
58
+ base = File.join(File.dirname(__FILE__), 'app', 'models', 'apn', 'base.rb')
59
+ require base
60
+
61
+ Dir.glob(File.join(File.dirname(__FILE__), 'app', 'models', 'apn', '*.rb')).sort.each do |f|
62
+ require f
63
+ end
64
+
65
+ %w{ models controllers helpers }.each do |dir|
66
+ path = File.join(File.dirname(__FILE__), 'app', dir)
67
+ $LOAD_PATH << path
68
+ # puts "Adding #{path}"
69
+ begin
70
+ if ActiveSupport::Dependencies.respond_to? :autoload_paths
71
+ ActiveSupport::Dependencies.autoload_paths << path
72
+ ActiveSupport::Dependencies.autoload_once_paths.delete(path)
73
+ else
74
+ ActiveSupport::Dependencies.load_paths << path
75
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
76
+ end
77
+ rescue NameError
78
+ Dependencies.load_paths << path
79
+ Dependencies.load_once_paths.delete(path)
80
+ end
81
+ end