<commit_before>module Keen
  class Client
    module PublishingMethods

      # @deprecated
      #
      # Publishes a synchronous event
      # @param event_collection
      # @param [Hash] event properties
      #
      # @return the JSON response from the API
      def add_event(event_collection, properties, options={})
        self.publish(event_collection, properties, options)
      end

      # Publishes a synchronous event
      # See detailed documentation here
      # https://keen.io/docs/api/reference/#event-collection-resource
      #
      # @param event_collection
      # @param [Hash] event properties
      #
      # @return the JSON response from the API
      def publish(event_collection, properties)
        ensure_project_id!
        ensure_write_key!
        check_event_data!(event_collection, properties)
        publish_body(
          api_event_collection_resource_path(event_collection),
          MultiJson.encode(properties),
          "publish")
      end

      # Publishes a batch of events
      # See detailed documentation here
      # https://keen.io/docs/api/reference/#post-request-body-example-of-batch-event-posting
      #
      # @param events - a hash where the keys are event collection names
      # and the values are arrays of hashes (event properties)
      #
      # @return the JSON response from the API
      def publish_batch(events)
        ensure_project_id!
        ensure_write_key!
        publish_body(
          api_events_resource_path,
          MultiJson.encode(events),
          "publish")
      end

      # Publishes an asynchronous event
      # See detailed documentation here
      # https://keen.io/docs/api/reference/#event-collection-resource
      #
      # @param event_collection
      # @param [Hash] event properties
      #
      # @return a deferrable to apply callbacks to
      def publish_async(event_collection, properties)
        ensure_project_id!
        ensure_write_key!
        check_event_data!(event_collection, properties)


        http_client = Keen::HTTP::Async.new(self.api_url, self.proxy_url)
        http = http_client.post(
          :path => api_event_collection_resource_path(event_collection),
          :headers => api_headers(self.write_key, "async"),
          :body => MultiJson.encode(properties)
        )
        deferrable = EventMachine::DefaultDeferrable.new

        if defined?(EM::Synchrony)
          if http.error
            error = HttpError.new("HTTP em-synchrony publish_async error: #{http.error}")
            Keen.logger.error(error)
            raise error
          else
            process_response(http.response_header.status, http.response.chomp)
          end
        else
          http.callback {
            begin
              response = process_response(http.response_header.status, http.response.chomp)
            rescue Exception => e
              Keen.logger.error(e)
              deferrable.fail(e)
            end
            deferrable.succeed(response) if response
          }
          http.errback {
            error = Error.new("HTTP publish_async failure: #{http.error}")
            Keen.logger.error(error)
            deferrable.fail(error)
          }
          deferrable
        end
      end<commit_msg>Recover code<commit_after>module Keen
  class Client
    module PublishingMethods

      # @deprecated
      #
      # Publishes a synchronous event
      # @param event_collection
      # @param [Hash] event properties
      #
      # @return the JSON response from the API
      def add_event(event_collection, properties, options={})
        self.publish(event_collection, properties, options)
      end

      # Publishes a synchronous event
      # See detailed documentation here
      # https://keen.io/docs/api/reference/#event-collection-resource
      #
      # @param event_collection
      # @param [Hash] event properties
      #
      # @return the JSON response from the API
      def publish(event_collection, properties)
        ensure_project_id!
        ensure_write_key!
        check_event_data!(event_collection, properties)
        publish_body(
          api_event_collection_resource_path(event_collection),
          MultiJson.encode(properties),
          "publish")
      end

      # Publishes a batch of events
      # See detailed documentation here
      # https://keen.io/docs/api/reference/#post-request-body-example-of-batch-event-posting
      #
      # @param events - a hash where the keys are event collection names
      # and the values are arrays of hashes (event properties)
      #
      # @return the JSON response from the API
      def publish_batch(events)
        ensure_project_id!
        ensure_write_key!
        publish_body(
          api_events_resource_path,
          MultiJson.encode(events),
          "publish")
      end

      # Publishes an asynchronous event
      # See detailed documentation here
      # https://keen.io/docs/api/reference/#event-collection-resource
      #
      # @param event_collection
      # @param [Hash] event properties
      #
      # @return a deferrable to apply callbacks to
      def publish_async(event_collection, properties)
        ensure_project_id!
        ensure_write_key!
        check_event_data!(event_collection, properties)

        deferrable = EventMachine::DefaultDeferrable.new

        http_client = Keen::HTTP::Async.new(self.api_url, self.proxy_url)
        http = http_client.post(
          :path => api_event_collection_resource_path(event_collection),
          :headers => api_headers(self.write_key, "async"),
          :body => MultiJson.encode(properties)
        )

        if defined?(EM::Synchrony)
          if http.error
            error = HttpError.new("HTTP em-synchrony publish_async error: #{http.error}")
            Keen.logger.error(error)
            raise error
          else
            process_response(http.response_header.status, http.response.chomp)
          end
        else
          http.callback {
            begin
              response = process_response(http.response_header.status, http.response.chomp)
            rescue Exception => e
              Keen.logger.error(e)
              deferrable.fail(e)
            end
            deferrable.succeed(response) if response
          }
          http.errback {
            error = Error.new("HTTP publish_async failure: #{http.error}")
            Keen.logger.error(error)
            deferrable.fail(error)
          }
          deferrable
        end
      end
{'fraction_non_alphanumeric': 0.07982871998776571, 'fraction_numerical': 0.0, 'mean_word_length': 2.1202290076335877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 6, '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': '1350865', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1718, 'n_words': 396}