<commit_before>require 'yaml'

module Beaneater
  class Connection
    attr_accessor :telnet_connection

    DEFAULT_PORT = 11300

    # @beaneater_connection = Beaneater::Connection.new(['localhost:11300'])
    def initialize(host)
      @telnet_connection = connect(host)
    end

    # transmit("stats", :match => /\n/) { |r| puts r }
    def transmit(command, options={}, &block)
      options.merge!("String" => command)
      parse_response(telnet_connection.cmd(options, &block))
    end

    protected

    # Init telnet
    # connect('localhost:3005')
    def connect(host)
      host, port = host.split(':')
      Net::Telnet.new('Host' => host, "Port" => (port || DEFAULT_PORT).to_i, "Prompt" => /\n/)
    end

    # Return => ["OK 456", "Body"]
    def parse_response(res)
      res_lines = res.split(/\r?\n/)
      status = res_lines.first
      status, id = status.scan(/\w+/)
      response = { :status => status, :body => YAML.load(res_lines[1..-1].join("\n")) }
      response[:id] = id if id
      response[:connection] = self
      response
    end
  end
end<commit_msg>[connection] add host and port instance variable, and override #to_s and #inspect<commit_after>require 'yaml'

module Beaneater
  class Connection
    attr_reader :telnet_connection, :host, :port

    DEFAULT_PORT = 11300

    # @beaneater_connection = Beaneater::Connection.new(['localhost:11300'])
    def initialize(address)
      @telnet_connection = connect(address)
    end

    # transmit("stats", :match => /\n/) { |r| puts r }
    def transmit(command, options={}, &block)
      options.merge!("String" => command)
      parse_response(telnet_connection.cmd(options, &block))
    end

    def to_s
      "#<Beaneater::Connection host=#{host.inspect} port=#{port.inspect}>"
    end

    def inspect
      "#<Beaneater::Connection host=#{host.inspect} port=#{port.inspect}>"
    end

    protected

    # Init telnet
    # connect('localhost:3005')
    def connect(address)
      @match = address.split(':')
      @host, @port = @match[0], Integer(@match[1] || DEFAULT_PORT)
      Net::Telnet.new('Host' => @host, "Port" => @port, "Prompt" => /\n/)
    end

    # Return => ["OK 456", "Body"]
    def parse_response(res)
      res_lines = res.split(/\r?\n/)
      status = res_lines.first
      status, id = status.scan(/\w+/)
      response = { :status => status, :body => YAML.load(res_lines[1..-1].join("\n")) }
      response[:id] = id if id
      response[:connection] = self
      response
    end
  end
end
{'fraction_non_alphanumeric': 0.159824631327222, 'fraction_numerical': 0.015942606616181746, 'mean_word_length': 3.003189792663477, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '5771812', 'n_tokens_mistral': 861, 'n_tokens_neox': 811, 'n_words': 181}