<commit_before>require 'socket'
require 'capybara/util/timeout'
require 'json'

class Capybara::Driver::Webkit
  class Browser
    def initialize(options = {})
      @socket_class = options[:socket_class] || TCPSocket
      start_server
      connect
    end

    def visit(url)
      command "Visit", url
    end

    def find(query)
      command("Find", query).split(",")
    end

    def reset!
      command("Reset")
    end

    def source
      command("Source")
    end

    def url
      command("Url")
    end

    def frame_focus(frame_id_or_index=nil)
      if frame_id_or_index.is_a? Fixnum
        command("FrameFocus", "", frame_id_or_index.to_s)
      elsif frame_id_or_index
        command("FrameFocus", frame_id_or_index)
      else
        command("FrameFocus")
      end
    end

    def command(name, *args)
      @socket.puts name
      @socket.puts args.size
      args.each do |arg|
        @socket.puts arg.to_s.bytesize
        @socket.print arg
      end
      check
      read_response
    end

    def evaluate_script(script)
      json = command('Evaluate', script)
      JSON.parse("[#{json}]").first
    end

    def execute_script(script)
      command('Execute', script)
    end

    private

    def start_server
      server_path = File.expand_path("../../../../../bin/webkit_server", __FILE__)
      @pid = fork { exec(server_path) }
      at_exit { Process.kill("INT", @pid) }
    end

    def connect
      Capybara.timeout(5) do
        attempt_connect
        !@socket.nil?
      end
    end

    def attempt_connect
      @socket = @socket_class.open("localhost", 9200)
    rescue Errno::ECONNREFUSED
    end

    def check
      result = @socket.gets.strip

      unless result == 'ok'
        raise WebkitError, read_response
      end
    end

    def read_response
      response_length = @socket.gets.to_i
      response = @socket.read(response_length)
      response.force_encoding("UTF-8")
      response
    end
  end
end<commit_msg>Fix for Ruby 1.8.x<commit_after>require 'socket'
require 'capybara/util/timeout'
require 'json'

class Capybara::Driver::Webkit
  class Browser
    def initialize(options = {})
      @socket_class = options[:socket_class] || TCPSocket
      start_server
      connect
    end

    def visit(url)
      command "Visit", url
    end

    def find(query)
      command("Find", query).split(",")
    end

    def reset!
      command("Reset")
    end

    def source
      command("Source")
    end

    def url
      command("Url")
    end

    def frame_focus(frame_id_or_index=nil)
      if frame_id_or_index.is_a? Fixnum
        command("FrameFocus", "", frame_id_or_index.to_s)
      elsif frame_id_or_index
        command("FrameFocus", frame_id_or_index)
      else
        command("FrameFocus")
      end
    end

    def command(name, *args)
      @socket.puts name
      @socket.puts args.size
      args.each do |arg|
        @socket.puts arg.to_s.bytesize
        @socket.print arg
      end
      check
      read_response
    end

    def evaluate_script(script)
      json = command('Evaluate', script)
      JSON.parse("[#{json}]").first
    end

    def execute_script(script)
      command('Execute', script)
    end

    private

    def start_server
      server_path = File.expand_path("../../../../../bin/webkit_server", __FILE__)
      @pid = fork { exec(server_path) }
      at_exit { Process.kill("INT", @pid) }
    end

    def connect
      Capybara.timeout(5) do
        attempt_connect
        !@socket.nil?
      end
    end

    def attempt_connect
      @socket = @socket_class.open("localhost", 9200)
    rescue Errno::ECONNREFUSED
    end

    def check
      result = @socket.gets.strip

      unless result == 'ok'
        raise WebkitError, read_response
      end
    end

    def read_response
      response_length = @socket.gets.to_i
      response = @socket.read(response_length)
      response.force_encoding("UTF-8") if response.respond_to?(:force_encoding)
      response
    end
  end
end
{'fraction_non_alphanumeric': 0.1038863976083707, 'fraction_numerical': 0.003487792725460887, 'mean_word_length': 2.245755860953921, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '958852', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1249, 'n_words': 280}