Permalink
Browse files

Add GNTP.notify method. and add some example script.

  • Loading branch information...
snaka committed Apr 17, 2009
1 parent 133a09f commit 63a59095c6c50d0076517b4ebb2878a9ac52db68
Showing with 206 additions and 25 deletions.
  1. +4 −0 ChangeLog
  2. +29 −0 README
  3. +19 −0 example/gntp-notify
  4. +107 −0 example/twitter_notifier.rb
  5. +44 −24 lib/ruby_gntp.rb
  6. +3 −1 ruby_gntp.gemspec
View
@@ -9,3 +9,7 @@
== Version 0.0.2
* Fixed Japanese translated MIT License page URL.
+== Version 0.1.0 - 2009/4/17
+* Add GNTP.notify method for instant notification.
+* Add ruby script examples.
+
View
29 README
@@ -2,3 +2,32 @@ Ruby library for GNTP(Growl Notification Transport Protocol)
Sorry, document is not avilable now.
+Usage example:
+
+require 'rubygems'
+require 'ruby_gntp'
+
+# -- Standard way
+growl = GNTP.new("Ruby/GNTP self test")
+growl.register({:notifications => [{
+ :name => "notify",
+ :enabled => true,
+}]})
+
+growl.notify({
+ :name => "notify",
+ :title => "Congraturation",
+ :text => "Congraturation! You are successful install ruby_gntp.",
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
+ :sticky=> true,
+})
+
+# -- Instant notification
+GNTP.notify({
+ :app_name => "Instant notify",
+ :title => "Instant notification",
+ :text => "Instant notification available now.",
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
+})
+
+
View
@@ -0,0 +1,19 @@
+#!/usr/bin/ruby
+# Growl notifier command
+# Usage:
+# gntp-notify [message]
+# License:
+# public domain
+
+require "rubygems"
+require "ruby_gntp" # >= ver.0.1.0
+
+msg = ARGV[0] || "Alert!!"
+
+GNTP.notify({
+ :app_name => "gntp-notify",
+ :title => "from commandline",
+ :text => msg,
+ :sticky=> true,
+})
+
View
@@ -0,0 +1,107 @@
+#!/usr/bin/ruby
+#
+# Ruby/GNTP example : twitter notifier
+#
+# Usage: {{{
+# Please type the following command from command line,
+# and then, this script gets your time-line every 30 seconds.
+#
+# > ruby twitter_notifyer.rb
+#
+# If you want *STOP* this script, so press Ctrl + 'C'.
+#
+# Require environment variables:
+# - EDITOR : For Pit uses this variable to edit account information.
+# ex) set EDITOR = c:\Progra~1\vim71\vim.exe
+#
+# - HTTP_PROXY : If you access the Internet via proxy server, you need
+# this variable setting.
+# If variable's value icludes protcol scheme('http://' etc.)
+# would ignore that.
+# ex) set HTTP_PROXY = http://proxy.host.name:8080
+# or
+# set HTTP_PROXY = proxy.host.name:8080
+#
+# Web page:
+# http://d.hatena.ne.jp/snaka72/
+# http://sumimasen2.blogspot.com/
+#
+# License: public domain
+# }}}
+
+require 'net/http'
+
+require 'rubygems'
+require 'json'
+require 'pit'
+require 'ruby_gntp'
+
+$tweeted = {}
+
+$growl = GNTP.new
+$growl.register({
+ :app_name => "Twitter",
+ :notifications => [{ :name => "Tweet", :enabled => true },
+ { :name => "Error", :enabled => true }]
+})
+
+def get_timeline
+
+ max_count = 20
+
+ config = Pit.get("twitter", :require => {
+ "username" => "your twittername",
+ "password" => "your password"
+ })
+
+ Net::HTTP.version_1_2
+ req = Net::HTTP::Get.new('/statuses/friends_timeline.json')
+ req.basic_auth config["username"], config["password"]
+
+ proxy_host, proxy_port = (ENV["HTTP_PROXY"] || '').sub(/http:\/\//, '').split(':')
+
+ Net::HTTP::Proxy(proxy_host, proxy_port).start('twitter.com') {|http|
+ res = http.request(req)
+
+ if res.code != '200'
+ $growl.notify({
+ :name => "Error",
+ :title => "Error occurd",
+ :test => "Can not get messages"
+ })
+ puts res if $DEBUG
+ return
+ end
+
+ results = JSON.parser.new(res.body).parse()
+ results.reverse!
+ results.length.times do |i|
+ break if i >= max_count
+
+ id = results[i]["id"]
+ next if $tweeted.include?(id)
+
+ puts screen_name = results[i]["user"]["screen_name"]
+ puts text = results[i]["text"]
+ puts icon = results[i]["user"]["profile_image_url"]
+
+ $growl.notify({
+ :name => "Tweet",
+ :title => screen_name,
+ :text => text,
+ :icon => icon
+ })
+ $tweeted[id] = true
+
+ sleep 1
+ end
+ }
+end
+
+# Check timeline evry 30 seconds.
+while true do
+ get_timeline
+ sleep 30
+end
+
+# vim: ts=2 sw=2 et fdm=marker
View
@@ -24,13 +24,13 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
-# 日本語訳(japanese):
-# http://sourceforge.jp/projects/opensource/wiki/licenses%2FMIT_license
+# Japanese:
+# http://sourceforge.jp/projects/opensource/wiki/licenses%2FMIT_license
#
#}}}
require 'socket'
-$DEBUG = true
+#$DEBUG = true
class TooFewParametersError < Exception
end
@@ -49,25 +49,24 @@ def initialize(app_name = 'Ruby/GNTP', host = 'localhost', port = 23053)
# register
#
def register(params)
- @app_name = params[:app_name]
- @notifies = params[:notifies]
- raise TooFewParametersError, "Need least one 'notification' for register" unless @notifies
+ @notifications = params[:notifications]
+ raise TooFewParametersError, "Need least one 'notification' for register" unless @notifications
@app_icon = params[:app_icon]
@message = <<EOF
GNTP/1.0 REGISTER NONE
Application-Name: #{@app_name}
-Notifications-Count: #{@notifies.size}
+Notifications-Count: #{@notifications.size}
EOF
@message << "Application-Icon: #{@app_icon}\n" if @app_icon
@message << "\n"
- @notifies.each do |notify|
- name = notify[:name]
- disp_name = notify[:disp_name]
- enabled = notify[:enabled]
- icon = notify[:icon]
+ @notifications.each do |notification|
+ name = notification[:name]
+ disp_name = notification[:disp_name]
+ enabled = notification[:enabled] || true
+ icon = notification[:icon]
@message += <<EOF
Notification-Name: #{name}
@@ -112,6 +111,20 @@ def notify(params)
end
end
+
+ #
+ # instant notification
+ #
+ def self.notify(params)
+ growl = GNTP.new(params[:app_name])
+ notification = params
+ notification[:name] = params[:app_name] || "Ruby/GNTP notification"
+ growl.register(:notifications => [
+ :name => notification[:name]
+ ])
+ growl.notify(notification)
+ end
+
private
#
@@ -139,7 +152,7 @@ def send_and_recieve msg
# get notification icon
#
def get_notification_icon(name)
- notification = @notifies.find {|n| n[:name] == name}
+ notification = @notifications.find {|n| n[:name] == name}
return nil unless notification
return notification[:icon]
end
@@ -148,20 +161,27 @@ def get_notification_icon(name)
#----------------------------
# self test code
if __FILE__ == $0
- growl = GNTP.new
- growl.register({
- :app_name => "Ruby/GNTP self test",
- :notifies => [{
- :name => "hoge",
+ #--- Use standard notification method ('register' first then 'notify')
+ growl = GNTP.new("Ruby/GNTP self test")
+ growl.register({:notifications => [{
+ :name => "notify",
:enabled => true,
- }]
- })
+ }]})
growl.notify({
- :name => "hoge",
- :title => "GrowlTestです",
- :text => "hogeほげ?",
- :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif"
+ :name => "notify",
+ :title => "Congraturation",
+ :text => "Congraturation! You are successful install ruby_gntp.",
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
+ :sticky=> true,
+ })
+
+ #--- Use instant notification method (just 'notify')
+ GNTP.notify({
+ :app_name => "Instant notify",
+ :title => "Instant notification",
+ :text => "Instant notification available now.",
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
})
end
View
@@ -1,12 +1,14 @@
Gem::Specification.new do |s|
s.name = "ruby_gntp"
- s.version = "0.0.2"
+ s.version = "0.1.0"
s.summary = "Ruby library for GNTP(Growl Notification Transport Protocol) client"
s.author = "snaka"
s.email = "snaka.gml@gmail.com"
s.homepage = "http://snaka.github.com/ruby_gntp/"
s.files = [
"lib/ruby_gntp.rb",
+ "example/twitter_notifier.rb",
+ "example/gntp-notify",
"README",
"TODO",
"ChangeLog"

0 comments on commit 63a5909

Please sign in to comment.