Class: SlackItcAutoingestion::Slack

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_itc_autoingestion/slack.rb

Constant Summary collapse

DATE_FORMAT =
'%a, %b %-d, %Y'
ATTACTMENT_COLOR =
'#0594f9'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Slack

Returns a new instance of Slack.



10
11
12
13
14
# File 'lib/slack_itc_autoingestion/slack.rb', line 10

def initialize(params = {})
  @command = params[:command]
  @token = params[:token]
  @report_params = parse_text params[:text]
end

Instance Attribute Details

#reportObject

Returns the value of attribute report.



7
8
9
# File 'lib/slack_itc_autoingestion/slack.rb', line 7

def report
  @report
end

#report_paramsObject (readonly)

Returns the value of attribute report_params.



8
9
10
# File 'lib/slack_itc_autoingestion/slack.rb', line 8

def report_params
  @report_params
end

Instance Method Details

#attachmentsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/slack_itc_autoingestion/slack.rb', line 49

def attachments
  slack_attachments = []
  grouped_skus = @report.group_by { |i| i[:sku] }
  products = grouped_skus.select { |_, skus| skus[0][:parent_id].empty? }
  iaps = grouped_skus.select { |_, skus| skus[0][:parent_id].present? }

  products.each do |sku_id, skus|
    units = skus.reduce(0) {|sum, sku| sum + sku[:units] }
    sku_iaps = iaps.select { |_, iap| iap[0][:parent_id] == sku_id }

    slack_attachments.push({
      fallback: fallback(units),
      title: skus[0][:title],
      text: "#{units} units sold.",
      fields: fields(sku_iaps),
      color: ATTACTMENT_COLOR
    })
  end

  slack_attachments
end

#date_textObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/slack_itc_autoingestion/slack.rb', line 89

def date_text
  item = @report.first
  begin_date = item[:begin_date]
  end_date = item[:end_date]

  if begin_date == end_date
    "on #{formatted_date(begin_date)}"
  else
    "between #{formatted_date(begin_date)}-#{formatted_date(end_date)}"
  end
end

#error_messageObject



25
26
27
28
29
30
31
# File 'lib/slack_itc_autoingestion/slack.rb', line 25

def error_message
  if !valid_command?
    "Invalid Slack command received. Got '#{@command}' and expected '#{SlackItcAutoingestion.configuration.slack_command}'."
  elsif !valid_token?
    "Invalid Slack token received."
  end
end

#fallback(units) ⇒ Object



71
72
73
# File 'lib/slack_itc_autoingestion/slack.rb', line 71

def fallback(units)
  "#{units} units sold #{date_text}."
end

#fields(iaps) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/slack_itc_autoingestion/slack.rb', line 75

def fields(iaps)
  slack_fields = []

  iaps.each do |_, skus|
    slack_fields.push({
      title: skus[0][:title],
      value: skus.reduce(0) {|sum, sku| sum + sku[:units] },
      short: true
    })
  end

  slack_fields
end

#formatted_date(date) ⇒ Object



101
102
103
# File 'lib/slack_itc_autoingestion/slack.rb', line 101

def formatted_date(date)
  date.strftime DATE_FORMAT
end

#parse_text(text) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/slack_itc_autoingestion/slack.rb', line 16

def parse_text(text)
  data = {}
  args = text.split(' ')
  data[:report_date] = args[0] if args[0]
  data[:date_type] = args[1].capitalize if args[1]
  data[:report_type] = args[2].titleize.gsub(' ', '-') if args[2]
  data
end

#textObject



45
46
47
# File 'lib/slack_itc_autoingestion/slack.rb', line 45

def text
  "Units sold #{date_text}."
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/slack_itc_autoingestion/slack.rb', line 33

def valid?
  valid_command? && valid_token?
end

#valid_command?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/slack_itc_autoingestion/slack.rb', line 37

def valid_command?
  @command == SlackItcAutoingestion.configuration.slack_command
end

#valid_token?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/slack_itc_autoingestion/slack.rb', line 41

def valid_token?
  @token == SlackItcAutoingestion.configuration.slack_token
end