c2dm 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. data/Gemfile +3 -1
  2. data/Gemfile.lock +12 -3
  3. data/README.markdown +34 -23
  4. data/Rakefile +0 -7
  5. data/VERSION +1 -1
  6. data/c2dm.gemspec +14 -11
  7. data/lib/c2dm.rb +56 -62
  8. metadata +66 -62
data/Gemfile CHANGED
@@ -5,9 +5,11 @@ source "http://rubygems.org"
5
5
 
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
+ gem "httparty"
9
+
8
10
  group :development do
9
11
  gem "shoulda", ">= 0"
10
12
  gem "bundler", "~> 1.0.0"
11
13
  gem "jeweler", "~> 1.5.1"
12
- gem "rcov", ">= 0"
14
+ gem 'simplecov', :require => false
13
15
  end
@@ -2,19 +2,28 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  git (1.2.5)
5
+ httparty (0.8.1)
6
+ multi_json
7
+ multi_xml
5
8
  jeweler (1.5.2)
6
9
  bundler (~> 1.0.0)
7
10
  git (>= 1.2.5)
8
11
  rake
9
- rake (0.9.2)
10
- rcov (0.9.9)
12
+ multi_json (1.0.4)
13
+ multi_xml (0.4.1)
14
+ rake (0.9.2.2)
11
15
  shoulda (2.11.3)
16
+ simplecov (0.5.4)
17
+ multi_json (~> 1.0.3)
18
+ simplecov-html (~> 0.5.3)
19
+ simplecov-html (0.5.3)
12
20
 
13
21
  PLATFORMS
14
22
  ruby
15
23
 
16
24
  DEPENDENCIES
17
25
  bundler (~> 1.0.0)
26
+ httparty
18
27
  jeweler (~> 1.5.1)
19
- rcov
20
28
  shoulda
29
+ simplecov
@@ -12,43 +12,54 @@ An Android device running 2.2 or newer, its registration token, and a google acc
12
12
 
13
13
  ##Usage
14
14
 
15
+ *Important*: Version 0.2.0+ decouples auth from sending so the API changed. Please update your code.
16
+
15
17
  There are two ways to use c2dm.
16
18
 
17
19
  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")```
20
+
21
+ notifications = [
22
+ {
23
+ :registration_id => "...",
24
+ :data => {
25
+ :some_message => "Some payload"
26
+ :another_message => 10
27
+ },
28
+ :collapse_key => "foobar" #optional
29
+ }
30
+ ]
31
+
32
+ C2DM.authenticate!("your@googleuser.com", "somepassword", "YourCo-App-1.0.0")
33
+ C2DM.send_notifications(notifications)
30
34
 
31
35
  ...or one at a time:
32
- ```c2dm = C2DM.new("someone@gmail.com", "and_their_password", "MyCompany-MyApp-1.0")
33
36
 
34
- notification = {
35
- :registration_id => "...",
36
- :data => {
37
- :some_message => "Some payload",
38
- :another_message => 10
39
- },
40
- :collapse_key => "foobar" #optional
41
- }
37
+ C2DM.authenticate!("your@googleuser.com", "somepassword", "YourCo-App-1.0.0")
38
+ c2dm = C2DM.new
39
+
40
+ notification = {
41
+ :registration_id => "...",
42
+ :data => {
43
+ :some_message => "Some payload",
44
+ :another_message => 10
45
+ },
46
+ :collapse_key => "foobar" #optional
47
+ }
48
+
49
+ c2dm.send_notification(notification)
50
+
51
+ Note that calling *authenticate!* will authenticate all new instances of C2DM. You can override this by passing in your own auth_token:
42
52
 
43
- c2dm.send_notification(notification)```
53
+ c2dm = C2DM.new(auth_token)
44
54
 
45
55
  ##Copyrights
46
56
 
47
- * Copyright (c) 2010-2011 Amro Mousa, Shawn Veader. See LICENSE.txt for details.
57
+ * Copyright (c) 2010-2012 Amro Mousa, Shawn Veader. See LICENSE.txt for details.
48
58
 
49
59
  ##Thanks
50
60
 
51
61
  * [Paul Chun](https://github.com/sixofhearts)
62
+ * [gowalla](https://github.com/gowalla)
52
63
 
53
64
  ##Other stuff
54
65
 
data/Rakefile CHANGED
@@ -34,13 +34,6 @@ Rake::TestTask.new(:test) do |test|
34
34
  test.verbose = true
35
35
  end
36
36
 
37
- require 'rcov/rcovtask'
38
- Rcov::RcovTask.new do |test|
39
- test.libs << 'test'
40
- test.pattern = 'test/**/test_*.rb'
41
- test.verbose = true
42
- end
43
-
44
37
  task :default => :test
45
38
 
46
39
  require 'rake/rdoctask'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.2.0
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{c2dm}
8
- s.version = "0.1.6"
7
+ s.name = "c2dm"
8
+ s.version = "0.2.0"
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-06-23}
13
- s.description = %q{c2dm sends push notifications to Android devices via google c2dm.}
14
- s.email = %q{amromousa@gmail.com}
12
+ s.date = "2012-02-07"
13
+ s.description = "c2dm sends push notifications to Android devices via google c2dm."
14
+ s.email = "amromousa@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.markdown"
@@ -29,11 +29,11 @@ Gem::Specification.new do |s|
29
29
  "test/helper.rb",
30
30
  "test/test_c2dm.rb"
31
31
  ]
32
- s.homepage = %q{http://github.com/amro/c2dm}
32
+ s.homepage = "http://github.com/amro/c2dm"
33
33
  s.licenses = ["MIT"]
34
34
  s.require_paths = ["lib"]
35
- s.rubygems_version = %q{1.6.2}
36
- s.summary = %q{c2dm sends push notifications to Android devices via google c2dm.}
35
+ s.rubygems_version = "1.8.10"
36
+ s.summary = "c2dm sends push notifications to Android devices via google c2dm."
37
37
  s.test_files = [
38
38
  "test/helper.rb",
39
39
  "test/test_c2dm.rb"
@@ -43,23 +43,26 @@ Gem::Specification.new do |s|
43
43
  s.specification_version = 3
44
44
 
45
45
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_runtime_dependency(%q<httparty>, [">= 0"])
46
47
  s.add_development_dependency(%q<shoulda>, [">= 0"])
47
48
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
49
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
49
- s.add_development_dependency(%q<rcov>, [">= 0"])
50
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
50
51
  s.add_runtime_dependency(%q<httparty>, ["> 0.6.0"])
51
52
  else
53
+ s.add_dependency(%q<httparty>, [">= 0"])
52
54
  s.add_dependency(%q<shoulda>, [">= 0"])
53
55
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
56
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
55
- s.add_dependency(%q<rcov>, [">= 0"])
57
+ s.add_dependency(%q<simplecov>, [">= 0"])
56
58
  s.add_dependency(%q<httparty>, ["> 0.6.0"])
57
59
  end
58
60
  else
61
+ s.add_dependency(%q<httparty>, [">= 0"])
59
62
  s.add_dependency(%q<shoulda>, [">= 0"])
60
63
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
64
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
62
- s.add_dependency(%q<rcov>, [">= 0"])
65
+ s.add_dependency(%q<simplecov>, [">= 0"])
63
66
  s.add_dependency(%q<httparty>, ["> 0.6.0"])
64
67
  end
65
68
  end
@@ -5,48 +5,70 @@ class C2DM
5
5
  include HTTParty
6
6
  default_timeout 30
7
7
 
8
- attr_accessor :timeout, :username, :password, :source, :access_token
8
+ attr_accessor :timeout, :auth_token
9
9
 
10
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'
11
+ PUSH_URL = 'http://android.apis.google.com/c2dm/send' # Work around expired/bad SSL cert...
13
12
 
14
- def initialize(username=nil, password=nil, source=DEFAULT_SOURCE)
15
- @username = username
16
- @password = password
17
- @source = source
13
+ class << self
14
+ attr_accessor :auth_token
15
+
16
+ def authenticate!(username, password, source = nil)
17
+ auth_options = {
18
+ 'accountType' => 'HOSTED_OR_GOOGLE',
19
+ 'service' => 'ac2dm',
20
+ 'Email' => username,
21
+ 'Passwd' => password,
22
+ 'source' => source || 'MyCompany-MyAppName-1.0'
23
+ }
24
+ post_body = build_post_body(auth_options)
25
+
26
+ params = {
27
+ :body => post_body,
28
+ :headers => {
29
+ 'Content-type' => 'application/x-www-form-urlencoded',
30
+ 'Content-length' => post_body.length.to_s
31
+ }
32
+ }
18
33
 
19
- authenticate!
20
- end
34
+ response = self.post(AUTH_URL, params)
21
35
 
22
- def authenticated?
23
- !@auth_token.nil?
24
- end
36
+ # check for authentication failures
37
+ raise response.parsed_response if response['Error=']
25
38
 
26
- def authenticate!(username=nil, password=nil, source=nil)
27
- auth_options = {
28
- 'accountType' => 'HOSTED_OR_GOOGLE',
29
- 'service' => 'ac2dm',
30
- 'Email' => username || self.username,
31
- 'Passwd' => password || self.password,
32
- 'source' => source || self.source
33
- }
34
- post_body = build_post_body(auth_options)
35
-
36
- params = {
37
- :body => post_body,
38
- :headers => {
39
- 'Content-type' => 'application/x-www-form-urlencoded',
40
- 'Content-length' => post_body.length.to_s
41
- }
42
- }
39
+ @auth_token = response.body.split("\n")[2].gsub('Auth=', '')
40
+ end
43
41
 
44
- response = self.class.post(AUTH_URL, params)
42
+ def send_notifications(notifications = [])
43
+ c2dm = C2DM.new(@auth_token)
44
+ notifications.collect do |notification|
45
+ {
46
+ :body => c2dm.send_notification(notification),
47
+ :registration_id => notification[:registration_id]
48
+ }
49
+ end
50
+ end
51
+
52
+ def build_post_body(options={})
53
+ post_body = []
54
+
55
+ # data attributes need a key in the form of "data.key"...
56
+ data_attributes = options.delete(:data)
57
+ data_attributes.each_pair do |k,v|
58
+ post_body << "data.#{k}=#{CGI::escape(v.to_s)}"
59
+ end if data_attributes
60
+
61
+ options.each_pair do |k,v|
62
+ post_body << "#{k}=#{CGI::escape(v.to_s)}"
63
+ end
45
64
 
46
- # check for authentication failures
47
- raise response.parsed_response if response['Error=']
65
+ post_body.join('&')
66
+ end
67
+
68
+ end
48
69
 
49
- @auth_token = response.body.split("\n")[2].gsub('Auth=', '')
70
+ def initialize(auth_token = nil)
71
+ @auth_token = auth_token || self.class.auth_token
50
72
  end
51
73
 
52
74
  # {
@@ -59,7 +81,7 @@ class C2DM
59
81
  # }
60
82
  def send_notification(options)
61
83
  options[:collapse_key] ||= 'foo'
62
- post_body = build_post_body(options)
84
+ post_body = self.class.build_post_body(options)
63
85
 
64
86
  params = {
65
87
  :body => post_body,
@@ -73,32 +95,4 @@ class C2DM
73
95
  self.class.post(PUSH_URL, params)
74
96
  end
75
97
 
76
- class << self
77
- def send_notifications(username=nil, password=nil, notifications=[], source=nil)
78
- c2dm = C2DM.new(username, password, source)
79
-
80
- notifications.collect do |notification|
81
- { :body => c2dm.send_notification(notification),
82
- :registration_id => notification[:registration_id] }
83
- end
84
- end
85
- end
86
-
87
- private
88
- def build_post_body(options={})
89
- post_body = []
90
-
91
- # data attributes need a key in the form of "data.key"...
92
- data_attributes = options.delete(:data)
93
- data_attributes.each_pair do |k,v|
94
- post_body << "data.#{k}=#{CGI::escape(v.to_s)}"
95
- end if data_attributes
96
-
97
- options.each_pair do |k,v|
98
- post_body << "#{k}=#{CGI::escape(v.to_s)}"
99
- end
100
-
101
- post_body.join('&')
102
- end
103
-
104
98
  end
metadata CHANGED
@@ -1,83 +1,90 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: c2dm
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
4
5
  prerelease:
5
- version: 0.1.6
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Amro Mousa
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-06-23 00:00:00 -04:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2012-02-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70329319587220 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70329319587220
25
+ - !ruby/object:Gem::Dependency
17
26
  name: shoulda
18
- requirement: &id001 !ruby/object:Gem::Requirement
27
+ requirement: &70329319586360 !ruby/object:Gem::Requirement
19
28
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
24
33
  type: :development
25
34
  prerelease: false
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
35
+ version_requirements: *70329319586360
36
+ - !ruby/object:Gem::Dependency
28
37
  name: bundler
29
- requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirement: &70329319585400 !ruby/object:Gem::Requirement
30
39
  none: false
31
- requirements:
40
+ requirements:
32
41
  - - ~>
33
- - !ruby/object:Gem::Version
42
+ - !ruby/object:Gem::Version
34
43
  version: 1.0.0
35
44
  type: :development
36
45
  prerelease: false
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
46
+ version_requirements: *70329319585400
47
+ - !ruby/object:Gem::Dependency
39
48
  name: jeweler
40
- requirement: &id003 !ruby/object:Gem::Requirement
49
+ requirement: &70329319584520 !ruby/object:Gem::Requirement
41
50
  none: false
42
- requirements:
51
+ requirements:
43
52
  - - ~>
44
- - !ruby/object:Gem::Version
53
+ - !ruby/object:Gem::Version
45
54
  version: 1.5.1
46
55
  type: :development
47
56
  prerelease: false
48
- version_requirements: *id003
49
- - !ruby/object:Gem::Dependency
50
- name: rcov
51
- requirement: &id004 !ruby/object:Gem::Requirement
57
+ version_requirements: *70329319584520
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &70329319583540 !ruby/object:Gem::Requirement
52
61
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
57
66
  type: :development
58
67
  prerelease: false
59
- version_requirements: *id004
60
- - !ruby/object:Gem::Dependency
68
+ version_requirements: *70329319583540
69
+ - !ruby/object:Gem::Dependency
61
70
  name: httparty
62
- requirement: &id005 !ruby/object:Gem::Requirement
71
+ requirement: &70329319582780 !ruby/object:Gem::Requirement
63
72
  none: false
64
- requirements:
65
- - - ">"
66
- - !ruby/object:Gem::Version
73
+ requirements:
74
+ - - ! '>'
75
+ - !ruby/object:Gem::Version
67
76
  version: 0.6.0
68
77
  type: :runtime
69
78
  prerelease: false
70
- version_requirements: *id005
79
+ version_requirements: *70329319582780
71
80
  description: c2dm sends push notifications to Android devices via google c2dm.
72
81
  email: amromousa@gmail.com
73
82
  executables: []
74
-
75
83
  extensions: []
76
-
77
- extra_rdoc_files:
84
+ extra_rdoc_files:
78
85
  - LICENSE.txt
79
86
  - README.markdown
80
- files:
87
+ files:
81
88
  - .document
82
89
  - Gemfile
83
90
  - Gemfile.lock
@@ -89,37 +96,34 @@ files:
89
96
  - lib/c2dm.rb
90
97
  - test/helper.rb
91
98
  - test/test_c2dm.rb
92
- has_rdoc: true
93
99
  homepage: http://github.com/amro/c2dm
94
- licenses:
100
+ licenses:
95
101
  - MIT
96
102
  post_install_message:
97
103
  rdoc_options: []
98
-
99
- require_paths:
104
+ require_paths:
100
105
  - lib
101
- required_ruby_version: !ruby/object:Gem::Requirement
106
+ required_ruby_version: !ruby/object:Gem::Requirement
102
107
  none: false
103
- requirements:
104
- - - ">="
105
- - !ruby/object:Gem::Version
106
- hash: -2874560584859296799
107
- segments:
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ segments:
108
113
  - 0
109
- version: "0"
110
- required_rubygems_version: !ruby/object:Gem::Requirement
114
+ hash: 21375347209724665
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
116
  none: false
112
- requirements:
113
- - - ">="
114
- - !ruby/object:Gem::Version
115
- version: "0"
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
116
121
  requirements: []
117
-
118
122
  rubyforge_project:
119
- rubygems_version: 1.6.2
123
+ rubygems_version: 1.8.10
120
124
  signing_key:
121
125
  specification_version: 3
122
126
  summary: c2dm sends push notifications to Android devices via google c2dm.
123
- test_files:
127
+ test_files:
124
128
  - test/helper.rb
125
129
  - test/test_c2dm.rb