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.



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

def initialize(webhook=false)
  @settings = Settings.new
  @controller = Controller.new
  @slack = configure_slack(webhook)
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



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

def controller
  @controller
end

#slackObject

Returns the value of attribute slack.



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

def slack
  @slack
end

Instance Method Details

#configure_slack(webhook) ⇒ Object



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



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

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

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

    clear_screen
    puts table

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

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



36
37
38
39
40
41
42
# File 'lib/brewer/slacker.rb', line 36

def ping(message="Ping as #{Time.now}")
  if message.is_a? Array
    @slack.ping(message.join("\n"))
  else
    @slack.ping(message)
  end
end