c2dm_batch 0.1.2 → 0.2.0

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/README.md CHANGED
@@ -14,33 +14,34 @@ gem install typhoeus
14
14
 
15
15
  Configuration
16
16
  -------------
17
- C2dmBatch.email = 'your_c2dm_sender@gmail.com'
18
- C2dmBatch.password = 'password'
19
- C2dmBatch.source = 'your app name'
20
- C2dmBatch.logger = Logger.new(STDOUT) # default logger
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
- C2dmBatch.email = 'your_c2dm_sender@gmail.com'
26
- C2dmBatch.password = 'password'
27
- C2dmBatch.source = 'your app name'
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
- C2dmBatch.send_notification(notification)
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
- C2dmBatch.email = 'your_c2dm_sender@gmail.com'
42
- C2dmBatch.password = 'password'
43
- C2dmBatch.source = 'your app name'
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 = C2dmBatch.send_batch_notification(notification)
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
@@ -6,7 +6,7 @@ $:.unshift lib unless $:.include?(lib)
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "c2dm_batch"
9
- s.version = '0.1.2'
9
+ s.version = '0.2.0'
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.authors = ["Alex Rockwell"]
12
12
  s.email = ["arockwell@gmail.com"]
data/lib/c2dm_batch.rb CHANGED
@@ -8,7 +8,4 @@ require 'json'
8
8
 
9
9
  $:.unshift File.dirname(__FILE__)
10
10
 
11
- module C2dmBatch
12
- end
13
-
14
11
  require 'c2dm_batch/core'
@@ -1,19 +1,14 @@
1
- module C2dmBatch
2
- @auth_url = 'https://www.google.com/accounts/ClientLogin'
3
- @send_url = 'https://android.apis.google.com/c2dm/send'
4
-
5
- @email = nil
6
- @password = nil
7
- @source = nil
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 self.authenticate!
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 self.send_notification(notification)
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 self.send_batch_notifications(notifications)
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 self.build_post_body(options={})
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 self.create_notification_request(notification)
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
- C2dmBatch.email = @config['server']['username']
7
- C2dmBatch.password = @config['server']['password']
8
- C2dmBatch.source = @config['server']['source']
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 = C2dmBatch.authenticate!
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
- C2dmBatch.send_notification(create_notification(@reg_id, "boston-college-football"))
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 = C2dmBatch.send_batch_notifications(notifications)
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 = C2dmBatch.send_batch_notifications(notifications)
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 = C2dmBatch.send_batch_notifications(notifications)
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
- C2dmBatch.instance_variable_set("@hydra", hydra)
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 = C2dmBatch.send_batch_notifications(notifications)
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: 31
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
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: 2011-10-17 00:00:00 -07:00
18
+ date: 2012-06-13 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency