spidah-ruby_gntp 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,18 @@
1
+ = ruby_gntp change log
2
+
3
+ == Version 0.0.0
4
+ * Create public Git repository
5
+
6
+ == Version 0.0.1
7
+ * Initial version
8
+
9
+ == Version 0.0.2
10
+ * Fixed Japanese translated MIT License page URL.
11
+
12
+ == Version 0.1.0 - 2009/4/17
13
+ * Add GNTP.notify method for instant notification.
14
+ * Add ruby script examples.
15
+
16
+ == Version 0.1.1 - 2009/4/17
17
+ * Add NOTE to example directory.
18
+
data/README ADDED
@@ -0,0 +1,33 @@
1
+ Ruby library for GNTP(Growl Notification Transport Protocol)
2
+
3
+ Sorry, document is not avilable now.
4
+
5
+ Usage example:
6
+
7
+ require 'rubygems'
8
+ require 'ruby_gntp'
9
+
10
+ # -- Standard way
11
+ growl = GNTP.new("Ruby/GNTP self test")
12
+ growl.register({:notifications => [{
13
+ :name => "notify",
14
+ :enabled => true,
15
+ }]})
16
+
17
+ growl.notify({
18
+ :name => "notify",
19
+ :title => "Congraturation",
20
+ :text => "Congraturation! You are successful install ruby_gntp.",
21
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
22
+ :sticky=> true,
23
+ })
24
+
25
+ # -- Instant notification
26
+ GNTP.notify({
27
+ :app_name => "Instant notify",
28
+ :title => "Instant notification",
29
+ :text => "Instant notification available now.",
30
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
31
+ })
32
+
33
+
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ = TODO
2
+ * Include test code into gem
3
+ * Write RDoc comment
4
+
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/ruby
2
+ # Growl notifier command
3
+ # Usage:
4
+ # gntp-notify [message]
5
+ # License:
6
+ # public domain
7
+
8
+ require "rubygems"
9
+ require "ruby_gntp" # >= ver.0.1.0
10
+
11
+ msg = ARGV[0] || "Alert!!"
12
+
13
+ GNTP.notify({
14
+ :app_name => "gntp-notify",
15
+ :title => "from commandline",
16
+ :text => msg,
17
+ :sticky=> true,
18
+ })
19
+
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # Ruby/GNTP example : twitter notifier
4
+ #
5
+ # Usage: {{{
6
+ # Please type the following command from command line,
7
+ # and then, this script gets your time-line every 30 seconds.
8
+ #
9
+ # > ruby twitter_notifyer.rb
10
+ #
11
+ # If you want *STOP* this script, so press Ctrl + 'C'.
12
+ #
13
+ # Require environment variables:
14
+ # - EDITOR : For Pit uses this variable to edit account information.
15
+ # ex) set EDITOR = c:\Progra~1\vim71\vim.exe
16
+ #
17
+ # - HTTP_PROXY : If you access the Internet via proxy server, you need
18
+ # this variable setting.
19
+ # If variable's value icludes protcol scheme('http://' etc.)
20
+ # would ignore that.
21
+ # ex) set HTTP_PROXY = http://proxy.host.name:8080
22
+ # or
23
+ # set HTTP_PROXY = proxy.host.name:8080
24
+ #
25
+ # Web page:
26
+ # http://d.hatena.ne.jp/snaka72/
27
+ # http://sumimasen2.blogspot.com/
28
+ #
29
+ # License: public domain
30
+ # }}}
31
+
32
+ require 'net/http'
33
+
34
+ require 'rubygems'
35
+ require 'json'
36
+ require 'pit'
37
+ require 'ruby_gntp'
38
+
39
+ $tweeted = {}
40
+
41
+ $growl = GNTP.new
42
+ $growl.register({
43
+ :app_name => "Twitter",
44
+ :notifications => [{ :name => "Tweet", :enabled => true },
45
+ { :name => "Error", :enabled => true }]
46
+ })
47
+
48
+ def get_timeline
49
+
50
+ max_count = 20
51
+
52
+ config = Pit.get("twitter", :require => {
53
+ "username" => "your twittername",
54
+ "password" => "your password"
55
+ })
56
+
57
+ Net::HTTP.version_1_2
58
+ req = Net::HTTP::Get.new('/statuses/friends_timeline.json')
59
+ req.basic_auth config["username"], config["password"]
60
+
61
+ proxy_host, proxy_port = (ENV["HTTP_PROXY"] || '').sub(/http:\/\//, '').split(':')
62
+
63
+ Net::HTTP::Proxy(proxy_host, proxy_port).start('twitter.com') {|http|
64
+ res = http.request(req)
65
+
66
+ if res.code != '200'
67
+ $growl.notify({
68
+ :name => "Error",
69
+ :title => "Error occurd",
70
+ :test => "Can not get messages"
71
+ })
72
+ puts res if $DEBUG
73
+ return
74
+ end
75
+
76
+ results = JSON.parser.new(res.body).parse()
77
+ results.reverse!
78
+ results.length.times do |i|
79
+ break if i >= max_count
80
+
81
+ id = results[i]["id"]
82
+ next if $tweeted.include?(id)
83
+
84
+ puts screen_name = results[i]["user"]["screen_name"]
85
+ puts text = results[i]["text"]
86
+ puts icon = results[i]["user"]["profile_image_url"]
87
+
88
+ $growl.notify({
89
+ :name => "Tweet",
90
+ :title => screen_name,
91
+ :text => text,
92
+ :icon => icon
93
+ })
94
+ $tweeted[id] = true
95
+
96
+ sleep 1
97
+ end
98
+ }
99
+ end
100
+
101
+ # Check timeline evry 30 seconds.
102
+ while true do
103
+ get_timeline
104
+ sleep 30
105
+ end
106
+
107
+ # vim: ts=2 sw=2 et fdm=marker
data/lib/ruby_gntp.rb ADDED
@@ -0,0 +1,206 @@
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
+ # Japanese:
28
+ # http://sourceforge.jp/projects/opensource/wiki/licenses%2FMIT_license
29
+ #
30
+ #}}}
31
+ require 'socket'
32
+ require 'digest/md5'
33
+
34
+ #$DEBUG = true
35
+
36
+ class TooFewParametersError < Exception
37
+ end
38
+
39
+ class GNTP
40
+ attr_reader :app_name, :target_host, :target_port, :password
41
+ attr_reader :message if $DEBUG
42
+
43
+ def initialize(app_name = 'Ruby/GNTP', host = 'localhost', password = '', port = 23053)
44
+ @app_name = app_name
45
+ @target_host = host
46
+ @target_port = port
47
+ @password = password
48
+ end
49
+
50
+ #
51
+ # register
52
+ #
53
+ def register(params)
54
+ @notifications = params[:notifications]
55
+ raise TooFewParametersError, "Need least one 'notification' for register" unless @notifications
56
+
57
+ @app_icon = params[:app_icon]
58
+
59
+ @message = <<EOF
60
+ #{get_gntp_header_start('REGISTER')}
61
+ Application-Name: #{@app_name}
62
+ Notifications-Count: #{@notifications.size}
63
+ EOF
64
+ @message << "Application-Icon: #{@app_icon}\n" if @app_icon
65
+ @message << "\n"
66
+
67
+ @notifications.each do |notification|
68
+ name = notification[:name]
69
+ disp_name = notification[:disp_name]
70
+ enabled = notification[:enabled] || true
71
+ icon = notification[:icon]
72
+
73
+ @message += <<EOF
74
+ Notification-Name: #{name}
75
+ EOF
76
+ @message << "Notification-Display-Name: #{disp_name}\n" if disp_name
77
+ @message << "Notification-Enabled: #{enabled}\n" if enabled
78
+ @message << "Notification-Icon: #{icon}\n" if icon
79
+ @message << "\n"
80
+ end
81
+
82
+ unless (ret = send_and_recieve(@message))
83
+ raise "Register failed"
84
+ end
85
+ end
86
+
87
+
88
+ #
89
+ # notify
90
+ #
91
+ def notify(params)
92
+ name = params[:name]
93
+ raise TooFewParametersError, "Notification need 'name', 'title' parameters" unless name || title
94
+
95
+ title = params[:title]
96
+ text = params[:text]
97
+ icon = params[:icon] || get_notification_icon(name)
98
+ sticky = params[:sticky]
99
+
100
+ @message = <<EOF
101
+ #{get_gntp_header_start('NOTIFY')}
102
+ Application-Name: #{@app_name}
103
+ Notification-Name: #{name}
104
+ Notification-Title: #{title}
105
+ EOF
106
+ @message << "Notification-Text: #{text}\n" if text
107
+ @message << "Notification-Sticky: #{sticky}\n" if sticky
108
+ @message << "Notification-Icon: #{icon}\n" if icon
109
+ @message << "\n"
110
+
111
+ unless (ret = send_and_recieve(@message))
112
+ raise "Notify failed"
113
+ end
114
+ end
115
+
116
+
117
+ #
118
+ # instant notification
119
+ #
120
+ def self.notify(params)
121
+ growl = GNTP.new(params[:app_name])
122
+ notification = params
123
+ notification[:name] = params[:app_name] || "Ruby/GNTP notification"
124
+ growl.register(:notifications => [
125
+ :name => notification[:name]
126
+ ])
127
+ growl.notify(notification)
128
+ end
129
+
130
+ private
131
+
132
+ #
133
+ # send and recieve
134
+ #
135
+ def send_and_recieve msg
136
+ msg.gsub!(/\n/, "\r\n")
137
+ print msg if $DEBUG
138
+
139
+ sock = TCPSocket.open(@target_host, @target_port)
140
+ sock.write msg
141
+
142
+ ret = nil
143
+ while rcv = sock.gets
144
+ break if rcv == "\r\n"
145
+ print ">#{rcv}" if $DEBUG
146
+ ret = $1 if /GNTP\/1.0\s+-(\S+)/ =~ rcv
147
+ end
148
+ sock.close
149
+
150
+ return 'OK' == ret
151
+ end
152
+
153
+ #
154
+ # get notification icon
155
+ #
156
+ def get_notification_icon(name)
157
+ notification = @notifications.find {|n| n[:name] == name}
158
+ return nil unless notification
159
+ return notification[:icon]
160
+ end
161
+
162
+ #
163
+ # get start of the GNTP header
164
+ #
165
+ def get_gntp_header_start(type)
166
+ if @password.empty?
167
+ "GNTP/1.0 #{type} NONE"
168
+ else
169
+ saltvar = Time.now.to_s
170
+ salt = Digest::MD5.digest(saltvar)
171
+ salthash = Digest::MD5.hexdigest(saltvar)
172
+ key = Digest::MD5.digest("#{@password}#{salt}")
173
+ keyhash = Digest::MD5.hexdigest(key)
174
+ "GNTP/1.0 #{type} NONE MD5:#{keyhash}.#{salthash}"
175
+ end
176
+ end
177
+ end
178
+
179
+ #----------------------------
180
+ # self test code
181
+ if __FILE__ == $0
182
+ #--- Use standard notification method ('register' first then 'notify')
183
+ growl = GNTP.new("Ruby/GNTP self test")
184
+ growl.register({:notifications => [{
185
+ :name => "notify",
186
+ :enabled => true,
187
+ }]})
188
+
189
+ growl.notify({
190
+ :name => "notify",
191
+ :title => "Congraturation",
192
+ :text => "Congraturation! You are successful install ruby_gntp.",
193
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
194
+ :sticky=> true,
195
+ })
196
+
197
+ #--- Use instant notification method (just 'notify')
198
+ GNTP.notify({
199
+ :app_name => "Instant notify",
200
+ :title => "Instant notification",
201
+ :text => "Instant notification available now.",
202
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
203
+ })
204
+ end
205
+
206
+ # vim: ts=2 sw=2 expandtab fdm=marker
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spidah-ruby_gntp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - snaka
8
+ - David Hayward (spidah)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-05-16 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ - snaka.gml@gmail.com
20
+ - spidahman@gmail.com
21
+ executables: []
22
+
23
+ extensions: []
24
+
25
+ extra_rdoc_files: []
26
+
27
+ files:
28
+ - lib/ruby_gntp.rb
29
+ - example/twitter_notifier.rb
30
+ - example/gntp-notify
31
+ - README
32
+ - TODO
33
+ - ChangeLog
34
+ has_rdoc: false
35
+ homepage: http://snaka.github.com/ruby_gntp/
36
+ licenses:
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Ruby library for GNTP(Growl Notification Transport Protocol) client
61
+ test_files: []
62
+