class RecastAI::Conversation

Attributes

action[R]
conversation_token[R]
entities[R]
intents[R]
language[R]
memory[R]
next_actions[R]
processing_language[R]
raw[R]
replies[R]
sentiment[R]
source[R]
status[R]
timestamp[R]
uuid[R]
version[R]

Public Class Methods

new(response, token) click to toggle source
# 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

Public Instance Methods

get_memory(key = nil) click to toggle source
# 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
intent() click to toggle source
# File lib/recastai/apis/request/models/conversation.rb, line 60
def intent
  @intents.any? ? @intents.first : nil
end
joined_replies(sep = ' ') click to toggle source
# File lib/recastai/apis/request/models/conversation.rb, line 46
def joined_replies(sep = ' ')
  @replies.join(sep)
end
negative?() click to toggle source
# File lib/recastai/apis/request/models/conversation.rb, line 76
def negative?
  @sentiment == Utils::SENTIMENT_NEGATIVE
end
neutral?() click to toggle source
# File lib/recastai/apis/request/models/conversation.rb, line 72
def neutral?
  @sentiment == Utils::SENTIMENT_NEUTRAL
end
next_action() click to toggle source
# File lib/recastai/apis/request/models/conversation.rb, line 42
def next_action
  @next_actions.any? ? @next_actions.first : nil
end
positive?() click to toggle source
# File lib/recastai/apis/request/models/conversation.rb, line 68
def positive?
  @sentiment == Utils::SENTIMENT_POSITIVE
end
reply() click to toggle source
# File lib/recastai/apis/request/models/conversation.rb, line 38
def reply
  @replies.any? ? @replies.first : nil
end
reset_conversation() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/recastai/apis/request/models/conversation.rb, line 80
def vnegative?
  @sentiment == Utils::SENTIMENT_VNEGATIVE
end
vpositive?() click to toggle source
# File lib/recastai/apis/request/models/conversation.rb, line 64
def vpositive?
  @sentiment == Utils::SENTIMENT_VPOSITIVE
end