Sha256: 40a7f913da0ab7337478d1ea21a7b3b120dc3d41f6242098b814ae13d6d07fe7

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

require 'httparty'
require 'cgi'

module C2DM
  class Push
    include HTTParty
    default_timeout 30

    attr_accessor :timeout

    AUTH_URL = 'https://www.google.com/accounts/ClientLogin'
    PUSH_URL = 'https://android.apis.google.com/c2dm/send'

    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}"}}

      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

    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}"}}

      response = Push.post(PUSH_URL, params)
      response
    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
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
c2dm-0.1.2 lib/c2dm.rb