lib/c2dm.rb in c2dm-0.1.2 vs lib/c2dm.rb in c2dm-0.1.3
- old
+ new
@@ -1,48 +1,72 @@
require 'httparty'
require 'cgi'
-module C2DM
- class Push
- include HTTParty
- default_timeout 30
+class C2DM
+ include HTTParty
+ default_timeout 30
- attr_accessor :timeout
+ attr_accessor :timeout
- AUTH_URL = 'https://www.google.com/accounts/ClientLogin'
- PUSH_URL = 'https://android.apis.google.com/c2dm/send'
+ AUTH_URL = 'https://www.google.com/accounts/ClientLogin'
+ PUSH_URL = 'https://android.apis.google.com/c2dm/send'
+ DEFAULT_SOURCE = 'MyCompany-MyAppName-1.0'
- def initialize(username, password, source)
- post_body = "accountType=HOSTED_OR_GOOGLE&Email=#{username}&Passwd=#{password}&service=ac2dm&source=#{source}"
- params = {:body => post_body,
- :headers => {'Content-type' => 'application/x-www-form-urlencoded',
- 'Content-length' => "#{post_body.length}"}}
+ def initialize(username, password, source = DEFAULT_SOURCE)
+ post_body = "accountType=HOSTED_OR_GOOGLE&Email=#{username}&Passwd=#{password}&service=ac2dm&source=#{source}"
+ params = {
+ :body => post_body,
+ :headers => {
+ 'Content-type' => 'application/x-www-form-urlencoded',
+ 'Content-length' => "#{post_body.length}"
+ }
+ }
- response = Push.post(AUTH_URL, params)
-
- raise response if response["Error="]
-
- response_split = response.body.split("\n")
- @auth_token = response_split[2].gsub("Auth=", "")
- end
+ response = self.class.post(AUTH_URL, params)
- def send_notification(registration_id, message)
- post_body = "registration_id=#{registration_id}&collapse_key=foobar&data.message=#{CGI::escape(message)}"
- params = {:body => post_body,
- :headers => {'Authorization' => "GoogleLogin auth=#{@auth_token}"}}
+ raise response if response["Error="] #Raise error in case of auth failure
- response = Push.post(PUSH_URL, params)
- response
+ response_split = response.body.split("\n")
+ @auth_token = response_split[2].gsub("Auth=", "")
+ end
+
+ # {
+ # :registration_id => "...",
+ # :data => {
+ # :some_message => "Hi!",
+ # :another_message => 7
+ # }
+ # :collapse_key => "optional collapse_key string"
+ # }
+ def send_notification(options)
+ post_body = []
+ options.delete(:data).each_pair do |k,v|
+ post_body << "data.#{k}=#{CGI::escape(v.to_s)}"
end
- def self.send_notifications(username, password, source, notifications)
- c2dm = Push.new(username, password, source)
-
- responses = []
- notifications.each do |notification|
- responses << {:body => c2dm.send_notification(notification[:registration_id], notification[:message]), :registration_id => notification[:registration_id]}
- end
- responses
+ options[:collapse_key] = "foo" unless options[:collapse_key]
+
+ options.each_pair do |k,v|
+ post_body << "#{k}=#{CGI::escape(v.to_s)}"
end
+ post_body = post_body.join("&")
+ params = {
+ :body => post_body,
+ :headers => {
+ 'Authorization' => "GoogleLogin auth=#{@auth_token}",
+ 'Content-type' => 'application/x-www-form-urlencoded',
+ 'Content-length' => "#{post_body.length}"
+ }
+ }
+ self.class.post(PUSH_URL, params)
+ end
+
+ def self.send_notifications(username, password, notifications, source = DEFAULT_SOURCE)
+ c2dm = C2DM.new(username, password, source)
+ responses = []
+ notifications.each do |notification|
+ responses << {:body => c2dm.send_notification(notification), :registration_id => notification[:registration_id]}
+ end
+ responses
end
end
\ No newline at end of file