Class: Rerun::Notification

Inherits:
Object
  • Object
show all
Includes:
System
Defined in:
lib/rerun/notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from System

#linux?, #mac?, #rails?, #windows?

Constructor Details

#initialize(title, body, options = Options::DEFAULTS.dup) ⇒ Notification

Returns a new instance of Notification.



9
10
11
12
13
# File 'lib/rerun/notification.rb', line 9

def initialize(title, body, options = Options::DEFAULTS.dup)
  @title = title
  @body = body
  @options = options
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/rerun/notification.rb', line 7

def body
  @body
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/rerun/notification.rb', line 7

def options
  @options
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/rerun/notification.rb', line 7

def title
  @title
end

Instance Method Details

#app_nameObject



59
60
61
# File 'lib/rerun/notification.rb', line 59

def app_name
  options[:name]
end

#commandObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rerun/notification.rb', line 15

def command
  # todo: strategy or subclass

  s = nil

  if options[:notify] == true or options[:notify] == "growl"
    if (cmd = command_named("growlnotify"))
      # todo: check version of growlnotify and warn if it's too old
      icon_str = ("--image \"#{icon}\"" if icon)
      s = "#{cmd} -n \"#{app_name}\" -m \"#{body}\" \"#{app_name} #{title}\" #{icon_str}"
    end
  end

  if s.nil? and options[:notify] == true or options[:notify] == "osx"
    if (cmd = command_named("terminal-notifier"))
      icon_str = ("-appIcon \"#{icon}\"" if icon)
      s = "#{cmd} -title \"#{app_name}\" -message \"#{body}\" \"#{app_name} #{title}\" #{icon_str}"
    end
  end

  if s.nil? and options[:notify] == true or options[:notify] == "notify-send"
    if (cmd = command_named('notify-send'))
      icon_str = "--icon #{icon}" if icon
      s = "#{cmd} -t 500 --hint=int:transient:1 #{icon_str} \"#{app_name}: #{title}\" \"#{body}\""
    end
  end

  s
end

#command_named(name) ⇒ Object



45
46
47
48
49
50
# File 'lib/rerun/notification.rb', line 45

def command_named(name)
  which_command = windows? ? 'where.exe' : 'which'
  # TODO: remove 'INFO' error message
  path = `#{which_command} #{name}`.chomp
  path.empty? ? nil : path
end

#iconObject



63
64
65
# File 'lib/rerun/notification.rb', line 63

def icon
  "#{icon_dir}/rails_red_sml.png" if rails?
end

#icon_dirObject



67
68
69
70
# File 'lib/rerun/notification.rb', line 67

def icon_dir
  here = File.expand_path(File.dirname(__FILE__))
  File.expand_path("#{here}/../../icons")
end

#send(background = true) ⇒ Object



52
53
54
55
56
57
# File 'lib/rerun/notification.rb', line 52

def send(background = true)
  return unless command
  with_clean_env do
    `#{command}#{" &" if background}`
  end
end

#with_clean_envObject



72
73
74
75
76
77
78
79
80
# File 'lib/rerun/notification.rb', line 72

def with_clean_env
  if defined?(Bundler)
    Bundler.with_clean_env do
      yield
    end
  else
    yield
  end
end