<commit_before>require 'signature'
require 'digest/md5'
require 'multi_json'

module Pusher
  class Request
    attr_reader :body, :params

    def initialize(client, verb, uri, params, body = nil)
      @client, @verb, @uri = client, verb, uri
      @head = {}

      @body = body
      if body
        params[:body_md5] = Digest::MD5.hexdigest(body)
        @head['Content-Type'] = 'application/json'
      end

      request = Signature::Request.new(verb.to_s.upcase, uri.path, params)
      request.sign(client.authentication_token)
      @params = request.signed_params
    end

    def send_sync
      http = @client.sync_http_client

      begin
        response = http.request(@verb, @uri, @params, @body, @head)
      rescue HTTPClient::BadResponseError, HTTPClient::TimeoutError,
             SocketError, Errno::ECONNREFUSED => e
        error = Pusher::HTTPError.new("#{e.message} (#{e.class})")
        error.original_error = e
        raise error
      end

      body = response.body ? response.body.chomp : nil

      return handle_response(response.code.to_i, body)
    end

    def send_async
      if defined?(EventMachine) && EventMachine.reactor_running?
        http_client = @client.em_http_client(@uri)
        df = EM::DefaultDeferrable.new

        http = case @verb
        when :post
          http_client.post({
            :query => @params, :body => @body, :head => @head
          })
        when :get
          http_client.get({
            :query => @params, :head => @head
          })
        else
          raise "Unsuported verb"
        end
        http.callback {
          begin
            df.succeed(handle_response(http.response_header.status, http.response.chomp))
          rescue => e<commit_msg>Spelling correction<commit_after>require 'signature'
require 'digest/md5'
require 'multi_json'

module Pusher
  class Request
    attr_reader :body, :params

    def initialize(client, verb, uri, params, body = nil)
      @client, @verb, @uri = client, verb, uri
      @head = {}

      @body = body
      if body
        params[:body_md5] = Digest::MD5.hexdigest(body)
        @head['Content-Type'] = 'application/json'
      end

      request = Signature::Request.new(verb.to_s.upcase, uri.path, params)
      request.sign(client.authentication_token)
      @params = request.signed_params
    end

    def send_sync
      http = @client.sync_http_client

      begin
        response = http.request(@verb, @uri, @params, @body, @head)
      rescue HTTPClient::BadResponseError, HTTPClient::TimeoutError,
             SocketError, Errno::ECONNREFUSED => e
        error = Pusher::HTTPError.new("#{e.message} (#{e.class})")
        error.original_error = e
        raise error
      end

      body = response.body ? response.body.chomp : nil

      return handle_response(response.code.to_i, body)
    end

    def send_async
      if defined?(EventMachine) && EventMachine.reactor_running?
        http_client = @client.em_http_client(@uri)
        df = EM::DefaultDeferrable.new

        http = case @verb
        when :post
          http_client.post({
            :query => @params, :body => @body, :head => @head
          })
        when :get
          http_client.get({
            :query => @params, :head => @head
          })
        else
          raise "Unsupported verb"
        end
        http.callback {
          begin
            df.succeed(handle_response(http.response_header.status, http.response.chomp))
          rescue => e
{'fraction_non_alphanumeric': 0.1139421700543945, 'fraction_numerical': 0.0017177211565989122, 'mean_word_length': 2.2593283582089554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4081078', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1042, 'n_words': 232}