Module: Tweet

Defined in:
lib/tweet.rb

Overview

Tweet

Synopsis

A generalized notification daemon. Can be petitioned by an arbitrary application to activate a notification. Notifications can be customized by the user via a plugin architecture. Examples include a GTK2 popup dialog, a syslog entry, or an email alert.

Can also be required in a ruby script to access a running Tweet daemon (see Tweet.notify, Tweet.message, Tweet.running?, Tweet.stop).

The website is here: tweet.rubyforge.org/

Installation

gem install tweet

or maybe:

sudo gem install tweet

Usage

tweetd [options]

Requirements

  • Ruby 1.8

  • daemons library

  • pidify library

Examples

To start the daemon:

tweetd -d

To contact the daemon from the shell:

tweet "hello world"

To contact the daemon from ruby:

require 'tweet'
Tweet.message("hello world")

Author

Payton Swick, 2006

License Creative Commons GPL license.

creativecommons.org/licenses/GPL/2.0/

Defined Under Namespace

Classes: Monitor, Note, Notifier

Constant Summary collapse

VERSION =
'0.6'
PRI =

Priority levels.

They can be referenced from other classes like this:

Tweet::PRI['WARNING']
{
  'CRITICAL' => 1,
  'WARNING'  => 2,
  'INFO'     => 3,
  'LOW'      => 4,
  'DEBUG'    => 5
}
PORT =

The TCP port to listen on.

9128
USE_SYSLOG =

Enable using syslog for tweetd events (this is different from the syslog notifier plugin, as it does not log notifications. This logs only tweetd activity, eg: stopping and starting.)

true
USE_GTK_TRAY_ICON =

Experimental GTK Tray icon.

true

Class Method Summary collapse

Class Method Details

.message(message, duration = 0) ⇒ Object

Given a String, this will try to contact a running tweet instance and send the String as a notification from the calling application with a duration of a number of seconds (the default is a duration of 0 (permanent)). This is a simple form of Tweet::notify.

Example:

require 'tweet'
Tweet.message "Hello World!"


121
122
123
# File 'lib/tweet.rb', line 121

def message(message, duration=0)
  notify(Note.info(message, message, duration, $0))
end

.message_with_notifiers(message, notifiers = [], duration = 0) ⇒ Object

Just like Tweet.message, but allows specifying an array of notifier plugins to use as requested notifiers on the server. See Monitor for more information.



128
129
130
# File 'lib/tweet.rb', line 128

def message_with_notifiers(message, notifiers=[], duration=0)
  notify(Note.new(message, message, duration, Tweet::PRI['INFO'], $0, notifiers))
end

.notify(note) ⇒ Object

Given an instance of Note, this will try to contact a running tweet instance and send a notification. See Tweet::message for a simpler version.

Example:

require 'tweet'
Tweet.notify(Tweet::Note.info("Hello World", "Hello World!", "5", "test application"))


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tweet.rb', line 97

def notify(note)
  require 'net/telnet'
  message = 'app "'+note.application.to_s+'";priority "'+note.priority.to_s+'";duration "'+note.duration.to_s+'";notifier "'+note.requested_notifiers.join(' ')+'";title "'+note.title.to_s+'";message "'+note.message.to_s+'"'
  begin
    conn = Net::Telnet.new({'Host' => 'localhost', 'Port' => PORT, 'Telnet mode' => false })
  rescue
    raise "Connection to tweet failed.  Maybe tweetd is not running?"
  end
  out = conn.recv(128)
  raise "While trying to connect to tweetd, server replied: '#{out.chomp}'" if out !~ /^2/
  conn.write(message+"\n")
  out = conn.recv(128)
  raise "While trying to send this: '#{message}', tweetd replied: '#{out.chomp}'" if out !~ /^2/
  conn.close
end

.running?Boolean

Returns true if an instance of tweetd is running (or appears to be running).

Returns:

  • (Boolean)


139
140
141
# File 'lib/tweet.rb', line 139

def running?
  Pidify.running?
end

.startObject



148
149
150
# File 'lib/tweet.rb', line 148

def start
  Pidify.start
end

.stopObject

Stops a running instance of tweetd.



144
145
146
# File 'lib/tweet.rb', line 144

def stop
  Pidify.stop
end

.where_am_i?Boolean

Returns the path location to the library file.

Returns:

  • (Boolean)


133
134
135
# File 'lib/tweet.rb', line 133

def where_am_i?
  File.dirname(__FILE__)+'/..'
end