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
# File 'lib/watson/assistant/manager.rb', line 8

def initialize(config)
  @config = config
  check = check_config()

  if check == true
    create_dialog()
    prepare_storage()
  end

end

Instance Method Details

#check_configObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/watson/assistant/manager.rb', line 36

def check_config()
  if @config[:apikey]
    if @config[:username] || @config[:password]
      puts "Error: 'Both API key' and 'Username/Password' are used"
      return false
    end
    @config[:auth] = "apikey:#{@config[:apikey]}"

  elsif @config[:username] && @config[:password]
    @config[:auth] = "#{@config[:username]}:#{@config[:password]}"

  else
    puts "Error: Not authorized"
    return false
  end

  return true
end

#create_dialogObject



19
20
21
22
23
# File 'lib/watson/assistant/manager.rb', line 19

def create_dialog
  @dlg = Dialog.new(
    @config
  )
end

#delete(user) ⇒ Object



66
67
68
# File 'lib/watson/assistant/manager.rb', line 66

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

#delete_context_section(user:, key:) ⇒ Object



116
117
118
119
120
121
# File 'lib/watson/assistant/manager.rb', line 116

def delete_context_section(user:, key:)
  context = @users.fetch(user)
  context.delete(key)
  @users.store(user, context)
				return context
end

#has_dlg?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/watson/assistant/manager.rb', line 56

def has_dlg?
  return @dlg
end

#has_key?(user) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/watson/assistant/manager.rb', line 61

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

#prepare_storageObject



26
27
28
29
30
31
32
33
# File 'lib/watson/assistant/manager.rb', line 26

def prepare_storage
  storage = @config[:storage] || "hash"
  if storage == "hash"
    @users = Hash.new
  else
    @users = Redis.new(:url => storage)
  end
end

#read_context(user:) ⇒ Object



97
98
99
# File 'lib/watson/assistant/manager.rb', line 97

def read_context(user:)
  context = @users.fetch(user)
end

#read_context_section(user:, key:) ⇒ Object



102
103
104
105
# File 'lib/watson/assistant/manager.rb', line 102

def read_context_section(user:, key:)
  context = @users.fetch(user)
  return context[key]
end

#talk(user, question) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/watson/assistant/manager.rb', line 71

def talk(user, question)
  future_data = nil

  if @users.has_key?(user) == false
    code, body = @dlg.talk("", "")
  else
    code, body = @dlg.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

#update_context_section(user:, key:, value:) ⇒ Object



108
109
110
111
112
113
# File 'lib/watson/assistant/manager.rb', line 108

def update_context_section(user:, key:, value:)
  context = @users.fetch(user)
  context[key] = value
  @users.store(user, context)
				return context
end