Skip to content
Browse files

Initial release; extracted from test_notifier gem.

  • Loading branch information...
0 parents commit b5dff1fd94cd9732d96f7f178fe103f36fd475b6 @fnando committed
3 .gitignore
@@ -0,0 +1,3 @@
+pkg/*
+*.gem
+.bundle
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in notifier.gemspec
+gemspec
67 README.rdoc
@@ -0,0 +1,67 @@
+= Notifier
+
+Send system notifications on several platforms with a simple and unified API. Currently supports:
+
+* Growl (Mac OS X)
+* Kdialog (Linux/KDE)
+* Knotify (Linux/KDE)
+* OSD Cat (Linux)
+* Libnotify (Linux)
+* Snarl (Windows)
+
+== Installation
+
+ gem install notifier
+
+== Usage
+
+Notifier will try to detect which notifiers are available in your system. So you can just send a message:
+
+ Notifier.notify(
+ :image => "image.png",
+ :title => "Testing Notifier",
+ :message => "Sending an important message!"
+ )
+
+Not all notifiers support the image option, therefore it will be ignored.
+
+If your system support more than one notifier, you can specify which one you prefer:
+
+ Notifier.default_notifier = :notify_send
+
+The available names are <tt>growl</tt>, <tt>kdialog</tt>, <tt>knotify</tt>, <tt>notify_send</tt>, <tt>osd_cat</tt>, and <tt>snarl</tt>.
+
+There are several helper methods that you can use in order to retrieve notifiers.
+
+* <tt>Notifier.notifier</tt>: return the first supported notifier
+* <tt>Notifier.notifiers</tt>: return all notifiers
+* <tt>Notifier.supported_notifiers</tt>: return only supported notifiers
+* <tt>Notifier.from_name(name)</tt>: find notifier by its name
+* <tt>Notifier.supported_notifier_from_name(name)</tt>: find a supported notifier by its name
+
+== Maintainer
+
+* Nando Vieira - http://nandovieira.com.br
+
+== License
+
+(The MIT License)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 Rakefile
@@ -0,0 +1,10 @@
+require "bundler"
+Bundler::GemHelper.install_tasks
+
+require "rake/testtask"
+Rake::TestTask.new do |t|
+ t.libs += %w[test lib]
+ t.ruby_opts = %w[-rubygems]
+ t.test_files = FileList["test/**/*_test.rb"]
+ t.verbose = true
+end
49 lib/notifier.rb
@@ -0,0 +1,49 @@
+module Notifier
+ autoload :Growl, "notifier/growl"
+ autoload :Snarl, "notifier/snarl"
+ autoload :OsdCat, "notifier/osd_cat"
+ autoload :Knotify, "notifier/knotify"
+ autoload :Kdialog, "notifier/kdialog"
+ autoload :NotifySend, "notifier/notify_send"
+ autoload :Placebo, "notifier/placebo"
+
+ extend self
+
+ class << self
+ attr_accessor :default_notifier
+ end
+
+ def notifier
+ supported_notifier_from_name(default_notifier) || supported_notifiers.first
+ end
+
+ def notify(options)
+ notifier.notify(options)
+ end
+
+ def notifiers
+ constants.collect do |name|
+ const_get(name) unless name.to_s == "Placebo"
+ end.compact + [Placebo]
+ end
+
+ def supported_notifiers
+ notifiers.select {|notifier| notifier.supported?}
+ end
+
+ def from_name(name)
+ notifier = const_get(classify(name.to_s))
+ rescue Exception
+ nil
+ end
+
+ def supported_notifier_from_name(name)
+ notifier = from_name(name)
+ notifier && notifier.supported? ? notifier : nil
+ end
+
+ private
+ def classify(string)
+ string.gsub(/_(.)/sm) { "#{$1.upcase}" }.gsub(/^(.)/) { "#{$1.upcase}" }
+ end
+end
23 lib/notifier/growl.rb
@@ -0,0 +1,23 @@
+module Notifier
+ module Growl
+ extend self
+
+ SCRIPT = File.dirname(__FILE__) + "/../../../resources/register-growl.scpt"
+ FILE = File.expand_path("~/.test_notifier-growl")
+
+ def supported?
+ RUBY_PLATFORM =~ /darwin/ && `ps -Al | grep GrowlHelper` && `which growlnotify` && $? == 0
+ end
+
+ def notify(options)
+ Thread.new do
+ `growlnotify -n test_notifier --image #{options[:image]} -p 2 -m '#{options[:message]}' -t '#{options[:title]}'`
+ end
+ end
+
+ def register
+ return if File.file?(FILE)
+ system "osascript #{SCRIPT} > #{FILE}"
+ end
+ end
+end
15 lib/notifier/kdialog.rb
@@ -0,0 +1,15 @@
+module Notifier
+ module Kdialog
+ extend self
+
+ def supported?
+ RUBY_PLATFORM =~ /(linux|freebsd)/ && `which kdialog > /dev/null` && $? == 0
+ end
+
+ def notify(options)
+ Thread.new do
+ `kdialog --title #{options[:title]} --passivepopup \"#{options[:message]}\" 5`
+ end
+ end
+ end
+end
15 lib/notifier/knotify.rb
@@ -0,0 +1,15 @@
+module Notifier
+ module Knotify
+ extend self
+
+ def supported?
+ RUBY_PLATFORM =~ /(linux|freebsd)/ && `ps -Al | grep dcop` && $? == 0
+ end
+
+ def notify(options)
+ Thread.new do
+ `dcop knotify default notify eventname \'#{options[:title]}\' \'#{options[:message]}\' '' '' 16 2`
+ end
+ end
+ end
+end
15 lib/notifier/notify_send.rb
@@ -0,0 +1,15 @@
+module Notifier
+ module NotifySend
+ extend self
+
+ def supported?
+ RUBY_PLATFORM =~ /(linux|freebsd)/ && `which notify-send > /dev/null` && $? == 0
+ end
+
+ def notify(options)
+ Thread.new do
+ `notify-send -i #{options[:image]} #{options[:title]} \"#{options[:message]}\"`
+ end
+ end
+ end
+end
25 lib/notifier/osd_cat.rb
@@ -0,0 +1,25 @@
+module Notifier
+ module OsdCat
+ extend self
+
+ FONT = "-bitstream-charter-bold-r-normal--33-240-100-100-p-206-iso8859-1"
+ POSITION = "top"
+ POSITION_OFFSET = "0"
+ ALIGN = "center"
+ COLORS = {
+ :fail => "orange",
+ :success => "green",
+ :error => "red"
+ }
+
+ def supported?
+ RUBY_PLATFORM =~ /(linux|freebsd)/ && `which osd_cat > /dev/null` && $? == 0
+ end
+
+ def notify(options)
+ Thread.new do
+ `echo #{options[:message].inspect} | osd_cat --font=#{FONT} --shadow=0 --pos=#{POSITION} -o #{POSITION_OFFSET} --delay=4 --outline=4 --align=#{ALIGN} -c #{COLORS[options[:status]]}`
+ end
+ end
+ end
+end
12 lib/notifier/placebo.rb
@@ -0,0 +1,12 @@
+module Notifier
+ module Placebo
+ extend self
+
+ def supported?
+ true
+ end
+
+ def notify(options)
+ end
+ end
+end
20 lib/notifier/snarl.rb
@@ -0,0 +1,20 @@
+module Notifier
+ module Snarl
+ extend self
+
+ def supported?
+ return false unless RUBY_PLATFORM =~ /(mswin|mingw)/
+
+ begin
+ require "snarl" unless defined?(::Snarl)
+ true
+ rescue LoadError
+ false
+ end
+ end
+
+ def notify(options)
+ ::Snarl.show_message(options[:title], options[:message], options[:image])
+ end
+ end
+end
8 lib/notifier/version.rb
@@ -0,0 +1,8 @@
+module Notifier
+ module Version
+ MAJOR = 0
+ MINOR = 1
+ PATCH = 0
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
+ end
+end
24 notifier.gemspec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "notifier/version"
+
+Gem::Specification.new do |s|
+ s.name = "notifier"
+ s.version = Notifier::Version::STRING
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Nando Vieira"]
+ s.email = ["fnando.vieira@gmail.com"]
+ s.homepage = "http://rubygems.org/gems/notifier"
+ s.summary = "Send system notifications on several platforms with a simple and unified API. Currently supports Growl, Libnotify, OSD, KDE (Knotify and Kdialog) and Snarl"
+ s.description = "Send system notifications on several platforms with a simple and unified API. Currently supports Growl, Libnotify, OSD, KDE (Knotify and Kdialog) and Snarl"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.requirements << "Growl, Libnotify, OSD, KDE (Knotify and Kdialog) or Snarl"
+
+ s.add_development_dependency "test-unit"
+ s.add_development_dependency "mocha"
+end
73 test/notifier_test.rb
@@ -0,0 +1,73 @@
+require "test_helper"
+
+class NotifierTest < Test::Unit::TestCase
+ def setup
+ unsupport_all_notifiers
+ Notifier.default_notifier = nil
+ end
+
+ test "retrieve list of supported notifiers" do
+ Notifier::Snarl.expects(:supported?).returns(true)
+ Notifier::Knotify.expects(:supported?).returns(true)
+
+ assert_equal 3, Notifier.supported_notifiers.size
+ end
+
+ test "return first available notifier" do
+ Notifier::Snarl.expects(:supported?).returns(true)
+ Notifier::Knotify.expects(:supported?).returns(true)
+
+ assert_equal Notifier::Snarl, Notifier.notifier
+ end
+
+ test "prefer default notifier" do
+ Notifier::Snarl.stubs(:supported?).returns(true)
+ Notifier::Knotify.expects(:supported?).returns(true)
+
+ Notifier.default_notifier = :knotify
+
+ assert_equal Notifier::Knotify, Notifier.notifier
+ end
+
+ test "send notification" do
+ params = {
+ :title => "Some title",
+ :message => "Some message",
+ :image => "image.png"
+ }
+
+ Notifier::Snarl.expects(:supported?).returns(true)
+ Notifier::Snarl.expects(:notify).with(params)
+
+ Notifier.notify(params)
+ end
+
+ test "retrieve list of all notifiers" do
+ assert_equal 7, Notifier.notifiers.size
+ end
+
+ test "consider Placebo as fallback notifier" do
+ assert_equal Notifier::Placebo, Notifier.supported_notifiers.last
+ end
+
+ test "return notifier by its name" do
+ assert_equal Notifier::OsdCat, Notifier.from_name(:osd_cat)
+ assert_equal Notifier::NotifySend, Notifier.from_name(:notify_send)
+ assert_equal Notifier::Growl, Notifier.from_name(:growl)
+ end
+
+ test "return notifier by its name when supported" do
+ Notifier::Snarl.expects(:supported?).returns(true)
+
+ assert_equal Notifier::Snarl, Notifier.supported_notifier_from_name(:snarl)
+ end
+
+ test "return nil when have no supported notifiers" do
+ assert_nil Notifier.supported_notifier_from_name(:snarl)
+ end
+
+ test "return nil when an invalid notifier name is provided" do
+ assert_nil Notifier.from_name(:invalid)
+ assert_nil Notifier.supported_notifier_from_name(:invalid)
+ end
+end
14 test/test_helper.rb
@@ -0,0 +1,14 @@
+gem "test-unit"
+require "test/unit"
+require "mocha"
+
+require "notifier"
+
+class Test::Unit::TestCase
+ private
+ def unsupport_all_notifiers
+ Notifier.notifiers.each do |notifier|
+ notifier.stubs(:supported?).returns(false) unless notifier == Notifier::Placebo
+ end
+ end
+end

0 comments on commit b5dff1f

Please sign in to comment.
Something went wrong with that request. Please try again.