Class: WitBot::MessageThread

Inherits:
Object
  • Object
show all
Defined in:
lib/wit_bot/models/message_thread.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = SecureRandom.uuid) ⇒ MessageThread

Returns a new instance of MessageThread.



7
8
9
10
11
12
# File 'lib/wit_bot/models/message_thread.rb', line 7

def initialize(id=SecureRandom.uuid)
  @id = id
  @messages = ActiveSupport::OrderedHash.new
  @bot_messages = ActiveSupport::OrderedHash.new
  @metadata = nil
end

Instance Attribute Details

#bot_messagesObject (readonly)

Returns the value of attribute bot_messages.



5
6
7
# File 'lib/wit_bot/models/message_thread.rb', line 5

def bot_messages
  @bot_messages
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/wit_bot/models/message_thread.rb', line 5

def id
  @id
end

#messagesObject (readonly)

Returns the value of attribute messages.



5
6
7
# File 'lib/wit_bot/models/message_thread.rb', line 5

def messages
  @messages
end

#metadataObject

Returns the value of attribute metadata.



3
4
5
# File 'lib/wit_bot/models/message_thread.rb', line 3

def 
  @metadata
end

Class Method Details

.from_hash(json) ⇒ Object



62
63
64
65
# File 'lib/wit_bot/models/message_thread.rb', line 62

def self.from_hash(json)
  json = json.with_indifferent_access
  self.new(json[:id]).from_hash json
end

Instance Method Details

#==(other) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/wit_bot/models/message_thread.rb', line 67

def ==(other)
  self.id == other.id &&
      self.context == other.context &&
      self.bot_messages == other.bot_messages &&
      self.messages == other.messages &&
      self. == other.
end

#contextObject



32
33
34
# File 'lib/wit_bot/models/message_thread.rb', line 32

def context
  @context ||= Context.new
end

#create_bot_message(text, id = SecureRandom.uuid) ⇒ Object



28
29
30
# File 'lib/wit_bot/models/message_thread.rb', line 28

def create_bot_message(text, id=SecureRandom.uuid)
  @bot_messages[id] = WitBot::Bot::Message.new self, text, id: id
end

#create_message(text, id = SecureRandom.uuid) ⇒ Object



24
25
26
# File 'lib/wit_bot/models/message_thread.rb', line 24

def create_message(text, id=SecureRandom.uuid)
  @messages[id] = WitBot::Message.new self, text, id: id
end

#equals_without_messages(other) ⇒ Object



75
76
77
78
79
# File 'lib/wit_bot/models/message_thread.rb', line 75

def equals_without_messages(other)
  self.id == other.id &&
      self.context == other.context &&
      self. == other.
end

#from_hash(json) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/wit_bot/models/message_thread.rb', line 52

def from_hash(json)
  m = json[:messages]
  @messages = WitBot::Message.many_from_hash(self, m[:user])
  @bot_messages = WitBot::Message.many_from_hash(self, m[:bot])

  @context = Context.from_hash json[:context] if json[:context]
  @metadata = json[:metadata]
  self
end

#message(id) ⇒ Object



14
15
16
# File 'lib/wit_bot/models/message_thread.rb', line 14

def message(id)
  messages[id]
end

#messages_list(user: true, bot: false) ⇒ Object



18
19
20
21
22
# File 'lib/wit_bot/models/message_thread.rb', line 18

def messages_list(user: true, bot: false)
  bot_messages = bot ? @bot_messages.values : []
  user_messages = user ? @messages.values : []
  user_messages + bot_messages
end

#reset_contextObject



36
37
38
# File 'lib/wit_bot/models/message_thread.rb', line 36

def reset_context
  @context = Context.new
end

#to_hashObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wit_bot/models/message_thread.rb', line 40

def to_hash
  {
    id: @id,
    context: @context,
    messages: {
      bot: @bot_messages,
      user: @messages
    },
    metadata: @metadata
  }
end