Class: Charyf::Engine::Dispatcher::BaseDispatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/charyf/engine/dispatcher/default.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

base

Methods included from Strategy

included

Class Method Details

.setupObject



10
11
12
# File 'lib/charyf/engine/dispatcher/default.rb', line 10

def self.setup

end

Instance Method Details

#dispatch(request) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/charyf/engine/dispatcher/default.rb', line 14

def dispatch(request)
  # Find if session exist for this request

  context = Charyf::Engine::Context.new
  context.request = request

  # TODO process session as well
  context.session = session_processor.get.process(request)

  # Get intents
  intents = intent_processors.collect do |processor_klass|
    processor = processor_klass.get_for(context.session ? context.session.skill : nil)

    processor.determine(
        request
    )
  end.collect do |intent|
    [intent, intent.alternatives].flatten
  end.flatten.sort_by do |intent|
    intent.confidence
  end.reverse

  # Sort by confidence
  best_match = intents.shift

  # Return best match with alternatives
  if best_match
    context.intent = Charyf::Engine::Intent.new(
        best_match.skill,
        best_match.controller,
        best_match.action,
        best_match.confidence,
        best_match.matches,
        intents
    )

    context.intent = best_match
  end

  # TODO
  spawn_controller(context)
end