rapnd-mikec54088 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "hiredis", "~> 0.4.4"
4
+ gem "redis", "~> 2.2.2", :require => ["redis/connection/hiredis", "redis"]
5
+ gem 'activesupport'
6
+ gem 'daemons', '1.1.6'
7
+ gem 'i18n'
8
+ gem 'airbrake'
9
+
10
+ group :development do
11
+ gem 'mocha'
12
+ gem "rspec"
13
+ gem "bundler"
14
+ gem "jeweler"
15
+ gem "rcov"
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.0)
5
+ airbrake (3.0.9)
6
+ activesupport
7
+ builder
8
+ builder (3.0.0)
9
+ daemons (1.1.6)
10
+ diff-lcs (1.1.3)
11
+ git (1.2.5)
12
+ hiredis (0.4.4)
13
+ i18n (0.6.0)
14
+ jeweler (1.6.4)
15
+ bundler (~> 1.0)
16
+ git (>= 1.2.5)
17
+ rake
18
+ metaclass (0.0.1)
19
+ mocha (0.10.3)
20
+ metaclass (~> 0.0.1)
21
+ rake (0.9.3.beta.1)
22
+ rcov (0.9.11)
23
+ redis (2.2.2)
24
+ rspec (2.8.0)
25
+ rspec-core (~> 2.8.0)
26
+ rspec-expectations (~> 2.8.0)
27
+ rspec-mocks (~> 2.8.0)
28
+ rspec-core (2.8.0)
29
+ rspec-expectations (2.8.0)
30
+ diff-lcs (~> 1.1.2)
31
+ rspec-mocks (2.8.0)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ activesupport
38
+ airbrake
39
+ bundler
40
+ daemons (= 1.1.6)
41
+ hiredis (~> 0.4.4)
42
+ i18n
43
+ jeweler
44
+ mocha
45
+ rcov
46
+ redis (~> 2.2.2)
47
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Josh Symonds
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,55 @@
1
+ # rapnd
2
+
3
+ rapnd is an easy-to-use daemon that opens a persistent connection to the Apple Push Notification servers and sends notifications you specify to Apple.
4
+
5
+ It intentionally has very little in the way of model-level assumptions: this daemon is intended only to send notifications and does almost nothing to validate the content of the notifications (besides ensuring that the length is correct and a device_token is provided). Your models should validate that the messages they send to rapnd conform to the behavior you expect.
6
+
7
+ Each rapnd daemon operates on a separate resque queue, and multiple daemons can service one queue. Thus you can use rapnd with multiple Apple certs (if you have multiple applications you're trying to send notifications for) and, if you expect a lot of traffic, you can even have multiple daemons servicing one queue.
8
+
9
+ ## Daemon usage
10
+
11
+ ```
12
+ Usage: rapnd start [options]
13
+ --cert=MANDATORY Location of the cert pem file
14
+ --password=OPTIONAL Password for the cert pem file
15
+ --redis_host=OPTIONAL Redis hostname
16
+ --redis_port=OPTIONAL Redis port
17
+ --environment=OPTIONAL Specify sandbox or production
18
+ --queue=OPTIONAL Name of the redis queue
19
+ --foreground Run in the foreground
20
+ --dir=OPTIONAL Directory to start in
21
+ --airbrake=OPTIONAL Airbrake API key
22
+ --help Show help
23
+ ```
24
+
25
+ You can use ```rapnd stop``` instead of start in order to kill a running daemon with the options you provide.
26
+
27
+ ## Client usage
28
+
29
+ ```ruby
30
+ require 'rapnd'
31
+
32
+ Rapnd.configure do |config|
33
+ config.redis_host = 'localhost'
34
+ config.redis_port = 6379
35
+ end
36
+
37
+ message = {:badge => 1, :alert => 'This is a test from rapnd!', :sound => 'flash.caf', :custom_properties => {:test_id => 1234, :happiness => true}}
38
+ queue_name = 'rapnd_queue'
39
+
40
+ Rapnd.queue(queue_name, message)
41
+ ```
42
+
43
+ ## Contributing to rapnd
44
+
45
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
46
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
47
+ * Fork the project
48
+ * Start a feature/bugfix branch
49
+ * Commit and push until you are happy with your contribution
50
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
51
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
52
+
53
+ ## Copyright
54
+
55
+ Copyright (c) 2012 Josh Symonds. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
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
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rapnd"
18
+ gem.homepage = "http://github.com/Veraticus/rapnd"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{redis APN daemon}
21
+ gem.description = %Q{redis APN daemon}
22
+ gem.email = "veraticus@gmail.com"
23
+ gem.authors = ["Josh Symonds"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "rapnd #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.0
data/bin/rapnd ADDED
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ ARGV << '--help' if ARGV.empty?
5
+
6
+ require 'optparse'
7
+ require 'rapnd'
8
+ require 'daemons'
9
+
10
+ command = ARGV.shift
11
+
12
+ options = {}
13
+ OptionParser.new do |opts|
14
+ opts.banner = "Usage: rapnd [options]"
15
+
16
+ opts.on("--cert=MANDATORY", "Location of the cert pem file") do |cert|
17
+ options[:cert] = cert
18
+ end
19
+
20
+ opts.on("--password=OPTIONAL", "Password for the cert pem file") do |password|
21
+ options[:password] = password
22
+ end
23
+
24
+ opts.on("--redis_host=OPTIONAL", "Redis hostname") do |host|
25
+ options[:redis_host] = host
26
+ end
27
+
28
+ opts.on("--redis_port=OPTIONAL", "Redis port") do |port|
29
+ options[:redis_port] = port
30
+ end
31
+
32
+ opts.on("--redis_password=OPTIONAL", "Redis password") do |redis_password|
33
+ options[:redis_password] = redis_password
34
+ end
35
+
36
+ opts.on("--environment=OPTIONAL", "Specify sandbox or production") do |env|
37
+ if env == 'production'
38
+ options[:host] = 'gateway.push.apple.com'
39
+ else
40
+ options[:host] = 'gateway.sandbox.push.apple.com'
41
+ end
42
+ end
43
+
44
+ opts.on("--queue=OPTIONAL", "Name of the redis queue") do |queue|
45
+ options[:queue] = queue
46
+ end
47
+
48
+ opts.on("--foreground", "Run in the foreground") do |fore|
49
+ options[:foreground] = true
50
+ end
51
+
52
+ opts.on("--dir=OPTIONAL", "Directory to start in") do |dir|
53
+ options[:dir] = dir
54
+ end
55
+
56
+ opts.on("--airbrake=OPTIONAL", "Airbrake API key") do |airbrake|
57
+ options[:airbrake] = airbrake
58
+ end
59
+
60
+ opts.on('--help', 'Show help') do |help|
61
+ puts opts
62
+ end
63
+ end.parse!
64
+
65
+ case command
66
+ when 'start'
67
+ unless options[:foreground]
68
+ Daemons.daemonize(
69
+ :app_name => options[:queue],
70
+ :dir_mode => :normal,
71
+ :dir => "#{options[:dir]}/tmp",
72
+ :log_dir => "#{options[:dir]}/log",
73
+ :backtrace => true,
74
+ :log_output => true
75
+ )
76
+ end
77
+
78
+ Rapnd::Daemon.new(options).run!
79
+ when 'stop'
80
+ unless options[:foreground]
81
+ files = Daemons::PidFile.find_files("#{options[:dir]}/tmp", options[:queue])
82
+ files.each do |file|
83
+ pid = File.open(file) {|h| h.read}.to_i
84
+ `kill -9 #{pid}`
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,35 @@
1
+ module Rapnd
2
+ module Config
3
+ extend self
4
+
5
+ def option(name, options = {})
6
+ defaults[name] = settings[name] = options[:default]
7
+
8
+ class_eval <<-RUBY
9
+ def #{name}
10
+ settings[#{name.inspect}]
11
+ end
12
+
13
+ def #{name}=(value)
14
+ settings[#{name.inspect}] = value
15
+ end
16
+
17
+ def #{name}?
18
+ #{name}
19
+ end
20
+ RUBY
21
+ end
22
+
23
+ def defaults
24
+ @defaults ||= {}
25
+ end
26
+
27
+ def settings
28
+ @settings ||= {}
29
+ end
30
+
31
+ option :redis_host, :default => 'localhost'
32
+ option :redis_port, :default => 6879
33
+ option :redis_password
34
+ end
35
+ end
@@ -0,0 +1,82 @@
1
+ require 'redis'
2
+ require 'openssl'
3
+ require 'socket'
4
+ require 'active_support/ordered_hash'
5
+ require 'active_support/json'
6
+ require 'base64'
7
+ require 'airbrake'
8
+ require 'logger'
9
+
10
+ module Rapnd
11
+ class Daemon
12
+ attr_accessor :redis, :host, :apple, :cert, :queue, :connected, :logger, :airbrake
13
+
14
+ def initialize(options = {})
15
+ options[:redis_host] ||= 'localhost'
16
+ options[:redis_port] ||= '6379'
17
+ options[:host] ||= 'gateway.sandbox.push.apple.com'
18
+ options[:queue] ||= 'rapnd_queue'
19
+ options[:password] ||= ''
20
+ raise 'No cert provided!' unless options[:cert]
21
+
22
+ Airbrake.configure { |config| config.api_key = options[:airbrake]; @airbrake = true; } if options[:airbrake]
23
+
24
+ redis_options = { :host => options[:redis_host], :port => options[:redis_port] }
25
+ redis_options[:password] = options[:redis_password] if options.has_key?(:redis_password)
26
+
27
+ @redis = Redis.new(redis_options)
28
+ @queue = options[:queue]
29
+ @cert = options[:cert]
30
+ @host = options[:host]
31
+ @logger = Logger.new("#{options[:dir]}/log/#{options[:queue]}.log")
32
+ @logger.info "Listening on queue: #{self.queue}"
33
+ end
34
+
35
+ def connect!
36
+ @logger.info 'Connecting...'
37
+ @context = OpenSSL::SSL::SSLContext.new
38
+ @context.cert = OpenSSL::X509::Certificate.new(File.read(@cert))
39
+ @context.key = OpenSSL::PKey::RSA.new(File.read(@cert), @password)
40
+
41
+ @sock = TCPSocket.new(@host, 2195)
42
+ self.apple = OpenSSL::SSL::SSLSocket.new(@sock, @context)
43
+ self.apple.sync = true
44
+ self.apple.connect
45
+
46
+ self.connected = true
47
+ @logger.info 'Connected!'
48
+
49
+ return @sock, @ssl
50
+ end
51
+
52
+ def run!
53
+ loop do
54
+ begin
55
+ message = @redis.blpop(self.queue, 1)
56
+ if message
57
+ notification = Rapnd::Notification.new(Marshal.load(message.last))
58
+ self.connect! unless self.connected
59
+ @logger.info "Sending #{notification.device_token}: #{notification.json_payload}"
60
+ self.apple.write(notification.to_bytes)
61
+ end
62
+ rescue Exception => e
63
+ if e.class == Interrupt || e.class == SystemExit
64
+ @logger.info "Shutting down..."
65
+ exit(0)
66
+ end
67
+ self.connect!
68
+ if notification
69
+ @logger.info "Trying again for: #{notification.json_payload}"
70
+ self.apple.write(notification.to_bytes)
71
+ end
72
+ Airbrake.notify(e, {:environment_name => self.queue }) if @airbrake
73
+ @logger.error "Encountered error: #{e}"
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def send_message(message)
80
+
81
+ end
82
+ end
@@ -0,0 +1,40 @@
1
+ module Rapnd
2
+ class Notification
3
+ attr_accessor :badge, :alert, :sound, :content_available, :custom_properties, :device_token
4
+
5
+ def initialize(hash)
6
+ [:badge, :alert, :sound, :device_token, :content_available, :custom_properties].each do |k|
7
+ self.instance_variable_set("@#{k}".to_sym, hash[k]) if hash[k]
8
+ end
9
+ raise "Must provide device token: #{hash}" if self.device_token.nil?
10
+ self.device_token = self.device_token.delete(' ')
11
+ end
12
+
13
+ def payload
14
+ p = Hash.new
15
+ [:badge, :alert, :sound, :content_available].each do |k|
16
+ p[k.to_s.gsub('_','-').to_sym] = send(k) if send(k)
17
+ end
18
+ aps = {:aps => p}
19
+ aps.merge!(custom_properties) if custom_properties
20
+ aps
21
+ end
22
+
23
+ def json_payload
24
+ j = ActiveSupport::JSON.encode(payload)
25
+ while j.bytesize > 256 && payload[:alert].size > 0 do
26
+ payload[:alert] = payload[:alert][0..-2]
27
+ j = ActiveSupport::JSON.encode(payload)
28
+ end
29
+ if j.bytesize > 256
30
+ raise "The payload #{j} is larger than allowed: #{j.length}" if j.size > 256
31
+ end
32
+ j
33
+ end
34
+
35
+ def to_bytes
36
+ j = json_payload
37
+ [0, 0, 32, self.device_token, 0, j.bytesize, j].pack("cccH*cca*").force_encoding('ASCII-8BIT')
38
+ end
39
+ end
40
+ end
data/lib/rapnd.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'rapnd/daemon'
2
+ require 'rapnd/notification'
3
+ require 'rapnd/config'
4
+ require 'redis'
5
+
6
+ module Rapnd
7
+ extend self
8
+
9
+ def queue(queue_name, message)
10
+ self.redis.lpush(queue_name, Marshal.dump(message))
11
+ end
12
+
13
+ def redis
14
+ @redis ||= Redis.new(:host => Rapnd.config.redis_host, :port => Rapnd.config.redis_port, :password => Rapnd.config.redis_password)
15
+ end
16
+
17
+ def configure
18
+ block_given? ? yield(Config) : Config
19
+ end
20
+ alias :config :configure
21
+
22
+ end
data/rapnd.gemspec ADDED
@@ -0,0 +1,90 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "rapnd-mikec54088"
8
+ s.version = "0.5.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Josh Symonds"]
12
+ s.date = "2012-05-22"
13
+ s.description = "redis APN daemon"
14
+ s.email = "veraticus@gmail.com"
15
+ s.executables = ["rapnd"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.markdown"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.markdown",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/rapnd",
30
+ "lib/rapnd.rb",
31
+ "lib/rapnd/config.rb",
32
+ "lib/rapnd/daemon.rb",
33
+ "lib/rapnd/notification.rb",
34
+ "rapnd.gemspec",
35
+ "spec/config_spec.rb",
36
+ "spec/daemon_spec.rb",
37
+ "spec/notification_spec.rb",
38
+ "spec/rapnd_spec.rb",
39
+ "spec/redis-test.conf",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+ s.homepage = "http://github.com/Veraticus/rapnd"
43
+ s.licenses = ["MIT"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = "1.8.23"
46
+ s.summary = "redis APN daemon"
47
+
48
+ if s.respond_to? :specification_version then
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_runtime_dependency(%q<hiredis>, ["~> 0.4.4"])
53
+ s.add_runtime_dependency(%q<redis>, ["~> 2.2.2"])
54
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
55
+ s.add_runtime_dependency(%q<daemons>, ["= 1.1.6"])
56
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
57
+ s.add_runtime_dependency(%q<airbrake>, [">= 0"])
58
+ s.add_development_dependency(%q<mocha>, [">= 0"])
59
+ s.add_development_dependency(%q<rspec>, [">= 0"])
60
+ s.add_development_dependency(%q<bundler>, [">= 0"])
61
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
62
+ s.add_development_dependency(%q<rcov>, [">= 0"])
63
+ else
64
+ s.add_dependency(%q<hiredis>, ["~> 0.4.4"])
65
+ s.add_dependency(%q<redis>, ["~> 2.2.2"])
66
+ s.add_dependency(%q<activesupport>, [">= 0"])
67
+ s.add_dependency(%q<daemons>, ["= 1.1.6"])
68
+ s.add_dependency(%q<i18n>, [">= 0"])
69
+ s.add_dependency(%q<airbrake>, [">= 0"])
70
+ s.add_dependency(%q<mocha>, [">= 0"])
71
+ s.add_dependency(%q<rspec>, [">= 0"])
72
+ s.add_dependency(%q<bundler>, [">= 0"])
73
+ s.add_dependency(%q<jeweler>, [">= 0"])
74
+ s.add_dependency(%q<rcov>, [">= 0"])
75
+ end
76
+ else
77
+ s.add_dependency(%q<hiredis>, ["~> 0.4.4"])
78
+ s.add_dependency(%q<redis>, ["~> 2.2.2"])
79
+ s.add_dependency(%q<activesupport>, [">= 0"])
80
+ s.add_dependency(%q<daemons>, ["= 1.1.6"])
81
+ s.add_dependency(%q<i18n>, [">= 0"])
82
+ s.add_dependency(%q<airbrake>, [">= 0"])
83
+ s.add_dependency(%q<mocha>, [">= 0"])
84
+ s.add_dependency(%q<rspec>, [">= 0"])
85
+ s.add_dependency(%q<bundler>, [">= 0"])
86
+ s.add_dependency(%q<jeweler>, [">= 0"])
87
+ s.add_dependency(%q<rcov>, [">= 0"])
88
+ end
89
+ end
90
+
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Rapnd::Config" do
4
+ it 'has a default value' do
5
+ Rapnd.config.redis_host.should == 'localhost'
6
+ end
7
+
8
+ it 'overrides a default value with an assigner' do
9
+ Rapnd.config.redis_host = 'testhost'
10
+
11
+ Rapnd.config.redis_host.should == 'testhost'
12
+ end
13
+
14
+ it 'overrides a default value with a block' do
15
+ Rapnd.config do |config|
16
+ config.redis_host = 'testhost'
17
+ end
18
+
19
+ Rapnd.config.redis_host.should == 'testhost'
20
+ end
21
+
22
+ it 'checks a value has been assigned' do
23
+ Rapnd.config.redis_host?.should be_true
24
+
25
+ Rapnd.config.redis_host = nil
26
+
27
+ Rapnd.config.redis_host?.should be_false
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Rapnd::Daemon" do
4
+ before do
5
+ @daemon = Rapnd::Daemon.new(:redis_port => 9876, :cert => '', :dir => '.')
6
+ end
7
+
8
+ it "initializes a redis connection" do
9
+ @daemon.redis.ping.should == "PONG"
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Rapnd::Notification" do
4
+ before do
5
+ @notification = Rapnd::Notification.new(:badge => 99, :alert => 'Big test time', :custom_properties => {:key => 'This is a test!', :spid => 1234}, :device_token => '1234 5')
6
+ end
7
+
8
+ it 'removes whitespace from the device token' do
9
+ @notification.device_token.should == '12345'
10
+ end
11
+
12
+ it 'sets a content-available' do
13
+ @notification = Rapnd::Notification.new(:alert => 'Big test time', :content_available => "Yes!", :custom_properties => {:key => 'This is a test!', :spid => 1234}, :device_token => '1234 5')
14
+
15
+ @notification.payload.should == {:aps => {:alert => "Big test time", :'content-available' => "Yes!"}, :key => "This is a test!", :spid => 1234}
16
+ end
17
+
18
+ it 'does not set the body key if only an alert is passed' do
19
+ @notification = Rapnd::Notification.new(:alert => 'Big test time', :custom_properties => {:key => 'This is a test!', :spid => 1234}, :device_token => '1234 5')
20
+
21
+ @notification.payload.should == {:aps => {:alert => "Big test time"}, :key => "This is a test!", :spid => 1234}
22
+ end
23
+
24
+ it "automatically assigns hash variables to instance variables" do
25
+ @notification.badge.should == 99
26
+ @notification.custom_properties.should == {:key => 'This is a test!', :spid => 1234}
27
+ end
28
+
29
+ it 'creates a hash payload' do
30
+ @notification.payload.should == {:aps=>{:badge=>99, :alert=>"Big test time"}, :key => "This is a test!", :spid => 1234}
31
+ end
32
+
33
+ it 'jsonifies the hash payload' do
34
+ ActiveSupport::JSON.decode(@notification.json_payload).should == {"aps"=>{"badge"=>99, "alert"=>"Big test time"}, "key"=>"This is a test!", "spid"=>1234}
35
+ end
36
+
37
+ it 'turns into bytes sensibly' do
38
+ @notification.to_bytes.should == "\x00\x00 \x124P\x00P{\"aps\":{\"badge\":99,\"alert\":\"Big test time\"},\"key\":\"This is a test!\",\"spid\":1234}"
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Rapnd" do
4
+ before do
5
+ Rapnd.config do |config|
6
+ config.redis_host = 'localhost'
7
+ config.redis_port = 9876
8
+ end
9
+
10
+ @redis = Redis.new(:host => 'localhost', :port => 9876)
11
+ end
12
+
13
+ it 'enqueues a message' do
14
+ Rapnd.queue('test_queue', {:alert => 'Hi!'})
15
+
16
+ @redis.llen('test_queue').should == 1
17
+ Marshal.load(@redis.lpop('test_queue')).should == {:alert => 'Hi!'}
18
+ end
19
+
20
+ it 'gets a redis connection' do
21
+ Rapnd.redis.ping.should == "PONG"
22
+ end
23
+ end
@@ -0,0 +1,115 @@
1
+ # Redis configuration file example
2
+
3
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
4
+ # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
5
+ daemonize yes
6
+
7
+ # When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
8
+ # You can specify a custom pid file location here.
9
+ pidfile ./redis-test.pid
10
+
11
+ # Accept connections on the specified port, default is 6379
12
+ port 9876
13
+
14
+ # If you want you can bind a single interface, if the bind option is not
15
+ # specified all the interfaces will listen for connections.
16
+ #
17
+ # bind 127.0.0.1
18
+
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
+ timeout 300
21
+
22
+ # Save the DB on disk:
23
+ #
24
+ # save <seconds> <changes>
25
+ #
26
+ # Will save the DB if both the given number of seconds and the given
27
+ # number of write operations against the DB occurred.
28
+ #
29
+ # In the example below the behaviour will be to save:
30
+ # after 900 sec (15 min) if at least 1 key changed
31
+ # after 300 sec (5 min) if at least 10 keys changed
32
+ # after 60 sec if at least 10000 keys changed
33
+ save 900 1
34
+ save 300 10
35
+ save 60 10000
36
+
37
+ # The filename where to dump the DB
38
+ dbfilename dump.rdb
39
+
40
+ # For default save/load DB in/from the working directory
41
+ # Note that you must specify a directory not a file name.
42
+ dir .
43
+
44
+ # Set server verbosity to 'debug'
45
+ # it can be one of:
46
+ # debug (a lot of information, useful for development/testing)
47
+ # notice (moderately verbose, what you want in production probably)
48
+ # warning (only very important / critical messages are logged)
49
+ loglevel debug
50
+
51
+ # Specify the log file name. Also 'stdout' can be used to force
52
+ # the demon to log on the standard output. Note that if you use standard
53
+ # output for logging but daemonize, logs will be sent to /dev/null
54
+ logfile stdout
55
+
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
59
+ databases 16
60
+
61
+ ################################# REPLICATION #################################
62
+
63
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
64
+ # another Redis server. Note that the configuration is local to the slave
65
+ # so for example it is possible to configure the slave to save the DB with a
66
+ # different interval, or to listen to another port, and so on.
67
+
68
+ # slaveof <masterip> <masterport>
69
+
70
+ ################################## SECURITY ###################################
71
+
72
+ # Require clients to issue AUTH <PASSWORD> before processing any other
73
+ # commands. This might be useful in environments in which you do not trust
74
+ # others with access to the host running redis-server.
75
+ #
76
+ # This should stay commented out for backward compatibility and because most
77
+ # people do not need auth (e.g. they run their own servers).
78
+
79
+ # requirepass foobared
80
+
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
110
+ ############################### ADVANCED CONFIG ###############################
111
+
112
+ # Glue small output buffers together in order to send small replies in a
113
+ # single TCP packet. Uses a bit more CPU but most of the times it is a win
114
+ # in terms of number of queries per second. Use 'yes' if unsure.
115
+ glueoutputbuf yes
@@ -0,0 +1,20 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'rapnd'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+ config.mock_with(:mocha)
12
+ config.before(:suite) do
13
+ `redis-server #{File.dirname(__FILE__)}/redis-test.conf`
14
+ end
15
+ config.after(:suite) do
16
+ processes = `ps -A -o pid,command | grep [r]edis-test`.split("\n")
17
+ pids = processes.map { |process| process.split(" ")[0] }
18
+ pids.each { |pid| Process.kill("KILL", pid.to_i) }
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rapnd-mikec54088
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Symonds
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hiredis
16
+ requirement: &70235359023020 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.4.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70235359023020
25
+ - !ruby/object:Gem::Dependency
26
+ name: redis
27
+ requirement: &70235359022540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.2.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70235359022540
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &70235359022060 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70235359022060
47
+ - !ruby/object:Gem::Dependency
48
+ name: daemons
49
+ requirement: &70235359021320 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - =
53
+ - !ruby/object:Gem::Version
54
+ version: 1.1.6
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70235359021320
58
+ - !ruby/object:Gem::Dependency
59
+ name: i18n
60
+ requirement: &70235359020620 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70235359020620
69
+ - !ruby/object:Gem::Dependency
70
+ name: airbrake
71
+ requirement: &70235359020060 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70235359020060
80
+ - !ruby/object:Gem::Dependency
81
+ name: mocha
82
+ requirement: &70235359019500 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70235359019500
91
+ - !ruby/object:Gem::Dependency
92
+ name: rspec
93
+ requirement: &70235359019020 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70235359019020
102
+ - !ruby/object:Gem::Dependency
103
+ name: bundler
104
+ requirement: &70235359018500 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *70235359018500
113
+ - !ruby/object:Gem::Dependency
114
+ name: jeweler
115
+ requirement: &70235359018000 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *70235359018000
124
+ - !ruby/object:Gem::Dependency
125
+ name: rcov
126
+ requirement: &70235359017520 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: *70235359017520
135
+ description: redis APN daemon
136
+ email: veraticus@gmail.com
137
+ executables:
138
+ - rapnd
139
+ extensions: []
140
+ extra_rdoc_files:
141
+ - LICENSE.txt
142
+ - README.markdown
143
+ files:
144
+ - .document
145
+ - .rspec
146
+ - Gemfile
147
+ - Gemfile.lock
148
+ - LICENSE.txt
149
+ - README.markdown
150
+ - Rakefile
151
+ - VERSION
152
+ - bin/rapnd
153
+ - lib/rapnd.rb
154
+ - lib/rapnd/config.rb
155
+ - lib/rapnd/daemon.rb
156
+ - lib/rapnd/notification.rb
157
+ - rapnd.gemspec
158
+ - spec/config_spec.rb
159
+ - spec/daemon_spec.rb
160
+ - spec/notification_spec.rb
161
+ - spec/rapnd_spec.rb
162
+ - spec/redis-test.conf
163
+ - spec/spec_helper.rb
164
+ homepage: http://github.com/Veraticus/rapnd
165
+ licenses:
166
+ - MIT
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ none: false
173
+ requirements:
174
+ - - ! '>='
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ none: false
179
+ requirements:
180
+ - - ! '>='
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 1.8.15
186
+ signing_key:
187
+ specification_version: 3
188
+ summary: redis APN daemon
189
+ test_files: []