Skip to content

Commit

Permalink
Moves dependency on terminal-notifier to an independently-maintained …
Browse files Browse the repository at this point in the history
…fork, JacksonGariety/terminal-notifier. Now has a shiny ruby icon and Bitch name for notifications. Bitch.silence! no longer toggles, and Bitch.unsilence! has been added. Now checks Mac OS X version >= 10.8.
  • Loading branch information
Jackson Gariety committed Oct 13, 2013
1 parent 87a847e commit e1c5933
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
.DS_Store
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vendor/terminal-notifier"]
path = vendor/terminal-notifier
url = https://github.com/JacksonGariety/terminal-notifier.git
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Example #2 takes options based on the [terminal-notifier](https://github.com/all

## Contributing

#### Cloning

This repo depends upon my fork of @alloy's `terminal-notifier` gem. To clone with submodules:

$ git clone --recursive https://github.com/JacksonGariety/terminal-notifier.git

#### GitHub for dummies

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
Expand Down
2 changes: 0 additions & 2 deletions bitch.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "terminal-notifier"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
Expand Down
Binary file modified bitch.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 29 additions & 39 deletions lib/bitch.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
require "bitch/version"
require "bitch/os"
require 'bitch/version'
require 'bitch/os'
require 'shellwords'

module Bitch

# toggle silentness
def self.silence!
if @silenced
@silenced = !@silenced
else
@silenced = true
end
end

def self.silent?
@silenced
end

def self.yell(options)
# Main usage: Bitch.yell(MESSAGE)
def self.yell(message, options = {})
return false if @silenced

if OS.mac?
if options.is_a? Hash
options.each do |arg, val|
val.capitalize! if arg == :sound
(args ||= "") << "-#{arg} \"#{val.to_s}\" "
if message.is_a? String
if OS.mac? && `sw_vers -productVersion` >= '10.8'
bin = File.expand_path('../../vendor/terminal-notifier/bitch.app/Contents/MacOS/terminal-notifier', __FILE__)

options[:sound].capitalize! if options[:sound]
options = options.merge(:message => message).map do |argument, value|
value = value.to_s; ["-#{argument}", "#{Shellwords.escape(value[0,1])}#{value[1..-1]}"]
end

command = [bin, *options.flatten]
command = Shellwords.join(command) if RUBY_VERSION < '1.9'

IO.popen(command) # send message and options to terminal-notifier/bitch.app
elsif OS.linux? || OS.unix?
`notify-send \"#{message}\"`
else # OS doesn't have notification support
return "Bitch works on Linux, Unix, or Mac OS X >= 10.8."
end

args.strip!
else
args = "-message \"#{options.to_s}\""
end

`terminal-notifier #{args}`
elsif OS.linux? or OS.unix?
if options.is_a? String
args = "\"#{options}\""
else
return false
end

`notify-send #{args}`
else
return false
else # message is not a string
return "You should give that bitch a string. Bitches love strings."
end
end

# Silence methods
def self.silence!() @silenced = true end
def self.unsilence!() @silenced = false end
def self.silent?() @silenced end
end
87 changes: 87 additions & 0 deletions lib/bitch/yeller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'shellwords'

module TerminalNotifier
BIN_PATH = File.expand_path('../../../vendor/bitch/bitch.app/Contents/MacOS/terminal-notifier', __FILE__)

class UnsupportedPlatformError < StandardError; end
# Returns wether or not the current platform is Mac OS X 10.8, or higher.
def self.available?
@available ||= `uname`.strip == 'Darwin' && `sw_vers -productVersion`.strip >= '10.8'
end

def self.execute(verbose, options)
if available?
command = [BIN_PATH, *options.map { |k,v| v = v.to_s; ["-#{k}", "#{Shellwords.escape(v[0,1])}#{v[1..-1]}"] }.flatten]
command = Shellwords.join(command) if RUBY_VERSION < '1.9'
result = ''
IO.popen(command) do |stdout|
output = stdout.read
STDOUT.print output if verbose
result << output
end
result
else
raise UnsupportedPlatformError, "terminal-notifier is only supported on Mac OS X 10.8, or higher."
end
end

# Sends a User Notification and returns wether or not it was a success.
#
# The available options are `:title`, `:group`, `:activate`, `:open`,
# `:execute`, `:sender`, and `:sound`. For a description of each option see:
#
# https://github.com/alloy/terminal-notifier/blob/master/README.markdown
#
# Examples are:
#
# TerminalNotifier.notify('Hello World')
# TerminalNotifier.notify('Hello World', :title => 'Ruby')
# TerminalNotifier.notify('Hello World', :group => Process.pid)
# TerminalNotifier.notify('Hello World', :activate => 'com.apple.Safari')
# TerminalNotifier.notify('Hello World', :open => 'http://twitter.com/alloy')
# TerminalNotifier.notify('Hello World', :execute => 'say "OMG"')
# TerminalNotifier.notify('Hello World', :sender => 'com.apple.Safari')
# TerminalNotifier.notify('Hello World', :sound => 'default')
#
# Raises if not supported on the current platform.
def notify(message, options = {}, verbose = false)
TerminalNotifier.execute(verbose, options.merge(:message => message))
$?.success?
end
module_function :notify

# Removes a notification that was previously sent with the specified
# ‘group’ ID, if one exists.
#
# If no ‘group’ ID is given, all notifications are removed.
def remove(group = 'ALL', verbose = false)
TerminalNotifier.execute(verbose, :remove => group)
$?.success?
end
module_function :remove

LIST_FIELDS = [:group, :title, :subtitle, :message, :delivered_at].freeze

# If a ‘group’ ID is given, and a notification for that group exists,
# returns a hash with details about the notification.
#
# If no ‘group’ ID is given, an array of hashes describing all
# notifications.
#
# If no information is available this will return `nil`.
def list(group = 'ALL', verbose = false)
output = TerminalNotifier.execute(verbose, :list => group)
return if output.strip.empty?

require 'time'
notifications = output.split("\n")[1..-1].map do |line|
LIST_FIELDS.zip(line.split("\t")).inject({}) do |hash, (key, value)|
hash[key] = key == :delivered_at ? Time.parse(value) : (value unless value == '(null)')
hash
end
end

group == 'ALL' ? notifications : notifications.first
end
module_function :list
end
8 changes: 4 additions & 4 deletions spec/bitch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
describe Bitch do
describe ".yell" do
it "should send a message" do
Bitch.yell("message").should_not be_false
Bitch.yell("a problem you finna resolve").should_not be_false
end

it "should not send a message" do
Bitch.silence!
Bitch.yell("silent message").should_not be_true
Bitch.yell("a problem you were finna resolve but got stupid n' gave up").should_not be_true
end

it "should not send a message after toggling silence" do
Bitch.silence!
Bitch.yell('message').should_not be_false
Bitch.unsilence!
Bitch.yell("a problem you finna resolve").should_not be_false
end
end

Expand Down
1 change: 1 addition & 0 deletions vendor/terminal-notifier
Submodule terminal-notifier added at b436f9

0 comments on commit e1c5933

Please sign in to comment.