Module: SCSI::Helper

Defined in:
lib/scsi/helper.rb

Instance Method Summary collapse

Instance Method Details

#format_info(info) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/scsi/helper.rb', line 23

def format_info(info)
  attachments = []
  i = 0
  while i < slash_keys.count do
    a = slash_keys[i]
    b = slash_keys[i + 1]
    attachments << {fields: [
      {title: a, value: info[a], short: true},
      {title: b, value: info[b], short: true}
    ]}
    i += 2
  end
  return {
    response_type: "ephemeral",
    text: "SCSI: Slash Command Slack Info v#{SCSI::Version}",
    attachments: attachments
  }
end

#json_format_value(val) ⇒ Object

Return Ruby object/value to JSON standard format

Parameters:

  • val (Object)

Returns:

  • (Object)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/scsi/helper.rb', line 62

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

Parameters:

  • obj (Object)

    any Ruby object

  • opts (Hash) (defaults to: nil)

    any JSON options

Returns:

  • (String)

    JSON string



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/scsi/helper.rb', line 46

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
# 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] }
  return info
end

#slash_keysObject



8
9
10
11
12
13
14
# File 'lib/scsi/helper.rb', line 8

def slash_keys
  return [
    :user_name, :user_id,
    :channel_name, :channel_id,
    :team_domain, :team_id
  ]
end