c2dm 0.1.4 → 0.1.5

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.
Files changed (5) hide show
  1. data/README.markdown +1 -1
  2. data/VERSION +1 -1
  3. data/c2dm.gemspec +2 -2
  4. data/lib/c2dm.rb +59 -31
  5. metadata +3 -3
@@ -44,7 +44,7 @@ Sending many notifications:
44
44
 
45
45
  ##Copyrights
46
46
 
47
- * Copyright (c) 2010-2011 Amro Mousa. See LICENSE.txt for details.
47
+ * Copyright (c) 2010-2011 Amro Mousa, Shawn Veader. See LICENSE.txt for details.
48
48
 
49
49
  ##Other stuff
50
50
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{c2dm}
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
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-31}
12
+ s.date = %q{2011-04-01}
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 = [
@@ -5,32 +5,48 @@ class C2DM
5
5
  include HTTParty
6
6
  default_timeout 30
7
7
 
8
- attr_accessor :timeout
8
+ attr_accessor :timeout, :username, :password, :source, :access_token
9
9
 
10
10
  AUTH_URL = 'https://www.google.com/accounts/ClientLogin'
11
11
  PUSH_URL = 'https://android.apis.google.com/c2dm/send'
12
12
  DEFAULT_SOURCE = 'MyCompany-MyAppName-1.0'
13
13
 
14
- def initialize(username, password, source = DEFAULT_SOURCE)
15
- auth(username, password, source)
14
+ def initialize(username=nil, password=nil, source=DEFAULT_SOURCE)
15
+ @username = username
16
+ @password = password
17
+ @source = source
18
+
19
+ authenticate!
20
+ end
21
+
22
+ def authenticated?
23
+ !@auth_token.nil?
16
24
  end
17
25
 
18
- def auth(username, password, source)
19
- post_body = "accountType=HOSTED_OR_GOOGLE&Email=#{username}&Passwd=#{password}&service=ac2dm&source=#{source}"
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
+
20
36
  params = {
21
- :body => post_body,
37
+ :body => post_body,
22
38
  :headers => {
23
- 'Content-type' => 'application/x-www-form-urlencoded',
24
- 'Content-length' => "#{post_body.length}"
39
+ 'Content-type' => 'application/x-www-form-urlencoded',
40
+ 'Content-length' => post_body.length.to_s
25
41
  }
26
42
  }
27
43
 
28
44
  response = self.class.post(AUTH_URL, params)
29
45
 
30
- raise response if response["Error="] #Raise error in case of auth failure
46
+ # check for authentication failures
47
+ raise response if response['Error=']
31
48
 
32
- response_split = response.body.split("\n")
33
- @auth_token = response_split[2].gsub("Auth=", "")
49
+ @auth_token = response.body.split("\n")[2].gsub('Auth=', '')
34
50
  end
35
51
 
36
52
  # {
@@ -42,35 +58,47 @@ class C2DM
42
58
  # :collapse_key => "optional collapse_key string"
43
59
  # }
44
60
  def send_notification(options)
45
- post_body = []
46
- options.delete(:data).each_pair do |k,v|
47
- post_body << "data.#{k}=#{CGI::escape(v.to_s)}"
48
- end
49
-
50
- options[:collapse_key] = "foo" unless options[:collapse_key]
51
-
52
- options.each_pair do |k,v|
53
- post_body << "#{k}=#{CGI::escape(v.to_s)}"
54
- end
61
+ options[:collapse_key] ||= 'foo'
62
+ post_body = build_post_body(options)
55
63
 
56
- post_body = post_body.join("&")
57
64
  params = {
58
- :body => post_body,
65
+ :body => post_body,
59
66
  :headers => {
60
- 'Authorization' => "GoogleLogin auth=#{@auth_token}",
61
- 'Content-type' => 'application/x-www-form-urlencoded',
67
+ 'Authorization' => "GoogleLogin auth=#{@auth_token}",
68
+ 'Content-type' => 'application/x-www-form-urlencoded',
62
69
  'Content-length' => "#{post_body.length}"
63
70
  }
64
71
  }
72
+
65
73
  self.class.post(PUSH_URL, params)
66
74
  end
67
75
 
68
- def self.send_notifications(username, password, notifications, source = DEFAULT_SOURCE)
69
- c2dm = C2DM.new(username, password, source)
70
- responses = []
71
- notifications.each do |notification|
72
- responses << {:body => c2dm.send_notification(notification), :registration_id => notification[:registration_id]}
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)}"
73
99
  end
74
- responses
100
+
101
+ post_body.join('&')
75
102
  end
103
+
76
104
  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.4
5
+ version: 0.1.5
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-31 00:00:00 -04:00
13
+ date: 2011-04-01 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: -2523248155411028688
106
+ hash: 382073877203318182
107
107
  segments:
108
108
  - 0
109
109
  version: "0"