Class: Jambots::Conversation

Inherits:
Object
  • Object
show all
Defined in:
lib/jambots/conversation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Conversation

Returns a new instance of Conversation.



7
8
9
10
11
12
# File 'lib/jambots/conversation.rb', line 7

def initialize(file_path)
  @file_path = file_path
  @file_name = File.basename(file_path)
  @key = File.basename(file_name, File.extname(file_name))
  @messages = load_messages
end

Instance Attribute Details

#file_nameObject

Returns the value of attribute file_name.



5
6
7
# File 'lib/jambots/conversation.rb', line 5

def file_name
  @file_name
end

#file_pathObject

Returns the value of attribute file_path.



5
6
7
# File 'lib/jambots/conversation.rb', line 5

def file_path
  @file_path
end

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/jambots/conversation.rb', line 5

def key
  @key
end

#messagesObject

Returns the value of attribute messages.



5
6
7
# File 'lib/jambots/conversation.rb', line 5

def messages
  @messages
end

Instance Method Details

#add_message(role, content) ⇒ Object



14
15
16
# File 'lib/jambots/conversation.rb', line 14

def add_message(role, content)
  @messages << {role: role, content: content}
end

#deleteObject



31
32
33
# File 'lib/jambots/conversation.rb', line 31

def delete
  File.delete(@file_path) if File.exist?(@file_path)
end

#load_messagesObject



23
24
25
26
27
28
29
# File 'lib/jambots/conversation.rb', line 23

def load_messages
  return [] unless File.exist?(@file_path)

  file_content = File.read(@file_path)

  YAML.safe_load(file_content, permitted_classes: [Symbol], symbolize_names: true)
end

#saveObject



18
19
20
21
# File 'lib/jambots/conversation.rb', line 18

def save
  messages_transformed = @messages.map { |message| message.transform_keys(&:to_s) }
  File.write(@file_path, messages_transformed.to_yaml)
end