Class: Polytalk::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/polytalk/client.rb', line 7

def initialize(config = {})
  @port = config['port'] || 9090
  @host = config['host'] || '127.0.0.1'
end

Instance Method Details

#call(request) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/polytalk/client.rb', line 12

def call(request)
  client = TCPSocket.new(@host, @port)
  client.print request.to_json
  response = client.recv(2048)
  client.close

  if response.kind_of?(String) && (response.match(/^{/) || response.match(/^\[{/))
    response = JSON.parse(response)
  end
      
  if block_given?
    yield(response)
  else
    return response
  end

end