<commit_before>#!/usr/bin/env ruby

# See clippscript.md for documentation.

if RUBY_VERSION.split('.')[0..1].join('.').to_f < 1.9
  raise "Requires version 0.9 or higher."
end

module ClippScript
  CONNECTION_OPENED        = 1
  CONNECTION_DATA_IN       = 2
  CONNECTION_DATA_OUT      = 3
  CONNECTION_CLOSED        = 4
  REQUEST_STARTED          = 5
  REQUEST_HEADER           = 6
  REQUEST_HEADER_FINISHED  = 7
  REQUEST_BODY             = 8
  REQUEST_FINISHED         = 9
  RESPONSE_STARTED         = 10
  RESPONSE_HEADER          = 11
  RESPONSE_HEADER_FINISHED = 12
  RESPONSE_BODY            = 13
  RESPONSE_FINISHED        = 14

  def input(options = {})
    raise "Requires block." if ! block_given?
    options = symhash(options)

    result = {
      id: options[:id] || "ClippScript",
      connection: {
        pre_transaction_event:  [],
        transaction:            [],
        post_transaction_event: []
      }
    }

    yield InputProxy.new(result)

    if result[:connection][:pre_transaction_event].empty?
      result[:connection][:pre_transaction_event] = nil
    end
    if result[:connection][:post_transaction_event].empty?
      result[:connection][:post_transaction_event] = nil
    end

    result
  end

  def connection(options = {})
    options = symhash(options)

    connection_options = {
      local_ip:    options[:local_ip]    || "1.2.3.4",
      local_port:  options[:local_port]  || 80,
      remote_ip:   options[:remote_ip]   || "5.6.7.8",
      remote_port: options[:remote_port] || 1234
    }

    input(id: options[:id]) do |i|
      i.connection_opened(connection_options)
      yield i
      i.connection_closed
    end
  end

  def transaction(options = {}, &b)
    options = symhash(options)

    connection(
      move(options, :id, :local_ip, :local_port, :remote_ip, :remote_port)
    ) do |c|
      c.transaction(&b)
    end
  end

  def self.eval(what, &writer)
    results = []
    env = Environment.new do |x|
      if writer
        writer.(x)
      else
        results << x
      end
    end<commit_msg>clipp/clippscript.rb: Expose input, transaction, and connection as module methods, i.e., ClippScript::input(...)<commit_after>#!/usr/bin/env ruby

# See clippscript.md for documentation.

if RUBY_VERSION.split('.')[0..1].join('.').to_f < 1.9
  raise "Requires version 0.9 or higher."
end

module ClippScript
  CONNECTION_OPENED        = 1
  CONNECTION_DATA_IN       = 2
  CONNECTION_DATA_OUT      = 3
  CONNECTION_CLOSED        = 4
  REQUEST_STARTED          = 5
  REQUEST_HEADER           = 6
  REQUEST_HEADER_FINISHED  = 7
  REQUEST_BODY             = 8
  REQUEST_FINISHED         = 9
  RESPONSE_STARTED         = 10
  RESPONSE_HEADER          = 11
  RESPONSE_HEADER_FINISHED = 12
  RESPONSE_BODY            = 13
  RESPONSE_FINISHED        = 14

  def input(options = {})
    raise "Requires block." if ! block_given?
    options = symhash(options)

    result = {
      id: options[:id] || "ClippScript",
      connection: {
        pre_transaction_event:  [],
        transaction:            [],
        post_transaction_event: []
      }
    }

    yield InputProxy.new(result)

    if result[:connection][:pre_transaction_event].empty?
      result[:connection][:pre_transaction_event] = nil
    end
    if result[:connection][:post_transaction_event].empty?
      result[:connection][:post_transaction_event] = nil
    end

    result
  end

  def connection(options = {})
    options = symhash(options)

    connection_options = {
      local_ip:    options[:local_ip]    || "1.2.3.4",
      local_port:  options[:local_port]  || 80,
      remote_ip:   options[:remote_ip]   || "5.6.7.8",
      remote_port: options[:remote_port] || 1234
    }

    input(id: options[:id]) do |i|
      i.connection_opened(connection_options)
      yield i
      i.connection_closed
    end
  end

  def transaction(options = {}, &b)
    options = symhash(options)

    connection(
      move(options, :id, :local_ip, :local_port, :remote_ip, :remote_port)
    ) do |c|
      c.transaction(&b)
    end
  end

  # Add above as module methods as well.
  extend self

  def self.eval(what, &writer)
    results = []
    env = Environment.new do |x|
      if writer
        writer.(x)
      else
        results << x
      end
    end
{'fraction_non_alphanumeric': 0.11010030324236063, 'fraction_numerical': 0.01819454163750875, 'mean_word_length': 2.324031007751938, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '2807159', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1396, 'n_words': 292}