Module: SCSI::Helper
- Defined in:
- lib/scsi/helper.rb
Instance Method Summary collapse
- #attachments_from_text(text) ⇒ Object
- #create_attachments(data:, keys:) ⇒ Object
- #format_info(info) ⇒ Object
-
#json_format_value(val) ⇒ Object
Return Ruby object/value to JSON standard format.
-
#json_with_object(obj, pretty: true, opts: nil) ⇒ String
Convert object into JSON, optionally pretty-format.
- #parse_slash(params) ⇒ Object
- #slash_keys ⇒ Object
Instance Method Details
#attachments_from_text(text) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/scsi/helper.rb', line 39 def (text) = [] text.scan(/<(.*?)>/) do |m| case m[0][0] when '#' keys = [:channel_id, :channel_name] when '@' keys = [:user_id, :user_name] when '!' keys = [:special_command, :label] else keys = [:link, :label] end values = m[0].split('|') data = {keys[0] => values[0][1..-1], keys[1] => values[1]} << (data: data, keys: keys)[0] end return end |
#create_attachments(data:, keys:) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/scsi/helper.rb', line 24 def (data:, keys:) = [] i = 0 while i < keys.count do a = keys[i] b = keys[i + 1] << {fields: [ {title: a, value: data[a], short: true}, {title: b, value: data[b], short: true} ]} i += 2 end return end |
#format_info(info) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/scsi/helper.rb', line 59 def format_info(info) = [] q = '' = (info[:text]) if info[:text] if .empty? = (data: info, keys: slash_keys) else q = " ?>> `#{info[:text]}`" end return { response_type: "ephemeral", text: "SCSI: Slash Command Slack Info v#{SCSI::Version}" + q, attachments: } end |
#json_format_value(val) ⇒ Object
Return Ruby object/value to JSON standard format
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/scsi/helper.rb', line 95 def json_format_value(val) case val when Array val.map { |v| json_format_value(v) } when Hash val.reduce({}) { |h, (k, v)| h.merge({k => json_format_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: true, opts: nil) ⇒ String
Convert object into JSON, optionally pretty-format
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/scsi/helper.rb', line 79 def json_with_object(obj, pretty: true, opts: nil) return '{}' if obj.nil? if pretty opts = { indent: ' ', space: ' ', object_nl: "\n", array_nl: "\n" } end JSON.fast_generate(json_format_value(obj), opts) end |
#parse_slash(params) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/scsi/helper.rb', line 16 def parse_slash(params) return nil unless params.class == Hash info = {} slash_keys.each { |k| info[k] = params[k] } info[:text] = params[:text] return info end |
#slash_keys ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/scsi/helper.rb', line 8 def slash_keys return [ :user_id, :user_name, :channel_id, :channel_name, :team_id, :team_domain ] end |