Class: TimblClient
- Inherits:
-
Object
- Object
- TimblClient
- Defined in:
- lib/timbl_client.rb
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #classify(request) ⇒ Object (also: #tag)
-
#connect ⇒ Object
Open the socket.
- #disconnect ⇒ Object (also: #close)
- #format_request(request) ⇒ Object
-
#format_response(response) ⇒ Object
Turns the Timbl response into a more familiar Ruby-hash.
-
#initialize(host = "localhost", port = 6060) ⇒ TimblClient
constructor
A new instance of TimblClient.
Constructor Details
#initialize(host = "localhost", port = 6060) ⇒ TimblClient
Returns a new instance of TimblClient.
7 8 9 10 11 12 |
# File 'lib/timbl_client.rb', line 7 def initialize(host="localhost", port=6060) @host = host @port = port @socket = nil connect end |
Instance Attribute Details
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
5 6 7 |
# File 'lib/timbl_client.rb', line 5 def socket @socket end |
Instance Method Details
#classify(request) ⇒ Object Also known as: tag
20 21 22 23 24 |
# File 'lib/timbl_client.rb', line 20 def classify(request) #connect unless connected? @socket.send(format_request(request), 0) format_response(@socket.recv(1024)) end |
#connect ⇒ Object
Open the socket
15 16 17 18 |
# File 'lib/timbl_client.rb', line 15 def connect @socket = TCPSocket.open(@host, @port) @socket.gets # consumes the welcome msg end |
#disconnect ⇒ Object Also known as: close
41 42 43 44 |
# File 'lib/timbl_client.rb', line 41 def disconnect @socket.send("exit\n", 0) @socket.close end |
#format_request(request) ⇒ Object
27 28 29 30 |
# File 'lib/timbl_client.rb', line 27 def format_request(request) request = "c " + request unless request.start_with?("c ") request.chomp + "\n" end |
#format_response(response) ⇒ Object
Turns the Timbl response into a more familiar Ruby-hash. E.g. the response ‘CATEGORY 0n’ is converted to => “0”
34 35 36 37 38 39 |
# File 'lib/timbl_client.rb', line 34 def format_response(response) response.scan(/([A-Z]+) \{ ?(.*?) ?\}/).inject({}) do |res, e| res[e[0].downcase.to_sym] = e[1] res end end |