kw_apn 0.3.1 → 0.4.beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/config.rb +60 -32
- data/lib/connection.rb +2 -2
- data/lib/core.rb +1 -1
- data/lib/feedback_reader.rb +3 -3
- data/lib/generators/kw_apn/templates/config/kw_apn.yml +1 -2
- data/lib/sender.rb +13 -9
- metadata +12 -9
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.beta.1
|
data/lib/config.rb
CHANGED
@@ -1,39 +1,67 @@
|
|
1
1
|
module KwAPN
|
2
2
|
require 'erb'
|
3
|
-
class Config
|
3
|
+
class Config
|
4
4
|
class << self
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
p_env = if defined? Rails
|
18
|
-
Rails.env
|
19
|
-
elsif defined? RACK_ENV
|
20
|
-
RACK_ENV
|
21
|
-
else
|
22
|
-
puts "Warning (KwAPN): You need to specifiy either Rails.env or RACK_ENV for apns to work!"
|
23
|
-
nil
|
24
|
-
end
|
25
|
-
|
26
|
-
@@options = begin
|
27
|
-
raw_config = File.read(p_root.join("config", "kw_apn.yml"))
|
28
|
-
parsed_config = ERB.new(raw_config).result
|
29
|
-
YAML.load(parsed_config)[p_env].symbolize_keys
|
30
|
-
rescue => e
|
31
|
-
puts "Warning (KwAPN): Could not parse config file: #{e.message}"
|
32
|
-
{}
|
33
|
-
end
|
34
|
-
@@options[:root] = p_root
|
5
|
+
attr_accessor :default_push_host
|
6
|
+
attr_accessor :default_feedback_host
|
7
|
+
|
8
|
+
# calculates the project environment (uses Rails or RACK_ENV if available)
|
9
|
+
def env
|
10
|
+
@p_env ||= if defined? Rails
|
11
|
+
Rails.env
|
12
|
+
elsif defined? RACK_ENV
|
13
|
+
RACK_ENV
|
14
|
+
else
|
15
|
+
puts "Warning (KwAPN): You need to specifiy either Rails.env or RACK_ENV for apns to work!"
|
16
|
+
'development'
|
35
17
|
end
|
36
|
-
|
18
|
+
end
|
19
|
+
|
20
|
+
# calculates the project root (uses Rails or RACK_ROOT if available)
|
21
|
+
def root
|
22
|
+
p_root ||= if defined? Rails
|
23
|
+
Rails.root
|
24
|
+
elsif defined? RACK_ROOT
|
25
|
+
RACK_ROOT
|
26
|
+
else
|
27
|
+
puts "Warning (KwAPN): You need to specifiy either Rails.root or RACK_ROOT for apns to work!"
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# loads the options from '/config/kw_apn.yml'
|
33
|
+
def load_options
|
34
|
+
@options = begin
|
35
|
+
raw_config = File.read(root.join("config", "kw_apn.yml"))
|
36
|
+
parsed_config = ERB.new(raw_config).result
|
37
|
+
YAML.load(parsed_config)[env].symbolize_keys
|
38
|
+
rescue => e
|
39
|
+
puts "Warning (KwAPN): Could not parse config file: #{e.message}"
|
40
|
+
{}
|
41
|
+
end
|
42
|
+
@options[:root] ||= root
|
43
|
+
@options[:push_host] ||= default_push_host
|
44
|
+
@options[:feedback_host] ||= default_feedback_host
|
45
|
+
true
|
46
|
+
end
|
47
|
+
|
48
|
+
# returns or loads the current options
|
49
|
+
def option(opt, app_id = nil)
|
50
|
+
@options || load_options
|
51
|
+
if app_id && @options[app_id] && @options[app_id][opt]
|
52
|
+
@options[app_id][opt]
|
53
|
+
else
|
54
|
+
@options[opt]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# set some default options based on the envrionment
|
59
|
+
if self.env == 'production'
|
60
|
+
self.default_push_host = 'gateway.push.apple.com'
|
61
|
+
self.default_feedback_host = 'feedback.push.apple.com'
|
62
|
+
else
|
63
|
+
self.default_push_host = 'gateway.sandbox.push.apple.com'
|
64
|
+
self.default_feedback_host = 'feedback.sandbox.push.apple.com'
|
37
65
|
end
|
38
66
|
end
|
39
67
|
end
|
data/lib/connection.rb
CHANGED
@@ -14,12 +14,12 @@ module KwAPN
|
|
14
14
|
ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
|
15
15
|
ssl.connect # start SSL session
|
16
16
|
ssl.sync_close = true # close underlying socket on SSLSocket#close
|
17
|
-
ssl
|
17
|
+
ssl
|
18
18
|
end
|
19
19
|
|
20
20
|
class << self
|
21
21
|
def log(s)
|
22
|
-
File.open(KwAPN::Config.
|
22
|
+
File.open(KwAPN::Config.option(:root).join("log", "kw_apn.log"), File::WRONLY|File::APPEND|File::CREAT, 0666) do |f|
|
23
23
|
f.write("#{s}\n")
|
24
24
|
end
|
25
25
|
end
|
data/lib/core.rb
CHANGED
data/lib/feedback_reader.rb
CHANGED
@@ -2,9 +2,9 @@ module KwAPN
|
|
2
2
|
class FeedbackReader < Connection
|
3
3
|
|
4
4
|
attr_accessor :host, :port
|
5
|
-
def initialize(
|
6
|
-
@host =
|
7
|
-
@port =
|
5
|
+
def initialize(app_id = nil)
|
6
|
+
@host = KwAPN::Config.option(:feedback_host, app_id)
|
7
|
+
@port = KwAPN::Config.option(:feedback_port, app_id)
|
8
8
|
end
|
9
9
|
|
10
10
|
def read
|
@@ -7,7 +7,6 @@ defaults: &defaults
|
|
7
7
|
|
8
8
|
development:
|
9
9
|
<<: *defaults
|
10
|
-
profile: true
|
11
10
|
|
12
11
|
test:
|
13
12
|
<<: *defaults
|
@@ -15,4 +14,4 @@ test:
|
|
15
14
|
production:
|
16
15
|
<<: *defaults
|
17
16
|
push_host: 'gateway.push.apple.com'
|
18
|
-
feedback_host: 'feedback.push.apple.com'
|
17
|
+
feedback_host: 'feedback.push.apple.com'
|
data/lib/sender.rb
CHANGED
@@ -4,13 +4,16 @@ module KwAPN
|
|
4
4
|
# if there is a bug on our side feel free to fix it, but for now it seems to work with the offset workaround
|
5
5
|
ID_OFFSET = 333
|
6
6
|
class Sender < Connection
|
7
|
-
attr_accessor :host, :port, :count, :fail_count, :work_thread, :watch_thread, :failed_index_array, :session_id, :last_error_index
|
7
|
+
attr_accessor :host, :port, :count, :fail_count, :work_thread, :watch_thread, :failed_index_array, :session_id, :last_error_index, :app_id
|
8
8
|
|
9
9
|
# Creates new {Sender} object with given host and port
|
10
|
-
|
10
|
+
# @param [String] session_id - A Identifier for Login purpose
|
11
|
+
# @param [String] app_id - The App ID to send to (in the default configuration, there only is 1 id)
|
12
|
+
def initialize(session_id, app_id = nil)
|
11
13
|
@session_id = session_id
|
12
|
-
@
|
13
|
-
@
|
14
|
+
@app_id = app_id
|
15
|
+
@host = KwAPN::Config.option(:push_host, app_id) || 'gateway.sandbox.push.apple.com'
|
16
|
+
@port = KwAPN::Config.option(:push_host, app_id) || 2195
|
14
17
|
@count = 0
|
15
18
|
@fail_count = 0
|
16
19
|
@failed_index_array = []
|
@@ -27,7 +30,7 @@ module KwAPN
|
|
27
30
|
return [:ok, @failed_index_array.collect{|a| notifications[a].token}]
|
28
31
|
rescue => e
|
29
32
|
failed
|
30
|
-
self.class.log("(#{session_id}) Exception: #{e.message}")
|
33
|
+
self.class.log("(#{app_id} - #{session_id}) Exception: #{e.message}")
|
31
34
|
return [:nok, "Exception: #{e.message}"]
|
32
35
|
end
|
33
36
|
end
|
@@ -80,7 +83,7 @@ private
|
|
80
83
|
if @last_error_index.nil?
|
81
84
|
# stop watchthread as the connection should be allready down
|
82
85
|
@watch_thread.exit
|
83
|
-
self.class.log("(#{session_id}) Exception at index #{counter+index}: #{e.message}")
|
86
|
+
self.class.log("(#{app_id} - #{session_id}) Exception at index #{counter+index}: #{e.message}")
|
84
87
|
@failed_index_array << (counter+index)
|
85
88
|
failed
|
86
89
|
else
|
@@ -134,13 +137,14 @@ private
|
|
134
137
|
# convenient way of connecting with apple and pushing the notifications
|
135
138
|
# @param [Array] notifications - An Array of Objects of Type KwAPN::Notification
|
136
139
|
# @param [String] session_id - A Identifier for Login purpose
|
140
|
+
# @param [String] app_id - A Identifier for Login purpose
|
137
141
|
#
|
138
142
|
# @returns [Symbol, Array/String] if no Problems occured :ok and an Array of Tokens failed to push is returned. The Caller should take care of those invalid Tokens.
|
139
|
-
def push(notifications, session_id=nil)
|
140
|
-
s = self.new(session_id)
|
143
|
+
def push(notifications, session_id = nil, app_id = nil)
|
144
|
+
s = self.new(session_id, app_id)
|
141
145
|
startdate = Time.now
|
142
146
|
status, ret = s.push_batch(notifications)
|
143
|
-
log("(#{session_id}) #{startdate.to_s} SENT APN #{s.count - s.fail_count}/#{s.count} in #{Time.now.to_i - startdate.to_i} seconds")
|
147
|
+
log("(#{app_id} - #{session_id}) #{startdate.to_s} SENT APN #{s.count - s.fail_count}/#{s.count} in #{Time.now.to_i - startdate.to_i} seconds")
|
144
148
|
s.close_connection
|
145
149
|
return [status, ret]
|
146
150
|
end
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kw_apn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 62196337
|
5
|
+
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 4
|
9
|
+
- beta
|
9
10
|
- 1
|
10
|
-
version: 0.
|
11
|
+
version: 0.4.beta.1
|
11
12
|
platform: ruby
|
12
13
|
authors:
|
13
14
|
- Jonathan Cichon
|
@@ -16,7 +17,7 @@ autorequire:
|
|
16
17
|
bindir: bin
|
17
18
|
cert_chain: []
|
18
19
|
|
19
|
-
date: 2010-08-
|
20
|
+
date: 2010-08-26 00:00:00 +02:00
|
20
21
|
default_executable:
|
21
22
|
dependencies: []
|
22
23
|
|
@@ -65,12 +66,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
66
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
67
|
none: false
|
67
68
|
requirements:
|
68
|
-
- - "
|
69
|
+
- - ">"
|
69
70
|
- !ruby/object:Gem::Version
|
70
|
-
hash:
|
71
|
+
hash: 25
|
71
72
|
segments:
|
72
|
-
-
|
73
|
-
|
73
|
+
- 1
|
74
|
+
- 3
|
75
|
+
- 1
|
76
|
+
version: 1.3.1
|
74
77
|
requirements: []
|
75
78
|
|
76
79
|
rubyforge_project:
|