forked from PRX/apn_on_rails
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nearly done with feedback. Just adding documentation.
- Loading branch information
Showing
17 changed files
with
206 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
generators/templates/apn_migrations/003_add_registered_at_to_apn_devices.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class AddRegisteredAtToApnDevices < ActiveRecord::Migration # :nodoc: | ||
|
||
module APN | ||
class Device < ActiveRecord::Base | ||
set_table_name 'apn_devices' | ||
end | ||
end | ||
|
||
def self.up | ||
add_column :apn_devices, :last_registered_at, :datetime | ||
|
||
APN::Device.all.each do |device| | ||
device.last_registered_at = device.created_at | ||
device.save! | ||
end | ||
|
||
end | ||
|
||
def self.down | ||
remove_column :apn_devices, :last_registered_at | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module APN | ||
class Base < ActiveRecord::Base # :nodoc: | ||
|
||
def self.table_name # :nodoc: | ||
self.to_s.gsub("::", "_").tableize | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module APN | ||
# Module for talking to the Apple Feedback Service. | ||
# The service is meant to let you know when a device is no longer | ||
# registered to receive notifications for your application. | ||
module Feedback | ||
|
||
class << self | ||
|
||
# Returns an Array of APN::Device objects that | ||
# has received feedback from Apple. Each APN::Device will | ||
# have it's <tt>feedback_at</tt> accessor marked with the time | ||
# that Apple believes the device de-registered itself. | ||
def devices(&block) | ||
devices = [] | ||
APN::Connection.open_for_feedback do |conn, sock| | ||
while line = sock.gets # Read lines from the socket | ||
line.strip! | ||
feedback = line.unpack('N1n1H140') | ||
token = feedback[2].scan(/.{0,8}/).join(' ').strip | ||
device = APN::Device.find(:first, :conditions => {:token => token}) | ||
if device | ||
device.feedback_at = Time.at(feedback[0]) | ||
devices << device | ||
end | ||
end | ||
end | ||
devices.each(&block) if block_given? | ||
return devices | ||
end # devices | ||
|
||
def process_devices | ||
APN::Feedback.devices.each do |device| | ||
if device.last_registered_at < device.feedback_at | ||
device.destroy | ||
end | ||
end | ||
end # process_devices | ||
|
||
end # class << self | ||
|
||
end # Feedback | ||
end # APN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require File.dirname(__FILE__) + '/../../spec_helper' | ||
|
||
describe APN::Feedback do | ||
|
||
describe 'devices' do | ||
|
||
before(:each) do | ||
@time = Time.now | ||
@device = DeviceFactory.create | ||
|
||
@data_mock = mock('data_mock') | ||
@data_mock.should_receive(:strip!) | ||
@data_mock.should_receive(:unpack).with('N1n1H140').and_return([@time.to_i, 12388, @device.token.delete(' ')]) | ||
|
||
@ssl_mock = mock('ssl_mock') | ||
@sock_mock = mock('sock_mock') | ||
@sock_mock.should_receive(:gets).twice.and_return(@data_mock, nil) | ||
|
||
end | ||
|
||
it 'should an Array of devices that need to be processed' do | ||
APN::Connection.should_receive(:open_for_feedback).and_yield(@ssl_mock, @sock_mock) | ||
|
||
devices = APN::Feedback.devices | ||
devices.size.should == 1 | ||
r_device = devices.first | ||
r_device.token.should == @device.token | ||
r_device.feedback_at.to_s.should == @time.to_s | ||
end | ||
|
||
it 'should yield up each device' do | ||
APN::Connection.should_receive(:open_for_feedback).and_yield(@ssl_mock, @sock_mock) | ||
lambda { | ||
APN::Feedback.devices do |r_device| | ||
r_device.token.should == @device.token | ||
r_device.feedback_at.to_s.should == @time.to_s | ||
raise BlockRan.new | ||
end | ||
}.should raise_error(BlockRan) | ||
end | ||
|
||
end | ||
|
||
describe 'process_devices' do | ||
|
||
it 'should destroy devices that have a last_registered_at date that is before the feedback_at date' do | ||
devices = [DeviceFactory.create(:last_registered_at => 1.week.ago, :feedback_at => Time.now), | ||
DeviceFactory.create(:last_registered_at => 1.week.from_now, :feedback_at => Time.now)] | ||
APN::Feedback.should_receive(:devices).and_return(devices) | ||
lambda { | ||
APN::Feedback.process_devices | ||
}.should change(APN::Device, :count).by(-1) | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters