Class: Yudachi::SlackNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/yudachi/slack_notifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ SlackNotifier

Returns a new instance of SlackNotifier.



5
6
7
# File 'lib/yudachi/slack_notifier.rb', line 5

def initialize(url)
  @url = url
end

Instance Method Details

#notify(datapoints:, location_name:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/yudachi/slack_notifier.rb', line 9

def notify(datapoints:, location_name:)
  n = Slack::Notifier.new(@url) do
    defaults username: 'Yudachi'
  end

  beginning_idx = datapoints.find_index { |data| data['Type'] == 'forecast' && data['Rainfall'] >= RAINFALL_THRESHOLD }
  beginning = datapoints[beginning_idx]
  title = "#{location_name}#{beginning["Date"].strftime("%H時%M分")}から雨が降り出しそうです!"
  graph = datapoints[beginning_idx..].map { |data| data['Date'].strftime('%H:%M: ') + 'x' * data['Rainfall'].ceil }.join("\n")

  n.ping attachments: [
    { title: title, text: graph, color: 'danger' },
  ]
end