c2dm 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.markdown +27 -9
  2. data/VERSION +1 -1
  3. data/c2dm.gemspec +2 -2
  4. data/lib/c2dm.rb +61 -37
  5. metadata +3 -3
@@ -14,16 +14,34 @@ An Android device running 2.2 or newer, its registration token, and a google acc
14
14
 
15
15
  There are two ways to use c2dm.
16
16
 
17
- Sending an array of notifications:
18
-
19
- notifications = [{:registration_id => registration_id2_from_client, :message => "Some awesome message"}]
20
- C2DM::Push.send_notifications(your_google_account_id, your_password, "MyCompany-MyApp-1.0", notifications)
21
-
22
- or one at a time:
17
+ Sending many notifications:
18
+ notifications = [
19
+ {
20
+ :registration_id => "...",
21
+ :data => {
22
+ :some_message => "Some payload"
23
+ :another_message => 10
24
+ },
25
+ :collapse_key => "foobar" #optional
26
+ }
27
+ ]
28
+
29
+ C2DM.send_notifications("someone@gmail.com", "and_their_password", notifications, "MyCompany-MyApp-1.0.0")
30
+
31
+ ...or one at a time:
32
+ c2dm = C2DM.new("someone@gmail.com", "and_their_password", "MyCompany-MyApp-1.0")
33
+
34
+ notification = {
35
+ :registration_id => "...",
36
+ :data => {
37
+ :some_message => "Some payload"
38
+ :another_message => 10
39
+ },
40
+ :collapse_key => "foobar" #optional
41
+ }
23
42
 
24
- c2dm = C2DM::Push.new(your_google_account_id, your_password, "MyCompany-MyApp-1.0")
25
- c2dm.send_notification(registration_id2_from_client, "Some awesome message")
43
+ c2dm.send_notification(notification)
26
44
 
27
45
  ##Copyrights
28
46
 
29
- * Copyright (c) 2010 Amro Mousa. See LICENSE.txt for details.
47
+ * Copyright (c) 2010-2011 Amro Mousa. See LICENSE.txt for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{c2dm}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Amro Mousa"]
12
- s.date = %q{2011-03-29}
12
+ s.date = %q{2011-03-31}
13
13
  s.description = %q{c2dm sends push notifications to Android devices via google c2dm.}
14
14
  s.email = %q{amromousa@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,48 +1,72 @@
1
1
  require 'httparty'
2
2
  require 'cgi'
3
3
 
4
- module C2DM
5
- class Push
6
- include HTTParty
7
- default_timeout 30
8
-
9
- attr_accessor :timeout
10
-
11
- AUTH_URL = 'https://www.google.com/accounts/ClientLogin'
12
- PUSH_URL = 'https://android.apis.google.com/c2dm/send'
13
-
14
- def initialize(username, password, source)
15
- post_body = "accountType=HOSTED_OR_GOOGLE&Email=#{username}&Passwd=#{password}&service=ac2dm&source=#{source}"
16
- params = {:body => post_body,
17
- :headers => {'Content-type' => 'application/x-www-form-urlencoded',
18
- 'Content-length' => "#{post_body.length}"}}
19
-
20
- response = Push.post(AUTH_URL, params)
21
-
22
- raise response if response["Error="]
23
-
24
- response_split = response.body.split("\n")
25
- @auth_token = response_split[2].gsub("Auth=", "")
26
- end
4
+ class C2DM
5
+ include HTTParty
6
+ default_timeout 30
7
+
8
+ attr_accessor :timeout
9
+
10
+ AUTH_URL = 'https://www.google.com/accounts/ClientLogin'
11
+ PUSH_URL = 'https://android.apis.google.com/c2dm/send'
12
+ DEFAULT_SOURCE = 'MyCompany-MyAppName-1.0'
27
13
 
28
- def send_notification(registration_id, message)
29
- post_body = "registration_id=#{registration_id}&collapse_key=foobar&data.message=#{CGI::escape(message)}"
30
- params = {:body => post_body,
31
- :headers => {'Authorization' => "GoogleLogin auth=#{@auth_token}"}}
14
+ def initialize(username, password, source = DEFAULT_SOURCE)
15
+ post_body = "accountType=HOSTED_OR_GOOGLE&Email=#{username}&Passwd=#{password}&service=ac2dm&source=#{source}"
16
+ params = {
17
+ :body => post_body,
18
+ :headers => {
19
+ 'Content-type' => 'application/x-www-form-urlencoded',
20
+ 'Content-length' => "#{post_body.length}"
21
+ }
22
+ }
32
23
 
33
- response = Push.post(PUSH_URL, params)
34
- response
24
+ response = self.class.post(AUTH_URL, params)
25
+
26
+ raise response if response["Error="] #Raise error in case of auth failure
27
+
28
+ response_split = response.body.split("\n")
29
+ @auth_token = response_split[2].gsub("Auth=", "")
30
+ end
31
+
32
+ # {
33
+ # :registration_id => "...",
34
+ # :data => {
35
+ # :some_message => "Hi!",
36
+ # :another_message => 7
37
+ # }
38
+ # :collapse_key => "optional collapse_key string"
39
+ # }
40
+ def send_notification(options)
41
+ post_body = []
42
+ options.delete(:data).each_pair do |k,v|
43
+ post_body << "data.#{k}=#{CGI::escape(v.to_s)}"
35
44
  end
36
45
 
37
- def self.send_notifications(username, password, source, notifications)
38
- c2dm = Push.new(username, password, source)
39
-
40
- responses = []
41
- notifications.each do |notification|
42
- responses << {:body => c2dm.send_notification(notification[:registration_id], notification[:message]), :registration_id => notification[:registration_id]}
43
- end
44
- responses
46
+ options[:collapse_key] = "foo" unless options[:collapse_key]
47
+
48
+ options.each_pair do |k,v|
49
+ post_body << "#{k}=#{CGI::escape(v.to_s)}"
45
50
  end
46
51
 
52
+ post_body = post_body.join("&")
53
+ params = {
54
+ :body => post_body,
55
+ :headers => {
56
+ 'Authorization' => "GoogleLogin auth=#{@auth_token}",
57
+ 'Content-type' => 'application/x-www-form-urlencoded',
58
+ 'Content-length' => "#{post_body.length}"
59
+ }
60
+ }
61
+ self.class.post(PUSH_URL, params)
62
+ end
63
+
64
+ def self.send_notifications(username, password, notifications, source = DEFAULT_SOURCE)
65
+ c2dm = C2DM.new(username, password, source)
66
+ responses = []
67
+ notifications.each do |notification|
68
+ responses << {:body => c2dm.send_notification(notification), :registration_id => notification[:registration_id]}
69
+ end
70
+ responses
47
71
  end
48
72
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: c2dm
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Amro Mousa
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-29 00:00:00 -04:00
13
+ date: 2011-03-31 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -103,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
103
  requirements:
104
104
  - - ">="
105
105
  - !ruby/object:Gem::Version
106
- hash: -1271509393017915368
106
+ hash: 4436036205569639874
107
107
  segments:
108
108
  - 0
109
109
  version: "0"