Sha256: 5aa8007c9d5eea59ffd967adccb6343a68c3e3190f0c26d3c876a4b3cb684eef

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

# todo: unit tests

module Rerun
  class Notification
    include System

    attr_reader :title, :body, :options

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

    def command
      return unless mac?

      # 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

      s
    end

    def command_named(name)
      path = `which #{name}`.chomp
      path.empty? ? nil : path
    end

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

    def app_name
      options[:name]
    end

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

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

  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
poll-rerun-0.11.2 lib/rerun/notification.rb
poll-rerun-0.11.1 lib/rerun/notification.rb
rerun-0.11.0 lib/rerun/notification.rb