Class: Vangrail::ScoreProviders::Command

Inherits:
Transport
  • Object
show all
Defined in:
lib/vangrail/score_provider.rb

Overview

One JSON request on stdin and one JSON response on stdout.

Defined Under Namespace

Classes: OutputLimit

Constant Summary collapse

READ_CHUNK =
16 * 1024
TERMINATION_GRACE =
0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, timeout: DEFAULT_TIMEOUT, environment: {}, inherit_environment: false, **options) ⇒ Command

Returns a new instance of Command.

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
# File 'lib/vangrail/score_provider.rb', line 117

def initialize(command:, timeout: DEFAULT_TIMEOUT, environment: {},
               inherit_environment: false, **options)
  super(**options)
  @command = Array(command).map(&:to_s).freeze
  @timeout = positive_timeout(timeout)
  @environment = environment.to_h { |name, value| [name.to_s, value.to_s] }.freeze
  @inherit_environment = !!inherit_environment # rubocop:disable Style/DoubleNegation
  raise ArgumentError, 'command is required' if @command.empty? || @command.any?(&:empty?)
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



115
116
117
# File 'lib/vangrail/score_provider.rb', line 115

def command
  @command
end

#environmentObject (readonly)

Returns the value of attribute environment.



115
116
117
# File 'lib/vangrail/score_provider.rb', line 115

def environment
  @environment
end

#inherit_environmentObject (readonly)

Returns the value of attribute inherit_environment.



115
116
117
# File 'lib/vangrail/score_provider.rb', line 115

def inherit_environment
  @inherit_environment
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



115
116
117
# File 'lib/vangrail/score_provider.rb', line 115

def timeout
  @timeout
end

Instance Method Details

#score(text, side:, **context) ⇒ Object

Raises:



127
128
129
130
131
132
133
# File 'lib/vangrail/score_provider.rb', line 127

def score(text, side:, **context)
  _payload, input = request_payload(text, side, context)
  output, status = execute(input)
  raise ProtocolError, exit_reason(status) unless status.success?

  response_payload(parse(output))
end