lib/c2dm.rb in c2dm-0.1.4 vs lib/c2dm.rb in c2dm-0.1.5

- old
+ new

@@ -3,36 +3,52 @@ class C2DM include HTTParty default_timeout 30 - attr_accessor :timeout + attr_accessor :timeout, :username, :password, :source, :access_token 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 = DEFAULT_SOURCE) - auth(username, password, source) + def initialize(username=nil, password=nil, source=DEFAULT_SOURCE) + @username = username + @password = password + @source = source + + authenticate! end - def auth(username, password, source) - post_body = "accountType=HOSTED_OR_GOOGLE&Email=#{username}&Passwd=#{password}&service=ac2dm&source=#{source}" + def authenticated? + !@auth_token.nil? + end + + def authenticate!(username=nil, password=nil, source=nil) + auth_options = { + 'accountType' => 'HOSTED_OR_GOOGLE', + 'service' => 'ac2dm', + 'Email' => username || self.username, + 'Passwd' => password || self.password, + 'source' => source || self.source + } + post_body = build_post_body(auth_options) + params = { - :body => post_body, + :body => post_body, :headers => { - 'Content-type' => 'application/x-www-form-urlencoded', - 'Content-length' => "#{post_body.length}" + 'Content-type' => 'application/x-www-form-urlencoded', + 'Content-length' => post_body.length.to_s } } response = self.class.post(AUTH_URL, params) - raise response if response["Error="] #Raise error in case of auth failure + # check for authentication failures + raise response if response['Error='] - response_split = response.body.split("\n") - @auth_token = response_split[2].gsub("Auth=", "") + @auth_token = response.body.split("\n")[2].gsub('Auth=', '') end # { # :registration_id => "...", # :data => { @@ -40,37 +56,49 @@ # :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 + options[:collapse_key] ||= 'foo' + post_body = build_post_body(options) - 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, + :body => post_body, :headers => { - 'Authorization' => "GoogleLogin auth=#{@auth_token}", - 'Content-type' => 'application/x-www-form-urlencoded', + '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]} + class << self + def send_notifications(username=nil, password=nil, notifications=[], source=nil) + c2dm = C2DM.new(username, password, source) + + notifications.collect do |notification| + { :body => c2dm.send_notification(notification), + :registration_id => notification[:registration_id] } + end end - responses end + +private + def build_post_body(options={}) + post_body = [] + + # data attributes need a key in the form of "data.key"... + data_attributes = options.delete(:data) + data_attributes.each_pair do |k,v| + post_body << "data.#{k}=#{CGI::escape(v.to_s)}" + end if data_attributes + + options.each_pair do |k,v| + post_body << "#{k}=#{CGI::escape(v.to_s)}" + end + + post_body.join('&') + end + end \ No newline at end of file