Class: Vangrail::Front

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

Overview

One JSON envelope for check, screen, and assess, used by the CLI and the HTTP front. Foreign callers speak this, not MRI objects.

Constant Summary collapse

COMMANDS =
%w[check_input check_output check_context screen assess].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine:) ⇒ Front

Returns a new instance of Front.



15
16
17
# File 'lib/vangrail/front.rb', line 15

def initialize(engine:)
  @engine = engine
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



13
14
15
# File 'lib/vangrail/front.rb', line 13

def engine
  @engine
end

Class Method Details

.result_payload(result) ⇒ Object



69
70
71
72
73
# File 'lib/vangrail/front.rb', line 69

def self.result_payload(result)
  body = result.to_h
  body['content'] = result.content unless result.content.nil?
  body
end

Instance Method Details

#assess(payload) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
# File 'lib/vangrail/front.rb', line 58

def assess(payload)
  prior = payload['prior']
  raise ArgumentError, 'prior is required' if prior.nil?
  raise ArgumentError, 'prior must be a number' unless numeric?(prior)

  extra = context_of(payload)
  extra.delete(:prior)
  side = (payload['side'] || 'input').to_sym
  engine.assess(required(payload, 'text'), side: side, prior: prior.to_f, **extra).to_h
end

#check_context(payload) ⇒ Object



39
40
41
# File 'lib/vangrail/front.rb', line 39

def check_context(payload)
  result_payload(engine.check_context(required(payload, 'text'), **context_of(payload)))
end

#check_input(payload) ⇒ Object



26
27
28
# File 'lib/vangrail/front.rb', line 26

def check_input(payload)
  result_payload(engine.check_input(required(payload, 'text'), context_of(payload)))
end

#check_output(payload) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/vangrail/front.rb', line 30

def check_output(payload)
  result_payload(engine.check_output(
                   required(payload, 'text'),
                   user_input: payload['user_input'],
                   passages: payload['passages'],
                   **context_of(payload)
                 ))
end

#dispatch(command, payload = {}) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
# File 'lib/vangrail/front.rb', line 19

def dispatch(command, payload = {})
  name = command.to_s.tr('-', '_')
  raise ArgumentError, "unknown command #{command}" unless COMMANDS.include?(name)

  send(name, stringify(payload))
end

#screen(payload) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vangrail/front.rb', line 43

def screen(payload)
  documents = payload['documents']
  raise ArgumentError, 'documents is required' unless documents.is_a?(Array)

  screening = engine.screen(documents, **context_of(payload))
  {
    'kept' => screening.kept,
    'rejected' => screening.rejected.map do |entry|
      { 'document' => entry[:document], 'result' => result_payload(entry[:result]) }
    end,
    'certain' => screening.certain?,
    'reason' => screening.reason,
  }.compact
end