<commit_before>require 'crack/core_extensions' # Used for Hash#to_params
require 'hmac-sha2'
require 'multi_json'

module Pusher
  # Trigger events on Channels
  class Channel
    attr_reader :name

    def initialize(base_url, name)
      @uri = base_url.dup
      @uri.path = @uri.path + "/channels/#{name}/events"
      @name = name
    end

    # Trigger event asynchronously using EventMachine::HttpRequest
    #
    # @param (see #trigger!)
    # @return [EM::DefaultDeferrable]
    #   Attach a callback to be notified of success (with no parameters).
    #   Attach an errback to be notified of failure (with an error parameter
    #   which includes the HTTP status code returned)
    # @raise [LoadError] unless em-http-request gem is available
    # @raise [Pusher::Error] unless the eventmachine reactor is running. You
    #   probably want to run your application inside a server such as thin
    #
    def trigger_async(event_name, data, socket_id = nil, &block)
      unless defined?(EventMachine) && EventMachine.reactor_running?
        raise Error, "In order to use trigger_async you must be running inside an eventmachine loop"
      end
      require 'em-http' unless defined?(EventMachine::HttpRequest)
      
      @http_async ||= EventMachine::HttpRequest.new(@uri)

      request = Pusher::Request.new(@uri, event_name, data, socket_id)

      deferrable = EM::DefaultDeferrable.new
      
      http = @http_async.post({
        :query => request.query, :timeout => 5, :body => request.body,
        :head => {'Content-Type'=> 'application/json'}
      })
      http.callback {
        begin
          handle_response(http.response_header.status, http.response.chomp)
          deferrable.succeed
        rescue => e
          deferrable.fail(e)
        end
      }
      http.errback {
        Pusher.logger.debug("Network error connecting to pusher: #{http.inspect}")
        deferrable.fail(Error.new("Network error connecting to pusher"))
      }
      
      deferrable
    end

    # Trigger event
    #
    # @example
    #   begin
    #     Pusher['my-channel'].trigger!('an_event', {:some => 'data'})
    #   rescue Pusher::Error => e
    #     # Do something on error
    #   end
    #
    # @param data [Object] Event data to be triggered in javascript.
    #   Objects other than strings will be converted to JSON
    # @param socket_id Allows excluding a given socket_id from receiving the<commit_msg>Fixed trigger_async to work with em-http-request 1.0<commit_after>require 'crack/core_extensions' # Used for Hash#to_params
require 'hmac-sha2'
require 'multi_json'

module Pusher
  # Trigger events on Channels
  class Channel
    attr_reader :name

    def initialize(base_url, name)
      @uri = base_url.dup
      @uri.path = @uri.path + "/channels/#{name}/events"
      @name = name
    end

    # Trigger event asynchronously using EventMachine::HttpRequest
    #
    # @param (see #trigger!)
    # @return [EM::DefaultDeferrable]
    #   Attach a callback to be notified of success (with no parameters).
    #   Attach an errback to be notified of failure (with an error parameter
    #   which includes the HTTP status code returned)
    # @raise [LoadError] unless em-http-request gem is available
    # @raise [Pusher::Error] unless the eventmachine reactor is running. You
    #   probably want to run your application inside a server such as thin
    #
    def trigger_async(event_name, data, socket_id = nil, &block)
      unless defined?(EventMachine) && EventMachine.reactor_running?
        raise Error, "In order to use trigger_async you must be running inside an eventmachine loop"
      end
      require 'em-http' unless defined?(EventMachine::HttpRequest)

      request = Pusher::Request.new(@uri, event_name, data, socket_id)

      deferrable = EM::DefaultDeferrable.new
      
      http = EventMachine::HttpRequest.new(@uri).post({
        :query => request.query, :timeout => 5, :body => request.body,
        :head => {'Content-Type'=> 'application/json'}
      })
      http.callback {
        begin
          handle_response(http.response_header.status, http.response.chomp)
          deferrable.succeed
        rescue => e
          deferrable.fail(e)
        end
      }
      http.errback {
        Pusher.logger.debug("Network error connecting to pusher: #{http.inspect}")
        deferrable.fail(Error.new("Network error connecting to pusher"))
      }
      
      deferrable
    end

    # Trigger event
    #
    # @example
    #   begin
    #     Pusher['my-channel'].trigger!('an_event', {:some => 'data'})
    #   rescue Pusher::Error => e
    #     # Do something on error
    #   end
    #
    # @param data [Object] Event data to be triggered in javascript.
    #   Objects other than strings will be converted to JSON
    # @param socket_id Allows excluding a given socket_id from receiving the
{'fraction_non_alphanumeric': 0.0975809758097581, 'fraction_numerical': 0.0012300123001230013, 'mean_word_length': 2.73583460949464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6097325', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1409, 'n_words': 431}