<commit_before># require 'give_live_back_to_object/callbacks'
require 'eventmachine'
require 'faye'

module GiveLive

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods

    def faye_subscribe(channel, &block)
      wrapped_block = wrap(&block)
      client.subscribe channel, &wrapped_block
    end

    def faye_publish(channel, message)
      client.publish channel, message
    end

    def faye_render(attrs)
      raise "Error" if attrs[:template].blank? || attrs[:selector].blank?
      faye_publish("/give_live_back_to_object/render/", attrs)
    end

    private

    def wrap(&block)
      return lambda do |*args|
        begin
          yield(*args)
        rescue => e
          message = "[ERROR IN FAYE THREAD] #{e.message}"
          message << e.backtrace.join("\n")
          defined?(logger) ? logger.error(message) : puts(message)
        end
      end
    end

    def client
      @client ||= begin
                    Thread.new { EM.run } unless EM.reactor_running?
                    Thread.pass until EM.reactor_running?
                    Faye::Client.new('http://localhost:9292/faye')
                  end
    end
    
  end

end<commit_msg>fix hardcoded url<commit_after># require 'give_live_back_to_object/callbacks'
require 'eventmachine'
require 'faye'

module GiveLive

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods

    def faye_subscribe(channel, &block)
      wrapped_block = wrap(&block)
      client.subscribe channel, &wrapped_block
    end

    def faye_publish(channel, message)
      client.publish channel, message
    end

    def faye_render(attrs)
      raise "Error" if attrs[:template].blank? || attrs[:selector].blank?
      faye_publish("/give_live_back_to_object/render/", attrs)
    end

    private

    def wrap(&block)
      return lambda do |*args|
        begin
          yield(*args)
        rescue => e
          message = "[ERROR IN FAYE THREAD] #{e.message}"
          message << e.backtrace.join("\n")
          defined?(logger) ? logger.error(message) : puts(message)
        end
      end
    end

    def client
      @client ||= begin
                    Thread.new { EM.run } unless EM.reactor_running?
                    Thread.pass until EM.reactor_running?
                    Faye::Client.new(ENV['FAYE_URL'])
                  end
    end
    
  end

end
{'fraction_non_alphanumeric': 0.09787588504789671, 'fraction_numerical': 0.001665972511453561, 'mean_word_length': 2.1856763925729443, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '6589686', 'n_tokens_mistral': 729, 'n_tokens_neox': 706, 'n_words': 167}