Class: Baleen::Client

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/baleen/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 12345) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
# File 'lib/baleen/client.rb', line 9

def initialize(host, port=12345)
  @socket = TCPSocket.open(host, port)
  @result = "running"
  async.response
end

Instance Method Details

#closeObject



23
24
25
26
27
28
# File 'lib/baleen/client.rb', line 23

def close
  @socket.close if @socket
  info "connection closed"

rescue IOError; nil
end

#handle_response(response) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/baleen/client.rb', line 30

def handle_response(response)
  if response.nil?
    raise RuntimeError, 'Connection closed by server'
  end

  info "Got response"
  @result = Baleen::Message::Decoder.new(response).decode
  true
end

#request(request) ⇒ Object



15
16
17
# File 'lib/baleen/client.rb', line 15

def request(request)
  @socket.puts(request.to_json)
end

#responseObject



19
20
21
# File 'lib/baleen/client.rb', line 19

def response
  loop { break if handle_response(@socket.gets) }
end

#resultObject



40
41
42
# File 'lib/baleen/client.rb', line 40

def result
  @result
end