Class: WitBot::Message
- Inherits:
-
Object
- Object
- WitBot::Message
- Defined in:
- lib/wit_bot/models/message.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#_outcomes ⇒ Object
readonly
Returns the value of attribute _outcomes.
-
#_text ⇒ Object
readonly
Returns the value of attribute _text.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#outcomes ⇒ Object
readonly
Returns the value of attribute outcomes.
-
#sent ⇒ Object
readonly
Returns the value of attribute sent.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #bot? ⇒ Boolean
- #from_hash(json) ⇒ Object
-
#initialize(thread = WitBot.thread, text, id: SecureRandom.uuid) ⇒ Message
constructor
A new instance of Message.
- #outcome ⇒ Object
- #params(p = nil) ⇒ Object
- #parse_outcomes(outcomes = @_outcomes) ⇒ Object
- #parse_outcomes!(outcomes = @_outcomes) ⇒ Object
- #send(n = 1, keep_context = true) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(thread = WitBot.thread, text, id: SecureRandom.uuid) ⇒ Message
Returns a new instance of Message.
7 8 9 10 11 12 13 14 |
# File 'lib/wit_bot/models/message.rb', line 7 def initialize(thread=WitBot.thread, text, id: SecureRandom.uuid) @thread = thread @text = text @_text = nil @id = id @sent = false @outcomes = @_outcomes = nil end |
Instance Attribute Details
#_outcomes ⇒ Object (readonly)
Returns the value of attribute _outcomes.
3 4 5 |
# File 'lib/wit_bot/models/message.rb', line 3 def _outcomes @_outcomes end |
#_text ⇒ Object (readonly)
Returns the value of attribute _text.
3 4 5 |
# File 'lib/wit_bot/models/message.rb', line 3 def _text @_text end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/wit_bot/models/message.rb', line 3 def id @id end |
#meta ⇒ Object
Returns the value of attribute meta.
5 6 7 |
# File 'lib/wit_bot/models/message.rb', line 5 def @meta end |
#outcomes ⇒ Object (readonly)
Returns the value of attribute outcomes.
3 4 5 |
# File 'lib/wit_bot/models/message.rb', line 3 def outcomes @outcomes end |
#sent ⇒ Object (readonly)
Returns the value of attribute sent.
3 4 5 |
# File 'lib/wit_bot/models/message.rb', line 3 def sent @sent end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
3 4 5 |
# File 'lib/wit_bot/models/message.rb', line 3 def text @text end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
3 4 5 |
# File 'lib/wit_bot/models/message.rb', line 3 def thread @thread end |
Class Method Details
.from_hash(thread, id, json) ⇒ Object
38 39 40 41 42 |
# File 'lib/wit_bot/models/message.rb', line 38 def self.from_hash(thread, id, json) json = json.with_indifferent_access return Bot::Message.from_hash thread, id, json if json[:bot] self.new(thread, json[:text], id: id).from_hash json end |
.many_from_hash(thread, json) ⇒ Object
43 44 45 |
# File 'lib/wit_bot/models/message.rb', line 43 def self.many_from_hash(thread, json) json.inject({}) { |h, (id, )| h[id] = self.from_hash thread, id, ; h } end |
Instance Method Details
#==(other) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/wit_bot/models/message.rb', line 83 def ==(other) self.text == other.text && self._text == other._text && self.id == other.id && self.bot? == other.bot? && self.sent == other.sent && self._outcomes == other._outcomes && self.thread.(other.thread) && self.outcomes == other.outcomes end |
#bot? ⇒ Boolean
16 17 18 |
# File 'lib/wit_bot/models/message.rb', line 16 def bot? false end |
#from_hash(json) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/wit_bot/models/message.rb', line 31 def from_hash(json) @_text = json[:_text] @sent = json[:sent] @_outcomes = json[:_outcomes] parse_outcomes! self end |
#outcome ⇒ Object
79 80 81 |
# File 'lib/wit_bot/models/message.rb', line 79 def outcome outcomes.first end |
#params(p = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/wit_bot/models/message.rb', line 47 def params(p=nil) params = { q: text, msg_id: id, thread_id: thread.id } params[:context] = JSON.dump thread.context.as_json unless thread.context.empty? p ? params.merge(p) : params end |
#parse_outcomes(outcomes = @_outcomes) ⇒ Object
71 72 73 |
# File 'lib/wit_bot/models/message.rb', line 71 def parse_outcomes(outcomes=@_outcomes) outcomes.each_with_index.map { |outcome, i| Outcome.new self, outcome, i } if outcomes end |
#parse_outcomes!(outcomes = @_outcomes) ⇒ Object
75 76 77 |
# File 'lib/wit_bot/models/message.rb', line 75 def parse_outcomes!(outcomes=@_outcomes) @outcomes = parse_outcomes outcomes end |
#send(n = 1, keep_context = true) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wit_bot/models/message.rb', line 57 def send(n=1, keep_context=true) response = MessageRequest.new.request(self, n) @sent = true thread.reset_context unless keep_context @_text = response['_text'] @_outcomes = response['outcomes'] parse_outcomes! self end |
#to_hash ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/wit_bot/models/message.rb', line 20 def to_hash { text: @text, _text: @_text, id: @id, bot: bot?, sent: @sent, _outcomes: @_outcomes } end |