class DictClient::Client

Public Class Methods

new(host = DEFAULT_HOST, port = DEFAULT_PORT) click to toggle source
# File lib/dict_client/client.rb, line 6
def initialize(host = DEFAULT_HOST, port = DEFAULT_PORT)
  @host, @port = host, port
end

Public Instance Methods

banner() click to toggle source
connect() { |self| ... } click to toggle source
# File lib/dict_client/client.rb, line 14
def connect

  @conn = tcp_open @host, @port

  @banner = @conn.readline

  unless DictClient.reply_code(@banner) == RESPONSE_CONNECTED
    raise DictError.new, "Connection refused \"#{@banner}\"."
  end

  # announce ourselves to the server.
  send_command CLIENT_NAME

  unless DictClient.reply_code(reply = @conn.readline()) == RESPONSE_OK
    raise DictError.new, "Client announcement failed \"#{reply}\""
  end

  if block_given?
    yield self
  else
    self
  end

end
connected?() click to toggle source
# File lib/dict_client/client.rb, line 10
def connected?
  ! @conn.nil?
end
databases() click to toggle source
# File lib/dict_client/client.rb, line 52
def databases
  request_response "show db", DictionariesTcpReader.new, Dictionaries
end
define(word, database = DB_ALL) click to toggle source
# File lib/dict_client/client.rb, line 76
def define(word, database = DB_ALL)
  request_response %!define #{database} "#{word}"!, WordDefinitionsTcpReader.new, WordDefinitions
end
disconnect() click to toggle source
# File lib/dict_client/client.rb, line 39
def disconnect
  if connected?
    send_command 'quit'
    @conn.close
    @conn = nil
  end
end
help() click to toggle source
# File lib/dict_client/client.rb, line 64
def help
  request_response "help", ServerHelpTcpReader.new, ServerHelp
end
info(database) click to toggle source
# File lib/dict_client/client.rb, line 68
def info database
  request_response %!show info "#{database}"!, DictionaryInfoTcpReader.new, DictionaryInfo
end
match(word, strategy = MATCH_DEFAULT, database = DB_ALL) click to toggle source
# File lib/dict_client/client.rb, line 72
def match(word, strategy = MATCH_DEFAULT, database = DB_ALL)
  request_response %!match #{database} #{strategy} "#{word}"!, MatchTcpReader.new, WordMatch
end
server() click to toggle source
# File lib/dict_client/client.rb, line 60
def server
  request_response "show server", ServerInfoTcpReader.new, ServerInfo
end
strategies() click to toggle source
# File lib/dict_client/client.rb, line 56
def strategies
  request_response "show strat", StrategiesTcpReader.new, Strategies
end

Private Instance Methods

check_connection() click to toggle source
# File lib/dict_client/client.rb, line 101
def check_connection
  unless connected?
    raise DictError.new, 'Not connected.'
  end
end
request_response(command, reader, response_class) click to toggle source
# File lib/dict_client/client.rb, line 87
def request_response(command, reader, response_class)

  send_command command

  if DictClient.reply_code(reply = @conn.readline) == reader.good_response_code
    response_class.new(reader.read_from(@conn))
  elsif reader.bad_response_code && DictClient.reply_code(reply) == reader.bad_response_code
    EmptyResponse.new
  else
    raise DictError.new, reply
  end
end
send_command(command) click to toggle source
# File lib/dict_client/client.rb, line 107
def send_command command
  check_connection
  # STDERR.puts command
  @conn.write command + EOL
end
tcp_open(host, port) click to toggle source
# File lib/dict_client/client.rb, line 83
def tcp_open host, port
  TCPSocket.open(host, port)
end