class MosEisley::S3PO::Message

Attributes

event[R]

Public Class Methods

new(e) click to toggle source
# File lib/s3po/message.rb, line 6
def initialize(e)
  @event = e
end

Public Instance Methods

add_text(t) click to toggle source

Adds text after a space if text already exists @param t [String] text to add @return [Object] self

# File lib/s3po/message.rb, line 50
def add_text(t)
  nt = [event[:text], t].compact.join(' ')
  event[:text] = nt[0].upcase + nt[1..-1]
  return self
end
attachments() click to toggle source
# File lib/s3po/message.rb, line 56
def attachments
  event[:attachments]
end
attachments=(a) click to toggle source
# File lib/s3po/message.rb, line 60
def attachments=(a)
  event[:attachments] = a
end
channel() click to toggle source
# File lib/s3po/message.rb, line 76
def channel
  event[:channel]
end
for_me?() click to toggle source
# File lib/s3po/message.rb, line 28
def for_me?
  # check user is not myself
  myid = MosEisley.config.meta[:user_id]
  return false if event[:user] == myid
  return true if message_type == :im && simple_message?
  return true if (event[:text] =~ /^<@#{myid}[|>]/) && simple_message?
  return false
end
message_type() click to toggle source
# File lib/s3po/message.rb, line 10
def message_type
  return nil if event[:channel].nil?
  t = :na
  case event[:channel][0]
  when 'C'
    t = :channel
  when 'D'
    t = :im
  when 'G'
    t = :group
  end
  return t
end
postable_object() click to toggle source
# File lib/s3po/message.rb, line 97
def postable_object
  obj = @event.dup
  return obj unless obj[:attachments]
  obj[:attachments] = S3PO.json_with_object(obj[:attachments])
  return obj
end
reply(t = nil) click to toggle source

Create a new Message object for replying; pre-fills some attributes @param t [String] thread timestamp; only when replying as a thread (better to call reply_in_thread instead) @return [MosEisley::S3PO::Message]

# File lib/s3po/message.rb, line 107
def reply(t = nil)
  s = {
    channel: channel,
    as_user: true,
    attachments: []
  }
  s[:text] = "<@#{user}>" unless message_type == :im
  if thread_ts
    s[:thread_ts] = thread_ts
  elsif t
    s[:thread_ts] = t
  end
  Message.new(s)
end
reply_in_thread() click to toggle source
# File lib/s3po/message.rb, line 122
def reply_in_thread
  t = thread_ts
  t ||= ts
  reply(t)
end
simple_message?() click to toggle source
# File lib/s3po/message.rb, line 24
def simple_message?
  event[:subtype].nil?
end
text() click to toggle source
# File lib/s3po/message.rb, line 37
def text
  event[:text]
end
text=(t) click to toggle source

Defines text; existing string will be overwritten @param t [String] text to assign

# File lib/s3po/message.rb, line 43
def text=(t)
  event[:text] = t
end
text_body() click to toggle source
# File lib/s3po/message.rb, line 84
def text_body
  return nil unless simple_message?
  t = event[:text]
  t = t.sub(/^<@#{MosEisley.config.meta[:user_id]}[|]?[^>]*>/, '') if for_me?
  return t
end
text_plain() click to toggle source
# File lib/s3po/message.rb, line 91
def text_plain
  t = text_body
  return nil unless t
  return S3PO.decode_text(t)
end
thread_ts() click to toggle source
# File lib/s3po/message.rb, line 80
def thread_ts
  event[:thread_ts]
end
ts() click to toggle source
# File lib/s3po/message.rb, line 72
def ts
  event[:ts]
end
user() click to toggle source
# File lib/s3po/message.rb, line 64
def user
  event[:user]
end
user=(u) click to toggle source
# File lib/s3po/message.rb, line 68
def user=(u)
  event[:user] = u
end