Class: DictClient::Client
- Inherits:
-
Object
- Object
- DictClient::Client
- Defined in:
- lib/dict_client/client.rb
Instance Method Summary collapse
- #banner ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #databases ⇒ Object
- #define(word, database = DB_ALL) ⇒ Object
- #disconnect ⇒ Object
- #help ⇒ Object
- #info(database) ⇒ Object
-
#initialize(host = DEFAULT_HOST, port = DEFAULT_PORT) ⇒ Client
constructor
A new instance of Client.
- #match(word, strategy = MATCH_DEFAULT, database = DB_ALL) ⇒ Object
- #server ⇒ Object
- #strategies ⇒ Object
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
#banner ⇒ Object
47 48 49 50 |
# File 'lib/dict_client/client.rb', line 47 def check_connection end |
#connect ⇒ Object
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 = @conn.readline unless DictClient.reply_code() == 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
10 11 12 |
# File 'lib/dict_client/client.rb', line 10 def connected? ! @conn.nil? end |
#databases ⇒ Object
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 |
#disconnect ⇒ Object
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 |
#help ⇒ Object
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 |
#server ⇒ Object
60 61 62 |
# File 'lib/dict_client/client.rb', line 60 def server request_response "show server", ServerInfoTcpReader.new, ServerInfo end |
#strategies ⇒ Object
56 57 58 |
# File 'lib/dict_client/client.rb', line 56 def strategies request_response "show strat", StrategiesTcpReader.new, Strategies end |