Class: RecastAI::Conversation

Inherits:
Object
  • Object
show all
Defined in:
lib/recastai/apis/request/models/conversation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, token) ⇒ Conversation



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/recastai/apis/request/models/conversation.rb', line 13

def initialize(response, token)
  @token = token

  @raw = response

  response = JSON.parse(response)
  response = response['results']

  @uuid                = response['uuid']
  @source              = response['source']
  @replies             = response['replies']
  @action              = response['action'] ? Action.new(response['action']) : nil
  @next_actions        = response['next_actions'].map{ |i| Action.new(i) }
  @sentiment           = response['sentiment']
  @memory              = response['memory'].reject{ |_, e| e.nil? }.map{ |n, e| Entity.new(n, e) }
  @entities            = response['entities'].flat_map{ |n, e| e.map{ |ee| Entity.new(n, ee) } }
  @intents             = response['intents'].map{ |i| Intent.new(i) }
  @conversation_token  = response['conversation_token']
  @language            = response['language']
  @processing_language = response['processing_language']
  @version             = response['version']
  @timestamp           = response['timestamp']
  @status              = response['status']
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def action
  @action
end

#conversation_tokenObject (readonly)

Returns the value of attribute conversation_token.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def conversation_token
  @conversation_token
end

#entitiesObject (readonly)

Returns the value of attribute entities.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def entities
  @entities
end

#intentsObject (readonly)

Returns the value of attribute intents.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def intents
  @intents
end

#languageObject (readonly)

Returns the value of attribute language.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def language
  @language
end

#memoryObject (readonly)

Returns the value of attribute memory.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def memory
  @memory
end

#next_actionsObject (readonly)

Returns the value of attribute next_actions.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def next_actions
  @next_actions
end

#processing_languageObject (readonly)

Returns the value of attribute processing_language.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def processing_language
  @processing_language
end

#rawObject (readonly)

Returns the value of attribute raw.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def raw
  @raw
end

#repliesObject (readonly)

Returns the value of attribute replies.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def replies
  @replies
end

#sentimentObject (readonly)

Returns the value of attribute sentiment.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def sentiment
  @sentiment
end

#sourceObject (readonly)

Returns the value of attribute source.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def source
  @source
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def status
  @status
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def timestamp
  @timestamp
end

#uuidObject (readonly)

Returns the value of attribute uuid.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def uuid
  @uuid
end

#versionObject (readonly)

Returns the value of attribute version.



10
11
12
# File 'lib/recastai/apis/request/models/conversation.rb', line 10

def version
  @version
end

Instance Method Details

#get_memory(key = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/recastai/apis/request/models/conversation.rb', line 50

def get_memory(key = nil)
  return @memory if key.nil?

  @memory.each do |entity|
    return entity if entity.name.casecmp(key.to_s).zero?
  end

  nil
end

#intentObject



60
61
62
# File 'lib/recastai/apis/request/models/conversation.rb', line 60

def intent
  @intents.any? ? @intents.first : nil
end

#joined_replies(sep = ' ') ⇒ Object



46
47
48
# File 'lib/recastai/apis/request/models/conversation.rb', line 46

def joined_replies(sep = ' ')
  @replies.join(sep)
end

#negative?Boolean



76
77
78
# File 'lib/recastai/apis/request/models/conversation.rb', line 76

def negative?
  @sentiment == Utils::SENTIMENT_NEGATIVE
end

#neutral?Boolean



72
73
74
# File 'lib/recastai/apis/request/models/conversation.rb', line 72

def neutral?
  @sentiment == Utils::SENTIMENT_NEUTRAL
end

#next_actionObject



42
43
44
# File 'lib/recastai/apis/request/models/conversation.rb', line 42

def next_action
  @next_actions.any? ? @next_actions.first : nil
end

#positive?Boolean



68
69
70
# File 'lib/recastai/apis/request/models/conversation.rb', line 68

def positive?
  @sentiment == Utils::SENTIMENT_POSITIVE
end

#replyObject



38
39
40
# File 'lib/recastai/apis/request/models/conversation.rb', line 38

def reply
  @replies.any? ? @replies.first : nil
end

#reset_conversationObject

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/recastai/apis/request/models/conversation.rb', line 114

def reset_conversation
  body = { conversation_token: @conversation_token }
  response = HTTParty.delete(
    Utils::CONVERSE_ENDPOINT,
    body: body,
    headers: { 'Authorization' => "Token #{@token}" }
  )
  raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 200

  response = JSON.parse(response.body)
  response = response['results']
  response['memory'].reject{ |_, e| e.nil? }.map{ |n, e| Entity.new(n, e) }
end

#reset_memory(name = nil) ⇒ Object

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/recastai/apis/request/models/conversation.rb', line 98

def reset_memory(name = nil)
  body = { conversation_token: @conversation_token }
  body[:memory] = { name => nil } unless name.nil?

  response = HTTParty.put(
    Utils::CONVERSE_ENDPOINT,
    body: body,
    headers: { 'Authorization' => "Token #{@token}" }
  )
  raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 200

  response = JSON.parse(response.body)
  response = response['results']
  response['memory'].reject{ |_, e| e.nil? }.map{ |n, e| Entity.new(n, e) }
end

#set_memory(memory) ⇒ Object

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/recastai/apis/request/models/conversation.rb', line 84

def set_memory(memory)
  body = { conversation_token: @conversation_token, memory: memory }
  response = HTTParty.put(
    Utils::CONVERSE_ENDPOINT,
    body: body,
    headers: { 'Authorization' => "Token #{@token}" }
  )
  raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 200

  response = JSON.parse(response.body)
  response = response['results']
  response['memory'].reject{ |_, e| e.nil? }.map{ |n, e| Entity.new(n, e) }
end

#vnegative?Boolean



80
81
82
# File 'lib/recastai/apis/request/models/conversation.rb', line 80

def vnegative?
  @sentiment == Utils::SENTIMENT_VNEGATIVE
end

#vpositive?Boolean



64
65
66
# File 'lib/recastai/apis/request/models/conversation.rb', line 64

def vpositive?
  @sentiment == Utils::SENTIMENT_VPOSITIVE
end