Class: S3PO::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/s-3po/generator.rb

Class Method Summary collapse

Class Method Details

.generate_message(message, id = nil) ⇒ Object


16
17
18
19
20
21
22
23
# File 'lib/s-3po/generator.rb', line 16

def self.generate_message(message, id = nil)
  @id ||= 0
  obj = message.object
  msg = {type: 'message', channel: obj[:channel], text: obj[:text]}
  msg[:id] = id ? id : @id
  @id += 1 unless id
  return JSON.fast_generate(msg)
end

.generate_ping(id = nil) ⇒ Object


8
9
10
11
12
13
14
# File 'lib/s-3po/generator.rb', line 8

def self.generate_ping(id = nil)
  @id ||= 0
  msg = {type: 'ping'}
  msg[:id] = id ? id : @id
  @id += 1 unless id
  return JSON.fast_generate(msg)
end

.text_from_plain(plain) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/s-3po/generator.rb', line 25

def self.text_from_plain(plain)
  text = String.new(plain)
  # escape
  text.gsub!('&', '&')
  text.gsub!('<', '&lt;')
  text.gsub!('>', '&gt;')
  # add brackets to mentions
  text.gsub!(/(@[a-z0-9][a-z0-9.\-_]*)/, '<\1>')
  text.gsub!(/(@U[A-Z0-9]+)/, '<\1>')
  # add brackets to channels
  text.gsub!(/(#[a-z0-9\-_]+)/, '<\1>')
  text.gsub!(/(#C[A-Z0-9]+)/, '<\1>')
  # convert commands
  text.gsub!(/<@(everyone|channel|group)>/, '<!\1>')
  return text
end