<commit_before>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<commit_msg>Add support for multiple key-value pairs in payload<commit_after>require 'httparty'
require 'cgi'

class C2DM
  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 = "MyCompany-MyAppName-1.0")
    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 = self.class.post(AUTH_URL, params)

    raise response if response["Error="] #Raise error in case of auth failure

    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
    
    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, source, notifications)
    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
{'fraction_non_alphanumeric': 0.1443526170798898, 'fraction_numerical': 0.005509641873278237, 'mean_word_length': 3.2122969837587005, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, 'https://': 4, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7002615', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1148, 'n_words': 204}