Permalink
Browse files

Initial commit of fully functional system

  • Loading branch information...
1 parent 9e54694 commit f87c5c73284fc66759f496a262a6768d3ea280f2 @tj committed Apr 8, 2009
Showing with 257 additions and 2 deletions.
  1. +13 −0 Manifest
  2. +3 −1 Todo.rdoc
  3. +2 −1 lib/growl.rb
  4. +138 −0 lib/growl/growl.rb
  5. 0 spec/fixtures/icon.icns
  6. BIN spec/fixtures/image.png
  7. +95 −0 spec/growl_spec.rb
  8. +6 −0 spec/spec_helper.rb
View
@@ -0,0 +1,13 @@
+History.rdoc
+lib/growl/growl.rb
+lib/growl/version.rb
+lib/growl.rb
+Rakefile
+README.rdoc
+spec/growl_spec.rb
+spec/spec_helper.rb
+tasks/docs.rake
+tasks/gemspec.rake
+tasks/spec.rake
+Todo.rdoc
+Manifest
View
@@ -1,7 +1,9 @@
== Major:
-* Nothing
+* Switch aliases since appIcon is ... lame
+* Allow the icon switch to be an image, icon, or extension. This would be much more usable than --icon, --image, and --iconpath
+* Docs for installing growlnotify
== Minor:
View
@@ -21,4 +21,5 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
-require 'growl/version'
+require 'growl/version'
+require 'growl/growl'
View
@@ -0,0 +1,138 @@
+
+module Growl
+
+ BIN = 'growlnotify'
+
+ #--
+ # Exceptions
+ #++
+
+ class Error < StandardError; end
+
+ #--
+ # Singleton methods
+ #++
+
+ module_function
+
+ ##
+ # Execute +args+ against the binary.
+
+ def exec *args
+ Kernel.system BIN, *args
+ end
+
+ ##
+ # Return the version triple of the binary.
+
+ def version
+ `#{BIN} --version`.split[1]
+ end
+
+ ##
+ # Check if the binary is installed and accessable.
+
+ def installed?
+ !! version
+ end
+
+ ##
+ # Return an instance of Growl::Base.
+
+ def new *args, &block
+ Base.new *args, &block
+ end
+
+ #--
+ # Growl base
+ #++
+
+ class Base
+ attr_reader :args
+
+ ##
+ # Initialize with optional +block+, which is then
+ # instance evaled or yielded depending on the blocks arity.
+
+ def initialize &block
+ @args = []
+ if block_given?
+ if block.arity > 0
+ yield self
+ else
+ self.instance_eval &block
+ end
+ end
+ end
+
+ ##
+ # Run the notification, only --message is required.
+
+ def run
+ raise Error, 'message required' unless message
+ self.class.switches.each do |switch|
+ if send(:"#{switch}?")
+ args << "--#{switch}"
+ args << send(switch) if send(switch).is_a? String
+ end
+ end
+ Growl.exec *args
+ end
+
+ ##
+ # Define a switch +name+.
+ #
+ # === examples
+ #
+ # switch :sticky
+ #
+ # @growl.sticky!
+ # @growl.sticky? # => true
+ # @growl.sticky = false
+ # @growl.sticky? # => false
+ #
+
+ def self.switch name
+ ivar = :"@#{name}"
+ (@switches ||= []) << name
+ define_method(:"#{name}") { instance_variable_get(ivar) }
+ define_method(:"#{name}=") { |value| instance_variable_set(ivar, value) }
+ define_method(:"#{name}?") { !! instance_variable_get(ivar) }
+ define_method(:"#{name}!") { instance_variable_set(ivar, true) }
+ end
+
+ ##
+ # Return array of available switch symbols.
+
+ def self.switches
+ @switches
+ end
+
+ #--
+ # Switches
+ #++
+
+ switch :title
+ switch :message
+ switch :sticky
+ switch :name
+ switch :appIcon
+ switch :icon
+ switch :iconpath
+ switch :image
+ switch :priority
+ switch :identifier
+ switch :host
+ switch :password
+ switch :udp
+ switch :port
+ switch :auth
+ switch :crypt
+
+ end
+
+end
+
+def Growl *args, &block
+ Growl.new(*args, &block).run
+end
View
No changes.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,95 @@
+
+describe Growl do
+
+ describe "#installed?" do
+ it "should check if growlnotify is available" do
+ Growl.should be_installed
+ end
+ end
+
+ describe "#version" do
+ it "should return only the version triple" do
+ Growl.version.should match(/\d+\.\d+\.\d+/)
+ end
+ end
+
+ before :each do
+ @growl = Growl.new
+ @growl.message = 'Hello World'
+ end
+
+ describe "#Growl" do
+ it "should accept a block, running immediately after" do
+ Growl { |n| n.message = 'Invoked via Growl' }
+ end
+ end
+
+ describe "#run" do
+ it "should fail when no message is present" do
+ lambda { Growl.new.run }.should raise_error(Growl::Error, /message required/)
+ end
+
+ it "should execute a growl notification" do
+ @growl.run.should be_true
+ end
+ end
+
+ describe "#sticky!" do
+ it "should make a notification stick until explicitly closed" do
+ @growl.sticky = false
+ @growl.should_not be_sticky
+ @growl.sticky!
+ @growl.should be_sticky
+ @growl.message = 'Im Sticky'
+ @growl.run.should be_true
+ end
+ end
+
+ describe "#name" do
+ it "should set the application name" do
+ @growl.name = 'Ruby'
+ @growl.run.should be_true
+ end
+ end
+
+ describe "#title" do
+ it "should add a title" do
+ @growl.title = 'Im a title'
+ @growl.message = 'I am not a title'
+ @growl.run.should be_true
+ end
+ end
+
+ describe "#appIcon" do
+ it "should use an application for the icon" do
+ @growl.appIcon = 'Safari'
+ @growl.message = 'Safari Icon'
+ @growl.run.should be_true
+ end
+ end
+
+ describe "#iconpath" do
+ it "should use a path for the icon" do
+ @growl.iconpath = fixture 'icon.icns'
+ @growl.message = 'Custom Icon'
+ @growl.run.should be_true
+ end
+ end
+
+ describe "#icon" do
+ it "should use an icon based on a file type" do
+ @growl.icon = 'jpeg'
+ @growl.message = 'Jpeg Icon'
+ @growl.run.should be_true
+ end
+ end
+
+ describe "#image" do
+ it "should use an image path for the 'icon'" do
+ @growl.image = fixture 'image.png'
+ @growl.message = 'Image as icon'
+ @growl.run.should be_true
+ end
+ end
+
+end
View
@@ -0,0 +1,6 @@
+
+require 'growl'
+
+def fixture path
+ File.expand_path File.join(File.dirname(__FILE__), 'fixtures', path)
+end

0 comments on commit f87c5c7

Please sign in to comment.