<commit_before>require 'cgi'
require 'uri'
require 'eventmachine'

module Billy
  class Proxy
    attr_reader :cache

    def initialize
      reset
      @cache = Billy::Cache.new
    end

    def start(threaded = true)
      if threaded
        Thread.new { main_loop }
        sleep(0.01) while @signature.nil?
      else
        main_loop
      end
    end

    def url
      "http://#{host}:#{port}"
    end

    def host
      'localhost'
    end

    def port
      Socket.unpack_sockaddr_in(EM.get_sockname(@signature)).first
    end

    def call(method, url, headers, body)
      stub = find_stub(method, url)
      unless stub.nil?
        query_string = URI.parse(url).query || ""
        params = CGI.parse(query_string)
        stub.call(params, headers, body)
      end
    end

    def stub(url, options = {})
      ret = ProxyRequestStub.new(url, options)
      @stubs.unshift ret
      ret
    end

    def reset
      @stubs = []
    end

    def reset_cache
      @cache.reset
    end

    def restore_cache
      @cache.reset
      @cache.load_dir
    end

    protected

    def find_stub(method, url)
      @stubs.find {|stub| stub.matches?(method, url) }
    end

    def main_loop
      EM.run do
        EM.error_handler do |e|
          puts e.class.name, e
          puts e.backtrace.join("\n")
        end

        @signature = EM.start_server('127.0.0.1', 0, ProxyConnection) do |p|<commit_msg>Started work on naming caches<commit_after>require 'cgi'
require 'uri'
require 'eventmachine'

module Billy
  class Proxy
    attr_reader :cache

    def initialize
      reset
      @cache = Billy::Cache.new
    end

    def start(threaded = true)
      if threaded
        Thread.new { main_loop }
        sleep(0.01) while @signature.nil?
      else
        main_loop
      end
    end

    def url
      "http://#{host}:#{port}"
    end

    def host
      'localhost'
    end

    def port
      Socket.unpack_sockaddr_in(EM.get_sockname(@signature)).first
    end

    def call(method, url, headers, body)
      stub = find_stub(method, url)
      unless stub.nil?
        query_string = URI.parse(url).query || ""
        params = CGI.parse(query_string)
        stub.call(params, headers, body)
      end
    end

    def stub(url, options = {})
      ret = ProxyRequestStub.new(url, options)
      @stubs.unshift ret
      ret
    end

    def reset
      @stubs = []
    end

    def use_cache(name = nil)
      cache_index = @caches.index { |cache| cache.name == name }
      if cache_index.nil?
        @cache = Billy::Cache.new name
        @caches << @cache
      else
        @cache = @caches[cache_index]
      end
    end

    def reset_cache
      @cache.reset
    end

    def restore_cache
      @cache.reset
      @cache.load_dir
    end

    protected

    def find_stub(method, url)
      @stubs.find {|stub| stub.matches?(method, url) }
    end

    def main_loop
      EM.run do
        EM.error_handler do |e|
          puts e.class.name, e
          puts e.backtrace.join("\n")
        end

        @signature = EM.start_server('127.0.0.1', 0, ProxyConnection) do |p|
{'fraction_non_alphanumeric': 0.1010262989095574, 'fraction_numerical': 0.006414368184733804, 'mean_word_length': 1.904096834264432, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4467828', 'n_tokens_mistral': 1041, 'n_tokens_neox': 1019, 'n_words': 251}