rpush 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/README.md +24 -18
  4. data/bin/rpush +1 -42
  5. data/lib/generators/rpush_config_generator.rb +7 -0
  6. data/lib/generators/{rpush_generator.rb → rpush_migration_generator.rb} +6 -11
  7. data/lib/generators/templates/rpush.rb +12 -12
  8. data/lib/generators/templates/rpush_2_0_0_updates.rb +2 -1
  9. data/lib/generators/templates/rpush_2_1_0_updates.rb +11 -0
  10. data/lib/rpush.rb +12 -4
  11. data/lib/rpush/apns_feedback.rb +1 -1
  12. data/lib/rpush/cli.rb +133 -0
  13. data/lib/rpush/client/active_model/apns/binary_notification_validator.rb +2 -2
  14. data/lib/rpush/client/active_model/apns/notification.rb +4 -1
  15. data/lib/rpush/client/active_model/notification.rb +0 -4
  16. data/lib/rpush/client/active_record/apns/notification.rb +0 -6
  17. data/lib/rpush/client/active_record/notification.rb +3 -2
  18. data/lib/rpush/client/redis/notification.rb +2 -0
  19. data/lib/rpush/configuration.rb +16 -24
  20. data/lib/rpush/daemon.rb +28 -1
  21. data/lib/rpush/daemon/app_runner.rb +6 -1
  22. data/lib/rpush/daemon/proc_title.rb +1 -1
  23. data/lib/rpush/daemon/signal_handler.rb +13 -3
  24. data/lib/rpush/daemon/store/active_record.rb +1 -1
  25. data/lib/rpush/deprecatable.rb +2 -1
  26. data/lib/rpush/embed.rb +1 -1
  27. data/lib/rpush/logger.rb +32 -19
  28. data/lib/rpush/push.rb +1 -1
  29. data/lib/rpush/version.rb +1 -1
  30. data/lib/tasks/quality.rake +1 -1
  31. data/lib/tasks/test.rake +12 -0
  32. data/spec/functional/apns_spec.rb +13 -11
  33. data/spec/functional_spec_helper.rb +1 -4
  34. data/spec/spec_helper.rb +4 -4
  35. data/spec/support/active_record_setup.rb +2 -1
  36. data/spec/unit/client/active_record/apns/notification_spec.rb +29 -3
  37. data/spec/unit/configuration_spec.rb +0 -7
  38. data/spec/unit/daemon/app_runner_spec.rb +1 -1
  39. data/spec/unit/daemon/signal_handler_spec.rb +1 -1
  40. data/spec/unit/daemon_spec.rb +1 -1
  41. data/spec/unit/deprecatable_spec.rb +1 -1
  42. data/spec/unit/logger_spec.rb +4 -4
  43. metadata +48 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46ba458f1bce103da495d96eb5c823d75a8dbb65
4
- data.tar.gz: 39fd41cb301888ba9e93f6b01a76b3688ed2cf08
3
+ metadata.gz: bcc9513fb56f7be786d23e453cce23438c9ea9aa
4
+ data.tar.gz: cf5b79d6e9306478a509680494924b2e99335683
5
5
  SHA512:
6
- metadata.gz: e6818f8d0a1adc9990e1d7f28b2a4b4299ab1d38a9f43be7b23eb281a74d2743d72ab4bbc75dc7cf51c610afee9160f77ef2a929d344570d64f6da58053e6979
7
- data.tar.gz: 1eb53814eefda8f89a74f97dc85d7f8d54ff679041eb81ce1793c18be91655f7aae2e5518fceeaf61ad3ffbb127664a0b9e5446db2c307c67ff75e22ec8921ef
6
+ metadata.gz: a88119dfb66901691ec8d508f63f448b43d6387e7299b8c503c27ad04b3051056d9b51f2b593021163da1c1a072bd4ddc6cc006caceaa877947c2874095a7c8b
7
+ data.tar.gz: 8f636b57c346bfe27d16955876a47fa0c9def6a9c64bffd192c46474033bb5ef4885d8edea067d0e2c7ae10a64a87061f6e066e051001a1ed4db9cd43ac823fd
@@ -1,3 +1,12 @@
1
+ ## 2.1.0 (Oct 4, 2014)
2
+ * Bump APNs max payload size to 2048 for iOS 8.
3
+ * Add 'category' for iOS 8.
4
+ * Add url_args for Safari Push Notification Support (#77).
5
+ * Improved command-line interface.
6
+ * Rails integration is now optional.
7
+ * Added log_level config option.
8
+ * log_dir is now deprecated and has no effect, use log_file instead.
9
+
1
10
  ## 2.0.1 (Sept 13, 2014)
2
11
  * Add ssl_certificate_revoked reflection (#68).
3
12
  * Fix for Postgis support in 2.0.0 migration (#70).
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
- [![Build Status](https://secure.travis-ci.org/rpush/rpush.png?branch=master)](http://travis-ci.org/rpush/rpush)
2
- [![Code Climate](https://codeclimate.com/github/rpush/rpush.png)](https://codeclimate.com/github/rpush/rpush)
3
- [![Code Coverage](https://codeclimate.com/github/rpush/rpush/coverage.png)](https://codeclimate.com/github/rpush/rpush)
4
- [![Gem Version](https://badge.fury.io/rb/rpush.png)](http://badge.fury.io/rb/rpush)
1
+ [![Build Status](https://secure.travis-ci.org/rpush/rpush.svg?branch=master)](http://travis-ci.org/rpush/rpush)
2
+ [![Code Climate](https://codeclimate.com/github/rpush/rpush/badges/gpa.svg)](https://codeclimate.com/github/rpush/rpush)
3
+ [![Test Coverage](https://codeclimate.com/github/rpush/rpush/badges/coverage.svg)](https://codeclimate.com/github/rpush/rpush)
4
+ [![Gem Version](https://badge.fury.io/rb/rpush.svg)](http://badge.fury.io/rb/rpush)
5
5
 
6
6
  <img src="https://raw.github.com/rpush/rpush/master/logo.png" align="right" width="200px" />
7
7
 
@@ -9,16 +9,16 @@
9
9
 
10
10
  * Supported services:
11
11
  * [**Apple Push Notification Service**](#apple-push-notification-service)
12
+ * Including Safari Push Notifications.
12
13
  * [**Google Cloud Messaging**](#google-cloud-messaging)
13
14
  * [**Amazon Device Messaging**](#amazon-device-messaging)
14
15
  * [**Windows Phone Push Notification Service**](#windows-phone-notification-service)
15
16
 
16
17
  * Supported storage backends:
17
- * **ActiveRecord**
18
- * **Redis**
19
- * More coming!
18
+ * [**ActiveRecord**](https://github.com/rpush/rpush/wiki/Using-ActiveRecord)
19
+ * [**Redis**](https://github.com/rpush/rpush/wiki/Using-Redis)
20
20
 
21
- * Seamless Rails integration (3 & 4) .
21
+ * Optional Rails integration (version 3 & 4).
22
22
  * Scales vertically (threading) and horizontally (multiple processes).
23
23
  * Designed for uptime - new apps are loaded automatically, signal `HUP` to update running apps.
24
24
  * Run as a daemon or inside an [existing process](https://github.com/rpush/rpush/wiki/Embedding-API).
@@ -35,11 +35,11 @@ Add it to your Gemfile:
35
35
  gem 'rpush'
36
36
  ```
37
37
 
38
- Generate the migrations, rpush.rb and migrate:
38
+ Initialize Rpush into your project. Rails will be detected automatically.
39
39
 
40
40
  ```
41
- rails g rpush
42
- rake db:migrate
41
+ cd /path/to/project
42
+ rpush init
43
43
  ```
44
44
 
45
45
  ### Create an App & Notification
@@ -67,6 +67,8 @@ n.data = { foo: :bar }
67
67
  n.save!
68
68
  ```
69
69
 
70
+ The `url_args` attribute is available for Safari Push Notifications.
71
+
70
72
  You should also implement the [ssl_certificate_will_expire](https://github.com/rpush/rpush/wiki/Reflection-API) reflection to monitor when your certificate is due to expire.
71
73
 
72
74
  #### Google Cloud Messaging
@@ -134,13 +136,17 @@ It is recommended to run Rpush as a separate process in most cases, though embed
134
136
 
135
137
  #### As a daemon (recommended):
136
138
 
137
- cd /path/to/rails/app
138
- rpush <Rails environment> [options]
139
+ ```
140
+ cd /path/to/project
141
+ rpush start
142
+ ```
143
+
144
+ See `rpush help` for all available commands and options.
139
145
 
140
146
  #### Embedded inside an existing process
141
147
 
142
148
  ```ruby
143
- # Call this during startup of your application, for example, by adding it to the end of config/initializers/rpush.rb
149
+ # Call this during startup of your application, for example, by adding it to the end of config/rpush.rb
144
150
  Rpush.embed
145
151
  ```
146
152
 
@@ -157,11 +163,11 @@ See [Push API](https://github.com/rpush/rpush/wiki/Push-API) for more details.
157
163
 
158
164
  ### Configuration
159
165
 
160
- See [Configuration](https://github.com/rpush/rpush/wiki/Configuration) for a list of options, or run `rpush --help`.
166
+ See [Configuration](https://github.com/rpush/rpush/wiki/Configuration) for a list of options.
161
167
 
162
168
  ### Updating Rpush
163
169
 
164
- If you're using ActiveRecord, you should run `rails g rpush` after upgrading Rpush to check for any new migrations.
170
+ You should run `rails init` after upgrading Rpush to check for configuration and migration changes.
165
171
 
166
172
  ### Wiki
167
173
 
@@ -191,8 +197,6 @@ If you're using ActiveRecord, you should run `rails g rpush` after upgrading Rpu
191
197
 
192
198
  ### Contributing
193
199
 
194
- Fork as usual and go crazy!
195
-
196
200
  When running specs, please note that the ActiveRecord adapter can be changed by setting the `ADAPTER` environment variable. For example: `ADAPTER=postgresql rake`.
197
201
 
198
202
  Available adapters for testing are `mysql`, `mysql2` and `postgresql`.
@@ -200,3 +204,5 @@ Available adapters for testing are `mysql`, `mysql2` and `postgresql`.
200
204
  Note that the database username is changed at runtime to be the currently logged in user's name. So if you're testing
201
205
  with mysql and you're using a user named 'bob', you will need to grant a mysql user 'bob' access to the 'rpush_test'
202
206
  mysql database.
207
+
208
+ To switch between ActiveRecord and Redis, set the `CLIENT` environment variable to either `:active_record` or `:redis`.
data/bin/rpush CHANGED
@@ -1,45 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'optparse'
4
3
  require 'rpush'
5
-
6
- environment = ARGV[0]
7
-
8
- config = Rpush::ConfigurationWithoutDefaults.new
9
-
10
- options = ARGV.options do |opts|
11
- opts.banner = 'Usage: rpush <Rails environment> [options]'
12
- opts.on('-f', '--foreground', 'Run in the foreground.') { config.foreground = true }
13
- opts.on('-P N', '--db-poll N', Integer, "Frequency in seconds to check for new notifications.") { |n| config.push_poll = n }
14
- opts.on('-F N', '--feedback-poll N', Integer, "Frequency in seconds to check for feedback.") { |n| config.feedback_poll = n }
15
- opts.on('-e', '--no-error-checks', 'Disable APNs error checking after notification delivery.') { config.check_for_errors = false } # deprecated
16
- opts.on('-p PATH', '--pid-file PATH', String, 'Path to write PID file. Relative to Rails root unless absolute.') { |path| config.pid_file = path }
17
- opts.on('-b N', '--batch-size N', Integer, 'Storage backend notification batch size.') { |n| config.batch_size = n }
18
- opts.on('-B', '--[no-]batch-storage-updates', 'Perform storage updates in batches.') { |v| config.batch_storage_updates = v } # deprecated
19
- opts.on('-v', '--version', 'Print the version.') do
20
- puts "rpush #{Rpush::VERSION}"
21
- exit
22
- end
23
- opts.on('-h', '--help', 'You\'re looking at it.') do
24
- hidden_switches = %w(-e --no-error-checks)
25
- puts opts.to_s.split("\n").delete_if do |line|
26
- hidden_switches.any? { |s| line =~ /#{s}/ }
27
- end.join("\n")
28
- exit
29
- end
30
- end
31
-
32
- if environment.nil? || environment =~ /^-/
33
- puts options.to_s
34
- exit 1
35
- end
36
-
37
- options.parse!
38
-
39
- ENV['RAILS_ENV'] = environment
40
- load 'config/environment.rb'
41
- load 'config/initializers/rpush.rb' if File.exist?('config/initializers/rpush.rb')
42
-
43
- Rpush.config.update(config)
44
- Rpush.require_for_daemon
45
- Rpush::Daemon.start
4
+ Rpush::CLI.start(ARGV)
@@ -0,0 +1,7 @@
1
+ class RpushConfigGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def copy_config
5
+ copy_file 'rpush.rb', $RPUSH_CONFIG_PATH
6
+ end
7
+ end
@@ -1,4 +1,4 @@
1
- class RpushGenerator < Rails::Generators::Base
1
+ class RpushMigrationGenerator < Rails::Generators::Base
2
2
  include Rails::Generators::Migration
3
3
  source_root File.expand_path('../templates', __FILE__)
4
4
 
@@ -25,22 +25,17 @@ class RpushGenerator < Rails::Generators::Base
25
25
  end
26
26
 
27
27
  add_rpush_migration('rpush_2_0_0_updates')
28
- end
29
-
30
- def copy_config
31
- copy_file 'rpush.rb', 'config/initializers/rpush.rb'
28
+ add_rpush_migration('rpush_2_1_0_updates')
32
29
  end
33
30
 
34
31
  protected
35
32
 
36
- def add_rpush_migration(template)
37
- if !has_migration?(template)
38
- migration_template "#{template}.rb", "db/migrate/#{template}.rb"
39
- end
40
- end
41
-
42
33
  def has_migration?(template)
43
34
  migration_dir = File.expand_path('db/migrate')
44
35
  self.class.migration_exists?(migration_dir, template)
36
+ end
37
+
38
+ def add_rpush_migration(template)
39
+ migration_template "#{template}.rb", "db/migrate/#{template}.rb"
45
40
  end
46
41
  end
@@ -1,30 +1,30 @@
1
- # Rpush configuration. Options set here are overridden by command-line options.
2
-
3
- Rpush.configure do |config|
1
+ Rpush.configure do |config|
4
2
 
5
3
  # Supported clients are :active_record and :redis.
6
- # config.client = :active_record
4
+ config.client = :active_record
7
5
 
8
6
  # Options passed to Redis.new
9
7
  # config.redis_options = {}
10
8
 
11
- # Run in the foreground?
12
- # config.foreground = false
13
-
14
9
  # Frequency in seconds to check for new notifications.
15
- # config.push_poll = 2
10
+ config.push_poll = 2
16
11
 
17
12
  # Frequency in seconds to check for feedback
18
- # config.feedback_poll = 60
13
+ config.feedback_poll = 60
19
14
 
20
15
  # The maximum number of notifications to load from the store every `push_poll` seconds.
21
16
  # If some notifications are still enqueued internally, Rpush will load the batch_size less
22
17
  # the number enqueued. An exception to this is if the service is able to receive multiple
23
18
  # notification payloads over the connection with a single write, such as APNs.
24
- # config.batch_size = 100
19
+ config.batch_size = 100
20
+
21
+ # Path to write PID file. Relative to current directory unless absolute.
22
+ config.pid_file = 'tmp/rpush.pid'
23
+
24
+ # Path to log file. Relative to current directory unless absolute.
25
+ config.log_file = 'log/rpush.log'
25
26
 
26
- # Path to write PID file. Relative to Rails root unless absolute.
27
- # config.pid_file = '/path/to/rpush.pid'
27
+ config.log_level = (defined?(Rails) && Rails.logger) ? Rails.logger.level : ::Logger::Severity::INFO
28
28
 
29
29
  # Define a custom logger.
30
30
  # config.logger = MyLogger.new
@@ -33,7 +33,8 @@ class Rpush200Updates < ActiveRecord::Migration
33
33
  end
34
34
 
35
35
  def self.adapter_name
36
- ActiveRecord::Base.configurations[Rails.env]['adapter']
36
+ env = (defined?(Rails) && Rails.env) ? Rails.env : 'development'
37
+ ActiveRecord::Base.configurations[env]['adapter']
37
38
  end
38
39
 
39
40
  def self.postgresql?
@@ -0,0 +1,11 @@
1
+ class Rpush210Updates < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :rpush_notifications, :url_args, :text, null: true
4
+ add_column :rpush_notifications, :category, :string, null: true
5
+ end
6
+
7
+ def self.down
8
+ remove_column :rpush_notifications, :url_args
9
+ remove_column :rpush_notifications, :category
10
+ end
11
+ end
@@ -1,14 +1,19 @@
1
+ require 'logger'
1
2
  require 'multi_json'
2
3
  require 'active_support/all'
3
4
 
4
5
  module Rpush
5
6
  def self.attr_accessible_available?
6
7
  require 'rails'
8
+ rescue LoadError
9
+ false
10
+ else
7
11
  ::Rails::VERSION::STRING < '4' || defined?(::ActiveRecord::MassAssignmentSecurity)
8
12
  end
9
13
  end
10
14
 
11
15
  require 'rpush/version'
16
+ require 'rpush/cli'
12
17
  require 'rpush/deprecation'
13
18
  require 'rpush/deprecatable'
14
19
  require 'rpush/logger'
@@ -24,14 +29,17 @@ module Rpush
24
29
  defined? JRUBY_VERSION
25
30
  end
26
31
 
27
- def self.require_for_daemon
28
- require 'rpush/daemon'
29
- end
30
-
31
32
  def self.logger
32
33
  @logger ||= Logger.new
33
34
  end
34
35
 
36
+ def self.root
37
+ require 'rails'
38
+ Rails.root || Dir.pwd
39
+ rescue LoadError
40
+ Dir.pwd
41
+ end
42
+
35
43
  class << self
36
44
  attr_writer :logger
37
45
  end
@@ -1,6 +1,6 @@
1
1
  module Rpush
2
2
  def self.apns_feedback
3
- Rpush.require_for_daemon
3
+ require 'rpush/daemon'
4
4
  Rpush::Daemon.initialize_store
5
5
 
6
6
  Rpush::Apns::App.all.each do |app|
@@ -0,0 +1,133 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'thor'
4
+ require 'term/ansicolor'
5
+
6
+ module Rpush
7
+ class CLI < Thor
8
+ include Term::ANSIColor
9
+
10
+ def self.detect_rails?
11
+ ['bin/rails', 'script/rails'].any? { |path| File.exist?(path) }
12
+ end
13
+
14
+ def self.default_config_path
15
+ detect_rails? ? 'config/initializers/rpush.rb' : 'config/rpush.rb'
16
+ end
17
+
18
+ class_option :config, type: :string, aliases: '-c', default: default_config_path
19
+ class_option :rails_env, type: :string, aliases: '-e', default: 'development'
20
+
21
+ option :foreground, type: :boolean, aliases: '-f', default: false
22
+ option 'pid-file', type: :string, aliases: '-p'
23
+ desc 'start', 'Start Rpush'
24
+ def start
25
+ check_ruby_version
26
+
27
+ if detect_rails? && options[:rails_env]
28
+ STDOUT.write "* Booting Rails '#{options[:rails_env]}' environment... "
29
+ STDOUT.flush
30
+ ENV['RAILS_ENV'] = options[:rails_env]
31
+ load 'config/environment.rb'
32
+ puts green('✔')
33
+ end
34
+
35
+ load_config
36
+ require 'rpush/daemon'
37
+ Rpush::Daemon.start
38
+ end
39
+
40
+ desc 'stop', 'Stop Rpush'
41
+ option 'pid-file', type: :string, aliases: '-p'
42
+ def stop
43
+ check_ruby_version
44
+ load_config
45
+ ensure_pid_file
46
+
47
+ if File.exist?(Rpush.config.pid_file)
48
+ pid = File.read(Rpush.config.pid_file).strip.to_i
49
+ STDOUT.write "* Stopping Rpush (pid #{pid})... "
50
+ STDOUT.flush
51
+ Process.kill('TERM', pid)
52
+
53
+ loop do
54
+ begin
55
+ Process.getpgid(pid)
56
+ sleep 0.05
57
+ rescue Errno::ESRCH
58
+ break
59
+ end
60
+ end
61
+
62
+ puts green('✔')
63
+ else
64
+ STDERR.puts("* Rpush isn't running? #{Rpush.config.pid_file} does not exist.")
65
+ return
66
+ end
67
+ end
68
+
69
+ desc 'init', 'Initialize Rpush into the current directory.'
70
+ option 'active-record', type: :boolean, desc: 'Install ActiveRecord migrations.'
71
+ def init
72
+ check_ruby_version
73
+ require 'rails/generators'
74
+
75
+ puts "* #{green('Installing config...')}"
76
+ $RPUSH_CONFIG_PATH = default_config_path # rubocop:disable Style/GlobalVars
77
+ Rails::Generators.invoke('rpush_config')
78
+
79
+ install_migrations = options['active-record']
80
+
81
+ unless options.key?('active-record')
82
+ has_answer = false
83
+ until has_answer
84
+ STDOUT.write "\n* #{green('Install ActiveRecord migrations?')} [y/n]: "
85
+ STDOUT.flush
86
+ answer = STDIN.gets.chomp.downcase
87
+ has_answer = %w(y n).include?(answer)
88
+ end
89
+
90
+ install_migrations = answer == 'y'
91
+ end
92
+
93
+ Rails::Generators.invoke('rpush_migration') if install_migrations
94
+
95
+ puts "\n* #{green('Next steps:')}"
96
+ puts " - Run 'db:migrate'." if install_migrations
97
+ puts " - Review and update your configuration in #{default_config_path}."
98
+ puts " - Create your first app, see https://github.com/rpush/rpush for examples."
99
+ puts " - Run 'rpush help' for commands and options."
100
+ end
101
+
102
+ private
103
+
104
+ def detect_rails?
105
+ self.class.detect_rails?
106
+ end
107
+
108
+ def default_config_path
109
+ self.class.default_config_path
110
+ end
111
+
112
+ def ensure_pid_file
113
+ return if Rpush.config.pid_file.blank?
114
+
115
+ STDERR.puts(red('ERROR: ') + 'config.pid_file is not set.')
116
+ exit 1
117
+ end
118
+
119
+ def load_config
120
+ if !File.exist?(options[:config])
121
+ STDERR.puts(red('ERROR: ') + "#{options[:config]} does not exist. Please run 'rpush init' to generate it or specify the --config option.")
122
+ exit 1
123
+ else
124
+ load options[:config]
125
+ Rpush.config.update(options)
126
+ end
127
+ end
128
+
129
+ def check_ruby_version
130
+ STDERR.puts(yellow('WARNING: ') + "You are using an old and unsupported version of Ruby.") if RUBY_VERSION <= '1.9.3'
131
+ end
132
+ end
133
+ end