Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit ac0e45c53855e7529672bfd47585e53645d11dba 1 parent ba7bf85
@snaka authored
View
5 ChangeLog
@@ -0,0 +1,5 @@
+= ruby_gntp change log
+
+== Version 0.0.0
+* Initial commit
+
View
4 TODO
@@ -0,0 +1,4 @@
+= TODO
+* Include test code into gem
+* Write RDoc comment
+
View
168 lib/ruby_gntp.rb
@@ -0,0 +1,168 @@
+#!/usr/bin/ruby
+#
+# Ruby library for GNTP/1.0
+#
+# LICENSE:{{{
+# Copyright (c) 2009 snaka<snaka.gml@gmail.com>
+#
+# 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.
+#
+# 日本語訳:
+# http://www.opensource.jp/licenses/mit-license.html
+#
+#}}}
+require 'socket'
+
+$DEBUG = true
+
+class TooFewParametersError < Exception
+end
+
+class GNTP
+ attr_reader :app_name, :target_host, :target_port
+ attr_reader :message if $DEBUG
+
+ def initialize(app_name = 'Ruby/GNTP', host = 'localhost', port = 23053)
+ @app_name = app_name
+ @target_host = host
+ @target_port = port
+ end
+
+ #
+ # register
+ #
+ def register(params)
+ @app_name = params[:app_name]
+ @notifies = params[:notifies]
+ raise TooFewParametersError, "Need least one 'notification' for register" unless @notifies
+
+ @app_icon = params[:app_icon]
+
+ @message = <<EOF
+GNTP/1.0 REGISTER NONE
+Application-Name: #{@app_name}
+Notifications-Count: #{@notifies.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]
+
+ @message += <<EOF
+Notification-Name: #{name}
+EOF
+ @message << "Notification-Display-Name: #{disp_name}\n" if disp_name
+ @message << "Notification-Enabled: #{enabled}\n" if enabled
+ @message << "Notification-Icon: #{icon}\n" if icon
+ @message << "\n"
+ end
+
+ unless (ret = send_and_recieve(@message))
+ raise "Register failed"
+ end
+ end
+
+
+ #
+ # notify
+ #
+ def notify(params)
+ name = params[:name]
+ raise TooFewParametersError, "Notification need 'name', 'title' parameters" unless name || title
+
+ title = params[:title]
+ text = params[:text]
+ icon = params[:icon] || get_notification_icon(name)
+ sticky = params[:sticky]
+
+ @message = <<EOF
+GNTP/1.0 NOTIFY NONE
+Application-Name: #{@app_name}
+Notification-Name: #{name}
+Notification-Title: #{title}
+EOF
+ @message << "Notification-Text: #{text}\n" if text
+ @message << "Notification-Sticky: #{sticky}\n" if sticky
+ @message << "Notification-Icon: #{icon}\n" if icon
+ @message << "\n"
+
+ unless (ret = send_and_recieve(@message))
+ raise "Notify failed"
+ end
+ end
+
+ private
+
+ #
+ # send and recieve
+ #
+ def send_and_recieve msg
+ msg.gsub!(/\n/, "\r\n")
+ print msg if $DEBUG
+
+ sock = TCPSocket.open(@target_host, @target_port)
+ sock.write msg
+
+ ret = nil
+ while rcv = sock.gets
+ break if rcv == "\r\n"
+ print ">#{rcv}" if $DEBUG
+ ret = $1 if /GNTP\/1.0\s+-(\S+)/ =~ rcv
+ end
+ sock.close
+
+ return 'OK' == ret
+ end
+
+ #
+ # get notification icon
+ #
+ def get_notification_icon(name)
+ notification = @notifies.find {|n| n[:name] == name}
+ return nil unless notification
+ return notification[:icon]
+ end
+end
+
+#----------------------------
+# self test code
+if __FILE__ == $0
+ growl = GNTP.new
+ growl.register({
+ :app_name => "Ruby/GNTP self test",
+ :notifies => [{
+ :name => "hoge",
+ :enabled => true,
+ }]
+ })
+
+ growl.notify({
+ :name => "hoge",
+ :title => "GrowlTestです",
+ :text => "hogeほげ?",
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif"
+ })
+end
+
+# vim: ts=2 sw=2 expandtab fdm=marker
View
BIN  ruby_gntp-0.0.0.gem
Binary file not shown
View
15 ruby_gntp.gemspec
@@ -0,0 +1,15 @@
+Gem::Specification.new do |s|
+ s.name = "ruby_gntp"
+ s.version = "0.0.0"
+ s.summary = "Ruby library for GNTP(Growl Notification Transport Protocol) client"
+ s.author = "snaka"
+ s.email = "snaka.gml@gmail.com"
+ s.homepage = "http://d.hatena.ne.jp/snaka72/"
+ s.files = [
+ "lib/ruby_gntp.rb",
+ "README",
+ "TODO",
+ "ChangeLog"
+ ]
+ s.has_rdoc = false
+end
Please sign in to comment.
Something went wrong with that request. Please try again.