c2dm_batch 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +13 -12
- data/c2dm_batch.gemspec +1 -1
- data/lib/c2dm_batch.rb +0 -3
- data/lib/c2dm_batch/core.rb +14 -18
- data/spec/c2dm_batch/core_spec.rb +11 -10
- metadata +4 -4
data/README.md
CHANGED
@@ -14,33 +14,34 @@ gem install typhoeus
|
|
14
14
|
|
15
15
|
Configuration
|
16
16
|
-------------
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
c2dm_batch = C2dmBatch.new
|
18
|
+
@c2dm_batch.email = 'your_c2dm_sender@gmail.com'
|
19
|
+
@c2dm_batch.password = 'password'
|
20
|
+
@c2dm_batch.source = 'your app name'
|
21
|
+
@c2dm_batch.logger = Logger.new(STDOUT) # default logger
|
21
22
|
|
22
23
|
Send a single notification
|
23
24
|
--------------------------
|
24
25
|
<pre>
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
@c2dm_batch.email = 'your_c2dm_sender@gmail.com'
|
27
|
+
@c2dm_batch.password = 'password'
|
28
|
+
@c2dm_batch.source = 'your app name'
|
28
29
|
notification = {
|
29
30
|
:registration_id => "your_reg_id",
|
30
31
|
:data => {
|
31
32
|
:test => "test"
|
32
33
|
}
|
33
34
|
}
|
34
|
-
|
35
|
+
@c2dm_batch.send_notification(notification)
|
35
36
|
</pre>
|
36
37
|
|
37
38
|
Send notifications in batch
|
38
39
|
-----------------------------
|
39
40
|
|
40
41
|
<pre>
|
41
|
-
|
42
|
-
|
43
|
-
|
42
|
+
@c2dm_batch.email = 'your_c2dm_sender@gmail.com'
|
43
|
+
@c2dm_batch.password = 'password'
|
44
|
+
@c2dm_batch.source = 'your app name'
|
44
45
|
notification = [
|
45
46
|
{
|
46
47
|
:registration_id => "your_reg_id",
|
@@ -55,7 +56,7 @@ notification = [
|
|
55
56
|
}
|
56
57
|
}
|
57
58
|
]
|
58
|
-
errors =
|
59
|
+
errors = @c2dm_batch.send_batch_notification(notification)
|
59
60
|
</pre>
|
60
61
|
|
61
62
|
Using Typhoeus, the send_batch_notification will parallelize the request in up to 200 parallel requests. Once a request finishes, a new request will automatically get send out. The return value is an array of hashes. The hash is of the form { :registration_id => 'reg_id', :error => 'error_code' }
|
data/c2dm_batch.gemspec
CHANGED
data/lib/c2dm_batch.rb
CHANGED
data/lib/c2dm_batch/core.rb
CHANGED
@@ -1,19 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@hydra = Typhoeus::Hydra.new
|
10
|
-
@logger = Logger.new(STDOUT)
|
11
|
-
|
12
|
-
class << self
|
13
|
-
attr_accessor :auth_url, :send_url, :email, :password, :source, :logger
|
1
|
+
class C2dmBatch
|
2
|
+
attr_accessor :auth_url, :send_url, :email, :password, :source, :logger
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@auth_url = 'https://www.google.com/accounts/ClientLogin'
|
6
|
+
@send_url = 'https://android.apis.google.com/c2dm/send'
|
7
|
+
@hydra = Typhoeus::Hydra.new
|
8
|
+
@logger = Logger.new(STDOUT)
|
14
9
|
end
|
15
10
|
|
16
|
-
def
|
11
|
+
def authenticate!
|
17
12
|
request = Typhoeus::Request.new(@auth_url)
|
18
13
|
auth_options = {
|
19
14
|
'accountType' => 'HOSTED_OR_GOOGLE',
|
@@ -44,7 +39,7 @@ module C2dmBatch
|
|
44
39
|
@auth_token = auth_token
|
45
40
|
end
|
46
41
|
|
47
|
-
def
|
42
|
+
def send_notification(notification)
|
48
43
|
authenticate!
|
49
44
|
request = create_notification_request(notification)
|
50
45
|
|
@@ -53,7 +48,7 @@ module C2dmBatch
|
|
53
48
|
response = request.response
|
54
49
|
end
|
55
50
|
|
56
|
-
def
|
51
|
+
def send_batch_notifications(notifications)
|
57
52
|
authenticate!
|
58
53
|
requests = []
|
59
54
|
errors = []
|
@@ -65,6 +60,7 @@ module C2dmBatch
|
|
65
60
|
if response.success?
|
66
61
|
if response.body =~ /Error=(\w+)/
|
67
62
|
errors << { :registration_id => notification[:registration_id], :error => $1 }
|
63
|
+
@logger.error("Error received: #{response.body}")
|
68
64
|
@logger.info("Error sending: #{notification.to_json}")
|
69
65
|
else
|
70
66
|
@logger.info("Sent notification: #{notification.to_json}")
|
@@ -91,7 +87,7 @@ module C2dmBatch
|
|
91
87
|
end
|
92
88
|
|
93
89
|
private
|
94
|
-
def
|
90
|
+
def build_post_body(options={})
|
95
91
|
post_body = []
|
96
92
|
|
97
93
|
# data attributes need a key in the form of "data.key"...
|
@@ -108,7 +104,7 @@ module C2dmBatch
|
|
108
104
|
post_body.join('&')
|
109
105
|
end
|
110
106
|
|
111
|
-
def
|
107
|
+
def create_notification_request(notification)
|
112
108
|
request = Typhoeus::Request.new(@send_url)
|
113
109
|
notification[:collapse_key] = 'collapse'
|
114
110
|
request.body = build_post_body(notification)
|
@@ -3,19 +3,20 @@ require 'spec_helper'
|
|
3
3
|
describe C2dmBatch do
|
4
4
|
before do
|
5
5
|
@config = YAML::load(File.open("config.yml"))
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
@c2dm_batch = C2dmBatch.new
|
7
|
+
@c2dm_batch.email = @config['server']['username']
|
8
|
+
@c2dm_batch.password = @config['server']['password']
|
9
|
+
@c2dm_batch.source = @config['server']['source']
|
9
10
|
@reg_id = @config['client']['registration_id']
|
10
11
|
end
|
11
12
|
|
12
13
|
it "should give an auth token" do
|
13
|
-
auth_code =
|
14
|
+
auth_code = @c2dm_batch.authenticate!
|
14
15
|
auth_code.should_not eql("")
|
15
16
|
end
|
16
17
|
|
17
18
|
it "should send a notifcation" do
|
18
|
-
|
19
|
+
@c2dm_batch.send_notification(create_notification(@reg_id, "boston-college-football"))
|
19
20
|
end
|
20
21
|
|
21
22
|
it "should send notifications in batches" do
|
@@ -24,7 +25,7 @@ describe C2dmBatch do
|
|
24
25
|
teams.each do |team|
|
25
26
|
notifications << create_notification(@reg_id, team)
|
26
27
|
end
|
27
|
-
errors =
|
28
|
+
errors = @c2dm_batch.send_batch_notifications(notifications)
|
28
29
|
errors.size.should == 0
|
29
30
|
end
|
30
31
|
|
@@ -34,7 +35,7 @@ describe C2dmBatch do
|
|
34
35
|
bad_reg_id = "bad_reg_id"
|
35
36
|
notifications << create_notification(bad_reg_id, teams[0])
|
36
37
|
notifications << create_notification(@reg_id, teams[1])
|
37
|
-
errors =
|
38
|
+
errors = @c2dm_batch.send_batch_notifications(notifications)
|
38
39
|
errors.size.should == 1
|
39
40
|
errors[0][:registration_id].should == bad_reg_id
|
40
41
|
errors[0][:error].should == "InvalidRegistration"
|
@@ -43,7 +44,7 @@ describe C2dmBatch do
|
|
43
44
|
it "should return MessageToBig status code" do
|
44
45
|
notifications = []
|
45
46
|
notifications << create_notification(@reg_id, "1" * 1025)
|
46
|
-
errors =
|
47
|
+
errors = @c2dm_batch.send_batch_notifications(notifications)
|
47
48
|
errors[0][:registration_id].should == @reg_id
|
48
49
|
errors[0][:error].should == "MessageTooBig"
|
49
50
|
end
|
@@ -52,13 +53,13 @@ describe C2dmBatch do
|
|
52
53
|
hydra = Typhoeus::Hydra.new
|
53
54
|
response = Typhoeus::Response.new(:code => 503, :headers => "", :body => "registration=123")
|
54
55
|
hydra.stub(:post, 'https://android.apis.google.com/c2dm/send').and_return(response)
|
55
|
-
|
56
|
+
@c2dm_batch.instance_variable_set("@hydra", hydra)
|
56
57
|
teams = [ "buffalo-bills", "miami-dolphins", "new-york-jets"]
|
57
58
|
notifications = []
|
58
59
|
teams.each do |team|
|
59
60
|
notifications << create_notification(@reg_id, team)
|
60
61
|
end
|
61
|
-
errors =
|
62
|
+
errors = @c2dm_batch.send_batch_notifications(notifications)
|
62
63
|
errors.size.should == 3
|
63
64
|
errors[0][:error].should == 503
|
64
65
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: c2dm_batch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 1
|
9
8
|
- 2
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alex Rockwell
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-06-13 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|