Class: Attentive::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/attentive/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, params = {}) ⇒ Message

Returns a new instance of Message.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/attentive/message.rb', line 8

def initialize(text, params={})
  raise ArgumentError, "Message cannot be initialized without 'text'" unless text
  @text = text
  @contexts = Set.new(params.fetch(:contexts, []))
  contexts << :conversation if tokens.grep(Attentive::Tokens::Invocation).any?
end

Instance Attribute Details

#contextsObject (readonly)

Returns the value of attribute contexts.



6
7
8
# File 'lib/attentive/message.rb', line 6

def contexts
  @contexts
end

#textObject (readonly) Also known as: to_s

Returns the value of attribute text.



6
7
8
# File 'lib/attentive/message.rb', line 6

def text
  @text
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
# File 'lib/attentive/message.rb', line 19

def [](key)
  tokens[key]
end

#[]=(key, value) ⇒ Object



23
24
25
# File 'lib/attentive/message.rb', line 23

def []=(key, value)
  tokens[key] = value
end

#inspectObject



33
34
35
# File 'lib/attentive/message.rb', line 33

def inspect
  tokens.inspect
end

#lengthObject



27
28
29
# File 'lib/attentive/message.rb', line 27

def length
  tokens.length
end

#tokensObject



15
16
17
# File 'lib/attentive/message.rb', line 15

def tokens
  @tokens ||= Attentive::Tokenizer.tokenize(text)
end