Class: MosEisley::S3PO::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(e) ⇒ Message

Returns a new instance of Message.



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

def initialize(e)
  @event = e
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



4
5
6
# File 'lib/s3po/message.rb', line 4

def event
  @event
end

Instance Method Details

#add_text(t) ⇒ Object

Adds text after a space if text already exists

Parameters:

  • t (String)

    text to add

Returns:

  • (Object)

    self



50
51
52
53
54
# 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

#attachmentsObject



56
57
58
# File 'lib/s3po/message.rb', line 56

def attachments
  event[:attachments]
end

#attachments=(a) ⇒ Object



60
61
62
# File 'lib/s3po/message.rb', line 60

def attachments=(a)
  event[:attachments] = a
end

#channelObject



76
77
78
# File 'lib/s3po/message.rb', line 76

def channel
  event[:channel]
end

#for_me?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# 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_typeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# 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_objectObject



97
98
99
100
101
102
# 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) ⇒ MosEisley::S3PO::Message

Create a new Message object for replying; pre-fills some attributes

Parameters:

  • t (String) (defaults to: nil)

    thread timestamp; only when replying as a thread (better to call #reply_in_thread instead)

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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_threadObject



122
123
124
125
126
# File 'lib/s3po/message.rb', line 122

def reply_in_thread
  t = thread_ts
  t ||= ts
  reply(t)
end

#simple_message?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/s3po/message.rb', line 24

def simple_message?
  event[:subtype].nil?
end

#textObject



37
38
39
# File 'lib/s3po/message.rb', line 37

def text
  event[:text]
end

#text=(t) ⇒ Object

Defines text; existing string will be overwritten

Parameters:

  • t (String)

    text to assign



43
44
45
# File 'lib/s3po/message.rb', line 43

def text=(t)
  event[:text] = t
end

#text_bodyObject



84
85
86
87
88
89
# 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_plainObject



91
92
93
94
95
# File 'lib/s3po/message.rb', line 91

def text_plain
  t = text_body
  return nil unless t
  return S3PO.decode_text(t)
end

#thread_tsObject



80
81
82
# File 'lib/s3po/message.rb', line 80

def thread_ts
  event[:thread_ts]
end

#tsObject



72
73
74
# File 'lib/s3po/message.rb', line 72

def ts
  event[:ts]
end

#userObject



64
65
66
# File 'lib/s3po/message.rb', line 64

def user
  event[:user]
end

#user=(u) ⇒ Object



68
69
70
# File 'lib/s3po/message.rb', line 68

def user=(u)
  event[:user] = u
end