Class: DICT
Constant Summary collapse
- DATABASES_PRESENT =
'110'
- STRATEGIES_AVAILABLE =
'111'
- DATABASE_INFORMATION =
'112'
- HELP_TEXT =
'113'
- SERVER_INFORMATION =
'114'
- CHALLENGE_FOLLOWS =
'130'
- DEFINITIONS_RETRIEVED =
'150'
- WORD_DEFINITION =
'151'
- MATCHES_PRESENT =
'152'
- STATUS_RESPONSE =
'210'
- CONNECTION_ESTABLISHED =
'220'
- CLOSING_CONNECTION =
'221'
- AUTHENTICATION_SUCCESSFUL =
'230'
- OK =
'250'
- SEND_RESPONSE =
'330'
- TEMPORARILY_UNAVAILABLE =
'420'
- SHUTTING_DOWN =
'421'
- UNRECOGNISED_COMMAND =
'500'
- ILLEGAL_PARAMETERS =
'501'
- COMMAND_NOT_IMPLEMENTED =
'502'
- PARAMETER_NOT_IMPLEMENTED =
'503'
- ACCESS_DENIED =
'530'
- AUTH_DENIED =
'531'
- UNKNOWN_MECHANISM =
'532'
- INVALID_DATABASE =
'550'
- INVALID_STRATEGY =
'551'
- NO_MATCH =
'552'
- NO_DATABASES_PRESENT =
'554'
- NO_STRATEGIES_AVAILABLE =
'555'
- ALL_DATABASES =
'*'
- DEFAULT_MATCH_STRATEGY =
'.'
- DEFAULT_PORT =
2628
- ERROR =
/^[45]/
- FIRST_DATABASE =
'!'
- MAX_LINE_LENGTH =
1024
- PAIR =
/^(\S+)\s"(.+)"\r$/
- REPLY_CODE =
/^\d\d\d/
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
readonly
Returns the value of attribute capabilities.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#msgid ⇒ Object
readonly
Returns the value of attribute msgid.
Instance Method Summary collapse
-
#auth(user, secret) ⇒ Object
AUTHorise user.
-
#client(info) ⇒ Object
send CLIENT information.
-
#define(db, word) ⇒ Object
DEFINE a word.
-
#disconnect ⇒ Object
QUIT from the server.
-
#help ⇒ Object
request server-side HELP.
-
#initialize(hosts, port = DEFAULT_PORT, debug = false, verbose = false) ⇒ DICT
constructor
A new instance of DICT.
-
#match(db, strategy, word) ⇒ Object
MATCH a word.
-
#show_db ⇒ Object
get database list.
-
#show_info(db) ⇒ Object
get information on database.
-
#show_server ⇒ Object
get server information.
-
#show_strat ⇒ Object
get strategy list.
-
#status ⇒ Object
request server STATUS information.
Constructor Details
#initialize(hosts, port = DEFAULT_PORT, debug = false, verbose = false) ⇒ DICT
Returns a new instance of DICT.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/dict.rb', line 242 def initialize(hosts, port = DEFAULT_PORT, debug = false, verbose = false) hosts.each do |host| @debug = debug @verbose = verbose printf("Attempting to connect to %s:%d...\n", host, port) if @verbose begin @sock = TCPSocket.open(host, port) rescue next # cycle through list of servers, if more than one end break # continue if connection to this host succeeded end # catch failure raise ConnectError, 'Unable to connect to host' unless defined? @sock # check status line on connect line = get_line raise ConnectError, line if line =~ ERROR caps, @msgid = /<(.+?)>\s(<.+>)/.match(line)[1..2] @capabilities = caps.split(/\./) if @verbose printf("Capabilities: %s\n", @capabilities.join(', ')) printf("Msgid: %s\n", @msgid) end end |
Instance Attribute Details
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
201 202 203 |
# File 'lib/dict.rb', line 201 def capabilities @capabilities end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
201 202 203 |
# File 'lib/dict.rb', line 201 def code @code end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
201 202 203 |
# File 'lib/dict.rb', line 201 def @message end |
#msgid ⇒ Object (readonly)
Returns the value of attribute msgid.
201 202 203 |
# File 'lib/dict.rb', line 201 def msgid @msgid end |
Instance Method Details
#auth(user, secret) ⇒ Object
AUTHorise user
433 434 435 436 |
# File 'lib/dict.rb', line 433 def auth(user, secret) auth = MD5::new(@msgid + secret).hexdigest exec_cmd('AUTH %s %s' % [ user, auth ]) end |
#client(info) ⇒ Object
send CLIENT information
427 428 429 |
# File 'lib/dict.rb', line 427 def client(info) exec_cmd('CLIENT %s' % info) end |
#define(db, word) ⇒ Object
DEFINE a word
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/dict.rb', line 347 def define(db, word) definitions = Array.new d = Definition.new d.word = word d.definition = Array.new exec_cmd('DEFINE %s "%s"' % [ db, word ]) return nil if @code =~ ERROR while line = get_line return definitions if line =~ /^#{OK}/ if line =~ /^#{WORD_DEFINITION}/ word, d.database, d.description = /^\d\d\d\s"(.+?)"\s(\S+)\s"(.+)"\r$/.match(line)[1..3] elsif end_of_text? line # finish definition and start a new one definitions << d d = Definition.new d.word = word d.definition = Array.new else line.undot! d.definition << line end end end |
#disconnect ⇒ Object
QUIT from the server
340 341 342 343 |
# File 'lib/dict.rb', line 340 def disconnect exec_cmd('QUIT') @sock.close end |
#help ⇒ Object
request server-side HELP
420 421 422 423 |
# File 'lib/dict.rb', line 420 def help exec_cmd('HELP') parse_response end |
#match(db, strategy, word) ⇒ Object
MATCH a word
378 379 380 381 |
# File 'lib/dict.rb', line 378 def match(db, strategy, word) exec_cmd('MATCH %s %s "%s"' % [ db, strategy, word ]) parse_response end |
#show_db ⇒ Object
get database list
385 386 387 388 |
# File 'lib/dict.rb', line 385 def show_db exec_cmd("SHOW DB") parse_response end |
#show_info(db) ⇒ Object
get information on database
399 400 401 402 |
# File 'lib/dict.rb', line 399 def show_info(db) exec_cmd('SHOW INFO %s' % db) parse_response end |
#show_server ⇒ Object
get server information
406 407 408 409 |
# File 'lib/dict.rb', line 406 def show_server exec_cmd("SHOW SERVER") parse_response end |
#show_strat ⇒ Object
get strategy list
392 393 394 395 |
# File 'lib/dict.rb', line 392 def show_strat exec_cmd("SHOW STRAT") parse_response end |
#status ⇒ Object
request server STATUS information
413 414 415 416 |
# File 'lib/dict.rb', line 413 def status exec_cmd('STATUS') @message end |