<commit_before>require 'socket'
require 'json'

module MpvCtl
  class Socket
    class Error < StandardError
    end

    attr_reader :socket

    def initialize(path)
      @socket = UNIXSocket.new(path)
    end

    def command(*args)
      payload = JSON.dump({"command" => args})
      MpvCtl.logger.debug payload
      socket.puts(payload)
      response = socket.readline
      MpvCtl.logger.debug response
      parse_response(response)
    end

    def parse_response(json)
      response = JSON.parse(json)
      if response['error'] == 'success'
        response['data']
      else
        raise Error, response['error']
      end
    end

    def watch
      loop do
        line = socket.readline
        yield JSON.parse(line)
      end
    end

    def close
      @socket.close
    end
  end
end<commit_msg>Preface logging with SEND or RECV<commit_after>require 'socket'
require 'json'

module MpvCtl
  class Socket
    class Error < StandardError
    end

    attr_reader :socket

    def initialize(path)
      @socket = UNIXSocket.new(path)
    end

    def command(*args)
      payload = JSON.dump({"command" => args})

      send(payload)
      response = recv

      parse_response(response)
    end

    def watch
      loop do
        yield JSON.parse(recv)
      end
    end

    def close
      @socket.close
    end

    private
    def send(line)
      MpvCtl.logger.debug "SEND: #{line}"
      socket.puts(line)
    end

    def recv
      line = socket.readline.strip
      MpvCtl.logger.debug "RECV: #{line}"
      line
    end

    def parse_response(json)
      response = JSON.parse(json)
      if response['error'] == 'success'
        response['data']
      else
        raise Error, response['error']
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.08838526912181303, 'fraction_numerical': 0.0, 'mean_word_length': 2.076655052264808, 'pattern_counts': {'":': 0, '<': 5, '<?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': '878176', 'n_tokens_mistral': 554, 'n_tokens_neox': 536, 'n_words': 141}