wireframe-apn_on_rails 0.3.0.201302051600 → 0.3.0.20091216113551

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,9 +38,13 @@ module APN # :nodoc:
38
38
 
39
39
  # Raised when a notification message to Apple is longer than 256 bytes.
40
40
  class ExceededMessageSizeError < StandardError
41
- MAX_BYTES = 256
41
+
42
42
  def initialize(message) # :nodoc:
43
- super("The maximum size allowed for a notification payload is #{MAX_BYTES} bytes: '#{message}'")
43
+ super("The maximum size allowed for a notification payload is 256 bytes: '#{message}'")
44
+ @overage = message.size.to_i - 256
45
+ end
46
+ def overage
47
+ @overage
44
48
  end
45
49
  end
46
50
 
@@ -55,11 +59,7 @@ end
55
59
  %w{ models controllers helpers }.each do |dir|
56
60
  path = File.join(File.dirname(__FILE__), 'app', dir)
57
61
  $LOAD_PATH << path
58
- if ActiveSupport::Dependencies.respond_to?(:autoload_paths)
59
- ActiveSupport::Dependencies.autoload_paths << path
60
- ActiveSupport::Dependencies.autoload_once_paths.delete(path)
61
- else
62
- ActiveSupport::Dependencies.load_paths << path
63
- ActiveSupport::Dependencies.load_once_paths.delete(path)
64
- end
65
- end
62
+ # puts "Adding #{path}"
63
+ ActiveSupport::Dependencies.load_paths << path
64
+ ActiveSupport::Dependencies.load_once_paths.delete(path)
65
+ end
@@ -13,7 +13,7 @@ class APN::Device < APN::Base
13
13
  has_many :notifications, :class_name => 'APN::Notification'
14
14
 
15
15
  validates_uniqueness_of :token
16
- validates_format_of :token, :with => /\A[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\z/
16
+ validates_format_of :token, :with => /^[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}$/
17
17
 
18
18
  before_save :set_last_registered_at
19
19
 
@@ -45,4 +45,4 @@ class APN::Device < APN::Base
45
45
  self.last_registered_at = Time.now if self.last_registered_at.nil?
46
46
  end
47
47
 
48
- end
48
+ end
@@ -16,6 +16,7 @@
16
16
  # so as to not be sent again.
17
17
  class APN::Notification < APN::Base
18
18
  include ::ActionView::Helpers::TextHelper
19
+ extend ::ActionView::Helpers::TextHelper
19
20
 
20
21
  serialize :payload
21
22
 
@@ -56,42 +57,38 @@ class APN::Notification < APN::Base
56
57
  end
57
58
 
58
59
  # Creates the binary message needed to send to Apple.
59
- # see http://developer.apple.com/IPhone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4
60
60
  def message_for_sending
61
61
  json = self.to_apple_json
62
- message = "\0\0 #{self.device.to_hexa}\0#{encode((json.bytesize).chr)}#{encode(json)}"
63
- raise APN::Errors::ExceededMessageSizeError.new(message) if message.bytesize.to_i > APN::Errors::ExceededMessageSizeError::MAX_BYTES
62
+ message = "\0\0 #{self.device.to_hexa}\0#{json.length.chr}#{json}"
63
+ raise APN::Errors::ExceededMessageSizeError.new(message) if message.size.to_i > 256
64
64
  message
65
65
  end
66
- def encode(string)
67
- string.respond_to?(:force_encoding) ? string.force_encoding('BINARY') : string
68
- end
69
66
 
70
67
  class << self
71
68
 
72
69
  # Opens a connection to the Apple APN server and attempts to batch deliver
73
70
  # an Array of notifications.
71
+ #
72
+ # This method expects an Array of APN::Notifications. If no parameter is passed
73
+ # in then it will use the following:
74
+ # APN::Notification.all(:conditions => {:sent_at => nil})
75
+ #
76
+ # As each APN::Notification is sent the <tt>sent_at</tt> column will be timestamped,
77
+ # so as to not be sent again.
78
+ #
74
79
  # This can be run from the following Rake task:
75
80
  # $ rake apn:notifications:deliver
76
- def send_notifications(notifications)
77
- sent_ids = []
78
- sent = false
79
- message = ''
80
-
81
- notifications.find_each do |noty|
82
- sent_ids << noty.id
83
- message << noty.message_for_sending
84
- end
81
+ def send_notifications(notifications = APN::Notification.all(:conditions => {:sent_at => nil}))
82
+ unless notifications.nil? || notifications.empty?
85
83
 
86
- return if sent_ids.empty?
87
-
88
- begin
89
84
  APN::Connection.open_for_delivery do |conn, sock|
90
- sent = true
91
- conn.write(message)
85
+ notifications.each do |noty|
86
+ conn.write(noty.message_for_sending)
87
+ noty.sent_at = Time.now
88
+ noty.save
89
+ end
92
90
  end
93
- ensure
94
- APN::Notification.update_all(['sent_at = ?', Time.now.utc], ['id in (?)', sent_ids]) if sent && sent_ids.any?
91
+
95
92
  end
96
93
  end
97
94
 
@@ -101,13 +98,10 @@ class APN::Notification < APN::Base
101
98
  # Truncate alert message if message payload will be too long
102
99
  def truncate_alert
103
100
  return unless self.alert
104
- while self.alert.length > 1
105
- begin
106
- self.message_for_sending
107
- break
108
- rescue APN::Errors::ExceededMessageSizeError => e
109
- self.alert = truncate(self.alert, :escape => false, :length => self.alert.mb_chars.length - 1)
110
- end
101
+ begin
102
+ self.message_for_sending
103
+ rescue APN::Errors::ExceededMessageSizeError => e
104
+ self.alert = truncate(self.alert, :length => self.alert.size - e.overage)
111
105
  end
112
106
  end
113
- end # APN::Notification
107
+ end # APN::Notification
metadata CHANGED
@@ -1,56 +1,38 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: wireframe-apn_on_rails
3
- version: !ruby/object:Gem::Version
4
- version: 0.3.0.201302051600
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0.20091216113551
6
5
  platform: ruby
7
- authors:
8
- - ryan
6
+ authors:
7
+ - markbates
8
+ - ryansonnek
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-02-14 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
12
+
13
+ date: 2009-12-16 00:00:00 -06:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
15
17
  name: configatron
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
18
  type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: activerecord
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
- type: :runtime
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
- description: ! 'apn_on_rails was developed by: ryan'
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ description: "apn_on_rails was developed by: markbates"
47
27
  email: mark@markbates.com
48
28
  executables: []
29
+
49
30
  extensions: []
50
- extra_rdoc_files:
31
+
32
+ extra_rdoc_files:
51
33
  - README
52
34
  - LICENSE
53
- files:
35
+ files:
54
36
  - lib/apn_on_rails/apn_on_rails.rb
55
37
  - lib/apn_on_rails/app/models/apn/base.rb
56
38
  - lib/apn_on_rails/app/models/apn/device.rb
@@ -68,28 +50,33 @@ files:
68
50
  - generators/templates/apn_migrations/002_create_apn_notifications.rb
69
51
  - generators/templates/apn_migrations/003_alter_apn_devices.rb
70
52
  - generators/templates/apn_migrations/004_add_payload_to_notifications.rb
53
+ has_rdoc: true
71
54
  homepage: http://www.metabates.com
72
55
  licenses: []
56
+
73
57
  post_install_message:
74
58
  rdoc_options: []
75
- require_paths:
59
+
60
+ require_paths:
76
61
  - lib
77
- required_ruby_version: !ruby/object:Gem::Requirement
78
- none: false
79
- requirements:
80
- - - ! '>='
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- required_rubygems_version: !ruby/object:Gem::Requirement
84
- none: false
85
- requirements:
86
- - - ! '>='
87
- - !ruby/object:Gem::Version
88
- version: '0'
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
89
74
  requirements: []
75
+
90
76
  rubyforge_project: magrathea
91
- rubygems_version: 1.8.24
77
+ rubygems_version: 1.3.5
92
78
  signing_key:
93
79
  specification_version: 3
94
80
  summary: apn_on_rails
95
81
  test_files: []
82
+