Class: Watson::Assistant::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/watson/assistant/manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Manager

Returns a new instance of Manager.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/watson/assistant/manager.rb', line 8

def initialize(config)
  storage = config[:storage] || "hash"
  @cnv = Dialog.new(
    config
  )

  if storage == "hash"
    @users = Hash.new
  else
    @users = Redis.new(:url => storage)
  end
end

Instance Method Details

#delete(user) ⇒ Object



32
33
34
# File 'lib/watson/assistant/manager.rb', line 32

def delete(user)
  @users.delete(user)
end

#has_key?(user) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/watson/assistant/manager.rb', line 27

def has_key?(user) 
  @users.has_key?(user)
end

#talk(user, question) ⇒ Object



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

def talk(user, question)
  future_data = nil

  if @users.has_key?(user) == false
    code, body = @cnv.talk("", "")
  else
    code, body = @cnv.talk(question, context = @users.fetch(user))
  end

  if code == 200
    context = body["context"]
    output = body["output"]["text"]
  else
    output = body["error"]
  end

  if code == 200
    @users.store(user, context)
  else
    @users.delete(user)
  end

  return {user: user, status_code: code, output: output}.to_json
end

#usersObject



22
23
24
# File 'lib/watson/assistant/manager.rb', line 22

def users()
  @users
end