Class: FreelingClient::Client

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

Instance Attribute Summary

Attributes inherited from Base

#config, #ident, #port, #server

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Client

Initializes the client

Example:

>> client = FreelingClient::Client.new

Arguments:

server: (String)
port: (String)
timeout: (Integer)


19
20
21
22
23
# File 'lib/freeling_client/client.rb', line 19

def initialize(opt = {})
  @server = opt.fetch(:server, 'localhost')
  @port = opt.fetch(:port, 50005)
  @timeout = opt.fetch(:timeout, 120)
end

Instance Method Details

#call(text) ⇒ Object

Calls the server with a given text

Example:

>> client = FreelingClient::Client.new
>> client.call("Este texto está en español.")

Arguments:

text: (String)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/freeling_client/client.rb', line 35

def call(text)
  output = []
  file = Tempfile.new('foo', encoding: 'utf-8')

  begin
    file.write(text)
    file.close
    stdin, stdout, stderr = Open3.popen3(command(file.path))

    Timeout::timeout(@timeout) {
      until (line = stdout.gets).nil?
        output << line.chomp
      end

      message = stderr.readlines
      unless message.empty?
        raise ExtractionError, message.join("\n")
      end
    }
  rescue Timeout::Error
    raise ExtractionError, "Timeout"
  ensure
    file.close
    file.unlink
  end
  output
end