Class: Argosnap::Notifications

Inherits:
Object
  • Object
show all
Defined in:
lib/argosnap/notifications.rb

Overview

This class handles the available notifications

Instance Method Summary collapse

Instance Method Details

#notifyObject

When tarsnap balance is bellow threshold notify me Method is chosen based on threshold



9
10
11
12
13
14
15
# File 'lib/argosnap/notifications.rb', line 9

def notify
  if config.data[:threshold] >= Fetch.new.balance
    send_mail
    send_pushover
    send_osx_notification
  end
end

#osx_check_and_notifyObject



17
18
19
20
21
# File 'lib/argosnap/notifications.rb', line 17

def osx_check_and_notify
  if config.data[:threshold] >= Fetch.new.balance
    send_osx_notification
  end
end

#send_mailObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/argosnap/notifications.rb', line 23

def send_mail
  if config.data[:notifications_email]
    if config.gem_available?('mail')
      require 'mail'
      require 'socket'
      require_relative File.expand_path("../notifications/mailer", __FILE__)
      Mailer.new(config.data, config.logger).send_mail
    else
      config.log_and_abort("Please install 'mail' gem by executing: '$ gem install mail'.")
    end
  end
end

#send_osx_notificationObject

Manually send notification to osx notifications (desktop)



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

def send_osx_notification
  if config.data[:notifications_osx]
    if Gem::Platform.local.os == 'darwin'
      # load 'terminal-notifier'
      if config.gem_available?('terminal-notifier')
        require 'terminal-notifier'
        require_relative File.expand_path("../notifications/osxnotifications", __FILE__)
        message = "Your picoUSD amount is: #{Fetch.new.balance}"
        title  = 'argosnap'
        subtitle = 'running out of picoUSD!'
        OSXnotify.send(message, title, subtitle)
      else
         config.log_and_abort("You need to install 'terminal-notifier' gem!")
      end
    else
      config.log_and_abort("OSX notifications are available only under OSX > 10.8.x")
    end
  end
end

#send_pushover_notificationObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/argosnap/notifications.rb', line 36

def send_pushover_notification
  if config.data[:notifications_pushover]
    require "net/https"
    require_relative File.expand_path("../notifications/pushover", __FILE__)
    key     = config.data[:pushover][:key]
    token   = config.data[:pushover][:token]
    amount = Fetch.new.balance
    message = "Your current tarsnap amount is #{amount} picoUSD!"
    Pushover.send(token, key, message, config.logger, amount)
  end
end