Class: Brewer::Slacker

Inherits:
Object
  • Object
show all
Defined in:
lib/brewer/slacker.rb

Overview

This class is responsible for slack communication

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webhook = false) ⇒ Slacker

Returns a new instance of Slacker.



9
10
11
12
13
# File 'lib/brewer/slacker.rb', line 9

def initialize(webhook=false)
  @settings = Settings.new
  @brewer = Brewer.new
  @slack = configure_slack(webhook)
end

Instance Attribute Details

#brewerObject

Returns the value of attribute brewer.



7
8
9
# File 'lib/brewer/slacker.rb', line 7

def brewer
  @brewer
end

#slackObject

Returns the value of attribute slack.



7
8
9
# File 'lib/brewer/slacker.rb', line 7

def slack
  @slack
end

Instance Method Details

#configure_slack(webhook) ⇒ Object

This will look for a webhook in settings.yml and ask you for one if it doesn’t find one



16
17
18
19
20
21
22
23
24
# File 'lib/brewer/slacker.rb', line 16

def configure_slack(webhook)
  if webhook
    return Slack::Notifier.new webhook
  end
  if !@settings.settings['webhook_url']
    get_new_webhook
  end
  return Slack::Notifier.new @settings.settings['webhook_url']
end

#get_new_webhookObject



26
27
28
29
30
31
32
33
34
# File 'lib/brewer/slacker.rb', line 26

def get_new_webhook
  print "Slack Webhook URL: "
  webhook_url = gets.chomp
  @settings.add({
    'webhook_url' => webhook_url
  })
  @settings.cache
  webhook_url
end

#monitor(delay = 10) ⇒ Object

This does the same thing as Brewer#monitor, but it also sends a slack message after a specified wait, normally 10 minutes



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/brewer/slacker.rb', line 45

def monitor(delay=10)
  while true do
    table = @brewer.status_table

    before_temp = @brewer.pv
    @brewer.wait(to_seconds(delay))
    diff = @brewer.pv - before_temp

    clear_screen
    puts table

    ping([
      "Current Temperature: #{@brewer.pid['pv_temp']} F",
      "Set Value Temperature: #{@brewer.pid['sv_temp']} F",
      "Current temperature has climed #{diff} F since #{delay} minute(s) ago",
      "Sent at #{Time.now.strftime("%H:%M")}"
    ].join("\n"))
  end
  true
end

#ping(message = "ping at #{Time.now}") ⇒ Object

This sends a message in slack. If an array is passed in, it will send it as one message with new lines between each array item



39
40
41
# File 'lib/brewer/slacker.rb', line 39

def ping(message="ping at #{Time.now}")
  @slack.ping(message)
end