Class: Argosnap::OSXNotifications

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

Instance Method Summary collapse

Constructor Details

#initializeOSXNotifications

Returns a new instance of OSXNotifications.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/argosnap/osxnotify.rb', line 7

def initialize
  begin
    user = ENV['USER']
    raise ArgumentError.new("This command is made for Darwin! Please check the website for details #{url} !") unless Gem::Platform.local.os == 'darwin'
    r = YAML::load_file("#{Dir.home}/.argosnap/config.yml")
    logfile = "#{Dir.home}/.argosnap/argosnap.log"
    @threshold, @logger, @picodollars, @time_interval = r[:threshold], Logger.new(logfile), Argosnap::Fetch.new.balance, r[:seconds]
  rescue ArgumentError => e
    puts e.message
    # puts e.backtrace
    exit
  end
end

Instance Method Details

#displayObject

Display current ballance in OSX style notifications



54
55
56
# File 'lib/argosnap/osxnotify.rb', line 54

def display
  TerminalNotifier.notify("Current balance: #{@picodollars}", :title => 'TarSnap', :subtitle => 'balance running out') 
end

#display_checkObject

Display notification if threshold is surpassed



49
50
51
# File 'lib/argosnap/osxnotify.rb', line 49

def display_check
  TerminalNotifier.notify("Current balance: #{@picodollars}", :title => 'TarSnap', :subtitle => 'balance running out') if @picodollars < @threshold.to_i
end

#install_launchd_scriptObject

Creates launchd script with user’s variables

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/argosnap/osxnotify.rb', line 22

def install_launchd_script 
  raise ArgumentError.new("Please make sure you run 'argosnap install' in order to install configuration files, before running asnap!") unless File.exists?("#{Dir.home}/.argosnap/config.yml")
  user = ENV['USER']
  c = "#{Dir.home}/.argosnap/config.yml"
  begin
    launch_agents = "/Users/#{user}/Library/LaunchAgents/"
    # start_script loads the RVM environment.
    start_script = File.expand_path('../../../files/local.sh', __FILE__)
    if Dir.exists?(launch_agents)
      filename = "org.#{user}.argosnap.plist"
      if File.exist?("#{launch_agents}#{filename}")
        puts "Please delete file: #{launch_agents}#{filename} before proceeding!"
      else
        hash = {"Label"=>"#{filename.scan(/(.+)(?:\.[^.]+)/).flatten[0]}", "ProgramArguments"=>["#{start_script}"], "StartInterval"=> @time_interval, "RunAtLoad"=>true} 
        File.open("#{launch_agents}#{filename}", 'w') {|f| f.write(hash.to_plist)}
        puts "Launchd script is installed. Type 'launchctl load -w #{launch_agents}#{filename}' to load the plist."
      end
    else
      puts "No '#{launch_agents}' directory found! Aborting installation."
    end
  rescue Exception => e
    puts e.message
    # puts e.backtrace
  end
end