Class: S3PO::Parser

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

Class Method Summary collapse

Class Method Details

.instruction_from_plain(plain, botid) ⇒ Object


38
39
40
41
42
43
44
45
# File 'lib/s-3po/parser.rb', line 38

def self.instruction_from_plain(plain, botid)
  a = plain.split()
  if a[0] && a[0].start_with?("@#{botid}")
    prefix = a.shift
  end
  return nil if a.empty?
  return a
end

.mentions_from_text(text) ⇒ Object


15
16
17
18
19
# File 'lib/s-3po/parser.rb', line 15

def self.mentions_from_text(text)
  mentions = []
  text.scan(/<@([^>|]*)[^>]*>/) { |m| mentions << m[0]}
  return mentions
end

.parse_event(event, opts = {}) ⇒ Object


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

def self.parse_event(event, opts = {})
  obj = JSON.parse(event, {symbolize_names: true})
  return Response.new(obj, opts) if obj[:type].nil?
  return Message.new(obj, opts) if obj[:type] == 'message'
  return Event.new(obj, opts)
end

.plain_from_text(text) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/s-3po/parser.rb', line 21

def self.plain_from_text(text)
  # copy
  plain = String.new(text)
  # remove labels within brackets
  plain.gsub!(/<([^>|]*)[^>]*>/, '<\1>')
  # process commands
  plain.gsub!(/<!(everyone|channel|group)>/, '<@\1>')
  plain.gsub!(/<!(.*?)>/, '<\1>')
  # remove brackets
  plain.gsub!(/<(.*?)>/, '\1')
  # unescape
  plain.gsub!('&gt;', '>')
  plain.gsub!('&lt;', '<')
  plain.gsub!('&amp;', '&')
  return plain
end