<commit_before>require 'rulesio/version'
require 'rulesio/helpers'
require 'rulesio/exceptions'
require 'rulesio/users'
require 'rulesio/girl_friday_queue'
require 'rulesio/memory_queue'
require 'net/http'
require 'uri'
require 'logger'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/hash/indifferent_access'

module RulesIO
  mattr_accessor :filter_parameters, :buffer, :token, :webhook_url, :queue, :queue_options, :controller_data, :logger, :disable_sending_events

  def self.default_ignored_crawlers
    %w(Baidu Gigabot Googlebot libwww-perl lwp-trivial msnbot SiteUptime Slurp WordPress ZIBB ZyBorg Yandex Jyxobot Huaweisymantecspider ApptusBot NewRelicPinger CopperEgg)
  end

  def self.send_event(event)
    buffer << event
  end

  def self.flush(env={})
    return if (events = RulesIO.buffer).empty?
    RulesIO.buffer = []
    RulesIO.queue.push(:payload => events.map {|event| RulesIO.prepare_event(event, env)})
    # RulesIO.post_payload_to_token events.to_json, RulesIO.token
  end
  
  def self.post_payload_to_token(payload, token)
    return if RulesIO.disable_sending_events
    uri = URI(RulesIO.webhook_url + token)
    req = Net::HTTP::Post.new(uri.path)
    req.body = payload.to_json
    req.content_type = 'application/json'
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if RulesIO.webhook_url =~ /^https:/
    http.start do |http|
      http.request(req)
    end
  end

  def self.current_user(env)
    if controller = env['action_controller.instance']
      controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
    end
  rescue
    nil
  end
  
  def self.current_actor(env)
    if controller = env['action_controller.instance']
      begin
        data = controller.instance_eval(RulesIO.controller_data)
        data = data.with_indifferent_access
        return data[:_actor] if data[:_actor]
      rescue
      end

      user = controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
      [:to_param, :id].each do |method|
        return user.send(method) if user && user.respond_to?(method)
      end
    end
    nil
  end

  private
  def self.page_event_name(request, params)
    if params && params['controller']
      "#{params['controller']}##{params['action']}"
    else
      request.path.gsub('/', '-')[1..-1]
    end
  end

  def self.prepare_event(event, env)
    event = event.with_indifferent_access

    if controller = env['action_controller.instance']
      begin
        data = controller.instance_eval(RulesIO.controller_data).with_indifferent_access
        event = data.merge(event)
      rescue
      end
    end

    current_user = current_user(env)
    actor = current_actor(env)

    event[:_actor] = actor || 'anonymous' unless event[:_actor].present?
    event[:_timestamp] ||= Time.now.to_f<commit_msg>Added rescue block to prevent exceptions from happening current_user is not defined.<commit_after>require 'rulesio/version'
require 'rulesio/helpers'
require 'rulesio/exceptions'
require 'rulesio/users'
require 'rulesio/girl_friday_queue'
require 'rulesio/memory_queue'
require 'net/http'
require 'uri'
require 'logger'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/hash/indifferent_access'

module RulesIO
  mattr_accessor :filter_parameters, :buffer, :token, :webhook_url, :queue, :queue_options, :controller_data, :logger, :disable_sending_events

  def self.default_ignored_crawlers
    %w(Baidu Gigabot Googlebot libwww-perl lwp-trivial msnbot SiteUptime Slurp WordPress ZIBB ZyBorg Yandex Jyxobot Huaweisymantecspider ApptusBot NewRelicPinger CopperEgg)
  end

  def self.send_event(event)
    buffer << event
  end

  def self.flush(env={})
    return if (events = RulesIO.buffer).empty?
    RulesIO.buffer = []
    RulesIO.queue.push(:payload => events.map {|event| RulesIO.prepare_event(event, env)})
    # RulesIO.post_payload_to_token events.to_json, RulesIO.token
  end
  
  def self.post_payload_to_token(payload, token)
    return if RulesIO.disable_sending_events
    uri = URI(RulesIO.webhook_url + token)
    req = Net::HTTP::Post.new(uri.path)
    req.body = payload.to_json
    req.content_type = 'application/json'
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if RulesIO.webhook_url =~ /^https:/
    http.start do |http|
      http.request(req)
    end
  end

  def self.current_user(env)
    if controller = env['action_controller.instance']
      controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
    end
  rescue
    nil
  end
  
  def self.current_actor(env)
    if controller = env['action_controller.instance']
      begin
        data = controller.instance_eval(RulesIO.controller_data)
        data = data.with_indifferent_access
        return data[:_actor] if data[:_actor]
      rescue
      end

      begin
        user = controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
        [:to_param, :id].each do |method|
          return user.send(method) if user && user.respond_to?(method)
        end
      rescue
      end
    end
    nil
  end

  private
  def self.page_event_name(request, params)
    if params && params['controller']
      "#{params['controller']}##{params['action']}"
    else
      request.path.gsub('/', '-')[1..-1]
    end
  end

  def self.prepare_event(event, env)
    event = event.with_indifferent_access

    if controller = env['action_controller.instance']
      begin
        data = controller.instance_eval(RulesIO.controller_data).with_indifferent_access
        event = data.merge(event)
      rescue
      end
    end

    current_user = current_user(env)
    actor = current_actor(env)

    event[:_actor] = actor || 'anonymous' unless event[:_actor].present?
    event[:_timestamp] ||= Time.now.to_f
{'fraction_non_alphanumeric': 0.10821272298889263, 'fraction_numerical': 0.0006731740154830024, 'mean_word_length': 3.792741935483871, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5850245', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1911, 'n_words': 387}