snaka-ruby_gntp 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/ChangeLog +7 -0
  2. data/README +4 -0
  3. data/TODO +4 -0
  4. data/lib/ruby_gntp.rb +168 -0
  5. metadata +56 -0
@@ -0,0 +1,7 @@
1
+ = ruby_gntp change log
2
+
3
+ == Version 0.0.0
4
+ * Create public Git repository
5
+
6
+
7
+
data/README ADDED
@@ -0,0 +1,4 @@
1
+ Ruby library for GNTP(Growl Notification Transport Protocol)
2
+
3
+ Sorry, document is not avilable now.
4
+
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ = TODO
2
+ * Include test code into gem
3
+ * Write RDoc comment
4
+
@@ -0,0 +1,168 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # Ruby library for GNTP/1.0
4
+ #
5
+ # LICENSE:{{{
6
+ # Copyright (c) 2009 snaka<snaka.gml@gmail.com>
7
+ #
8
+ # The MIT License
9
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ # of this software and associated documentation files (the "Software"), to deal
11
+ # in the Software without restriction, including without limitation the rights
12
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ # copies of the Software, and to permit persons to whom the Software is
14
+ # furnished to do so, subject to the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included in
17
+ # all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ # THE SOFTWARE.
26
+ #
27
+ # 日本語訳:
28
+ # http://www.opensource.jp/licenses/mit-license.html
29
+ #
30
+ #}}}
31
+ require 'socket'
32
+
33
+ $DEBUG = true
34
+
35
+ class TooFewParametersError < Exception
36
+ end
37
+
38
+ class GNTP
39
+ attr_reader :app_name, :target_host, :target_port
40
+ attr_reader :message if $DEBUG
41
+
42
+ def initialize(app_name = 'Ruby/GNTP', host = 'localhost', port = 23053)
43
+ @app_name = app_name
44
+ @target_host = host
45
+ @target_port = port
46
+ end
47
+
48
+ #
49
+ # register
50
+ #
51
+ def register(params)
52
+ @app_name = params[:app_name]
53
+ @notifies = params[:notifies]
54
+ raise TooFewParametersError, "Need least one 'notification' for register" unless @notifies
55
+
56
+ @app_icon = params[:app_icon]
57
+
58
+ @message = <<EOF
59
+ GNTP/1.0 REGISTER NONE
60
+ Application-Name: #{@app_name}
61
+ Notifications-Count: #{@notifies.size}
62
+ EOF
63
+ @message << "Application-Icon: #{@app_icon}\n" if @app_icon
64
+ @message << "\n"
65
+
66
+ @notifies.each do |notify|
67
+ name = notify[:name]
68
+ disp_name = notify[:disp_name]
69
+ enabled = notify[:enabled]
70
+ icon = notify[:icon]
71
+
72
+ @message += <<EOF
73
+ Notification-Name: #{name}
74
+ EOF
75
+ @message << "Notification-Display-Name: #{disp_name}\n" if disp_name
76
+ @message << "Notification-Enabled: #{enabled}\n" if enabled
77
+ @message << "Notification-Icon: #{icon}\n" if icon
78
+ @message << "\n"
79
+ end
80
+
81
+ unless (ret = send_and_recieve(@message))
82
+ raise "Register failed"
83
+ end
84
+ end
85
+
86
+
87
+ #
88
+ # notify
89
+ #
90
+ def notify(params)
91
+ name = params[:name]
92
+ raise TooFewParametersError, "Notification need 'name', 'title' parameters" unless name || title
93
+
94
+ title = params[:title]
95
+ text = params[:text]
96
+ icon = params[:icon] || get_notification_icon(name)
97
+ sticky = params[:sticky]
98
+
99
+ @message = <<EOF
100
+ GNTP/1.0 NOTIFY NONE
101
+ Application-Name: #{@app_name}
102
+ Notification-Name: #{name}
103
+ Notification-Title: #{title}
104
+ EOF
105
+ @message << "Notification-Text: #{text}\n" if text
106
+ @message << "Notification-Sticky: #{sticky}\n" if sticky
107
+ @message << "Notification-Icon: #{icon}\n" if icon
108
+ @message << "\n"
109
+
110
+ unless (ret = send_and_recieve(@message))
111
+ raise "Notify failed"
112
+ end
113
+ end
114
+
115
+ private
116
+
117
+ #
118
+ # send and recieve
119
+ #
120
+ def send_and_recieve msg
121
+ msg.gsub!(/\n/, "\r\n")
122
+ print msg if $DEBUG
123
+
124
+ sock = TCPSocket.open(@target_host, @target_port)
125
+ sock.write msg
126
+
127
+ ret = nil
128
+ while rcv = sock.gets
129
+ break if rcv == "\r\n"
130
+ print ">#{rcv}" if $DEBUG
131
+ ret = $1 if /GNTP\/1.0\s+-(\S+)/ =~ rcv
132
+ end
133
+ sock.close
134
+
135
+ return 'OK' == ret
136
+ end
137
+
138
+ #
139
+ # get notification icon
140
+ #
141
+ def get_notification_icon(name)
142
+ notification = @notifies.find {|n| n[:name] == name}
143
+ return nil unless notification
144
+ return notification[:icon]
145
+ end
146
+ end
147
+
148
+ #----------------------------
149
+ # self test code
150
+ if __FILE__ == $0
151
+ growl = GNTP.new
152
+ growl.register({
153
+ :app_name => "Ruby/GNTP self test",
154
+ :notifies => [{
155
+ :name => "hoge",
156
+ :enabled => true,
157
+ }]
158
+ })
159
+
160
+ growl.notify({
161
+ :name => "hoge",
162
+ :title => "GrowlTestです",
163
+ :text => "hogeほげ?",
164
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif"
165
+ })
166
+ end
167
+
168
+ # vim: ts=2 sw=2 expandtab fdm=marker
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snaka-ruby_gntp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - snaka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-08 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: snaka.gml@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/ruby_gntp.rb
26
+ - README
27
+ - TODO
28
+ - ChangeLog
29
+ has_rdoc: false
30
+ homepage: http://d.hatena.ne.jp/snaka72/
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: Ruby library for GNTP(Growl Notification Transport Protocol) client
55
+ test_files: []
56
+