Class: DictClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dict_client/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host = DEFAULT_HOST, port = DEFAULT_PORT) ⇒ Client

Returns a new instance of Client.



6
7
8
# File 'lib/dict_client/client.rb', line 6

def initialize(host = DEFAULT_HOST, port = DEFAULT_PORT)
  @host, @port = host, port
end

Instance Method Details



47
48
49
50
# File 'lib/dict_client/client.rb', line 47

def banner
  check_connection
  @banner
end

#connectObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/dict_client/client.rb', line 10

def connected?
  ! @conn.nil?
end

#databasesObject



52
53
54
# File 'lib/dict_client/client.rb', line 52

def databases
  request_response "show db", DictionariesTcpReader.new, Dictionaries
end

#define(word, database = DB_ALL) ⇒ Object



76
77
78
# File 'lib/dict_client/client.rb', line 76

def define(word, database = DB_ALL)
  request_response %!define #{database} "#{word}"!, WordDefinitionsTcpReader.new, WordDefinitions
end

#disconnectObject



39
40
41
42
43
44
45
# File 'lib/dict_client/client.rb', line 39

def disconnect
  if connected?
    send_command 'quit'
    @conn.close
    @conn = nil
  end
end

#helpObject



64
65
66
# File 'lib/dict_client/client.rb', line 64

def help
  request_response "help", ServerHelpTcpReader.new, ServerHelp
end

#info(database) ⇒ Object



68
69
70
# 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) ⇒ Object



72
73
74
# 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

#serverObject



60
61
62
# File 'lib/dict_client/client.rb', line 60

def server
  request_response "show server", ServerInfoTcpReader.new, ServerInfo
end

#strategiesObject



56
57
58
# File 'lib/dict_client/client.rb', line 56

def strategies
  request_response "show strat", StrategiesTcpReader.new, Strategies
end