Module: MosEisley::S3PO

Defined in:
lib/s3po/s3po.rb,
lib/s3po/action.rb,
lib/s3po/generic.rb,
lib/s3po/message.rb

Defined Under Namespace

Classes: Action, GenericEvent, Message

Class Method Summary collapse

Class Method Details

.create_event(e, type = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/s3po/s3po.rb', line 52

def self.create_event(e, type = nil)
  type ||= e[:type] if e[:type]
  case type
  when 'message', 'app_mention'
    return Message.new(e)
  when :action
    return Action.new(e)
  else
    return GenericEvent.new(e)
  end
end

.decode_text(text) ⇒ String

Return plain text parsing Slack escapes and commands

Parameters:

  • text (String)

    string to decode

Returns:

  • (String)

    plain text



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/s3po/s3po.rb', line 78

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

.escape_command(cmd, label = nil) ⇒ String

Enclose Slack command in control characters

Parameters:

  • cmd (String)

    command

  • label (String) (defaults to: nil)

    optional label

Returns:

  • (String)

    escaped command



98
99
100
# File 'lib/s3po/s3po.rb', line 98

def self.escape_command(cmd, label = nil)
  "<#{cmd}" + (label ? "|#{label}" : '') + '>'
end

.escape_text(text) ⇒ String

Escape string with basic Slack rules; no command encoding is done as it often requires more information than provided in the text

Parameters:

  • text (String)

    string to escape

Returns:

  • (String)

    escaped text



67
68
69
70
71
72
73
# File 'lib/s3po/s3po.rb', line 67

def self.escape_text(text)
  esced = String.new(text)
  esced.gsub!('&', '&amp;')
  esced.gsub!('<', '&lt;')
  esced.gsub!('>', '&gt;')
  return esced
end

.format_json_value(val) ⇒ Object

Return Ruby object/value to JSON standard format

Parameters:

  • val (Object)

Returns:

  • (Object)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/s3po/s3po.rb', line 36

def self.format_json_value(val)
  s3po = MosEisley::S3PO
  case val
  when Array
    val.map { |v| s3po.format_json_value(v) }
  when Hash
    val.reduce({}) { |h, (k, v)| h.merge({k => s3po.format_json_value(v)}) }
  when String
    val.encode('UTF-8', {invalid: :replace, undef: :replace})
  when Time
    val.utc.iso8601
  else
    val
  end
end

.json_with_object(obj, pretty: false, opts: nil) ⇒ String

Convert object into JSON, optionally pretty-format

Parameters:

  • obj (Object)

    any Ruby object

  • opts (Hash) (defaults to: nil)

    any JSON options

Returns:

  • (String)

    JSON string



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/s3po/s3po.rb', line 20

def self.json_with_object(obj, pretty: false, opts: nil)
  return '{}' if obj.nil?
  if pretty
    opts = {
      indent: '  ',
      space: ' ',
      object_nl: "\n",
      array_nl: "\n"
    }
  end
  JSON.fast_generate(MosEisley::S3PO.format_json_value(obj), opts)
end

.parse_json(json) ⇒ Object



9
10
11
12
13
14
# File 'lib/s3po/s3po.rb', line 9

def self.parse_json(json)
  return JSON.parse(json, {symbolize_names: true})
rescue => e
  MosEisley.logger.warn("JSON parse error: #{e}")
  return nil
end