ruby_gntp 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,188 @@
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
+
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
+ @notifications = params[:notifications]
53
+ raise TooFewParametersError, "Need least one 'notification' for register" unless @notifications
54
+
55
+ @app_icon = params[:app_icon]
56
+
57
+ @message = <<EOF
58
+ GNTP/1.0 REGISTER NONE
59
+ Application-Name: #{@app_name}
60
+ Notifications-Count: #{@notifications.size}
61
+ EOF
62
+ @message << "Application-Icon: #{@app_icon}\n" if @app_icon
63
+ @message << "\n"
64
+
65
+ @notifications.each do |notification|
66
+ name = notification[:name]
67
+ disp_name = notification[:disp_name]
68
+ enabled = notification[:enabled] || true
69
+ icon = notification[:icon]
70
+
71
+ @message += <<EOF
72
+ Notification-Name: #{name}
73
+ EOF
74
+ @message << "Notification-Display-Name: #{disp_name}\n" if disp_name
75
+ @message << "Notification-Enabled: #{enabled}\n" if enabled
76
+ @message << "Notification-Icon: #{icon}\n" if icon
77
+ @message << "\n"
78
+ end
79
+
80
+ unless (ret = send_and_recieve(@message))
81
+ raise "Register failed"
82
+ end
83
+ end
84
+
85
+
86
+ #
87
+ # notify
88
+ #
89
+ def notify(params)
90
+ name = params[:name]
91
+ raise TooFewParametersError, "Notification need 'name', 'title' parameters" unless name || title
92
+
93
+ title = params[:title]
94
+ text = params[:text]
95
+ icon = params[:icon] || get_notification_icon(name)
96
+ sticky = params[:sticky]
97
+
98
+ @message = <<EOF
99
+ GNTP/1.0 NOTIFY NONE
100
+ Application-Name: #{@app_name}
101
+ Notification-Name: #{name}
102
+ Notification-Title: #{title}
103
+ EOF
104
+ @message << "Notification-Text: #{text}\n" if text
105
+ @message << "Notification-Sticky: #{sticky}\n" if sticky
106
+ @message << "Notification-Icon: #{icon}\n" if icon
107
+ @message << "\n"
108
+
109
+ unless (ret = send_and_recieve(@message))
110
+ raise "Notify failed"
111
+ end
112
+ end
113
+
114
+
115
+ #
116
+ # instant notification
117
+ #
118
+ def self.notify(params)
119
+ growl = GNTP.new(params[:app_name])
120
+ notification = params
121
+ notification[:name] = params[:app_name] || "Ruby/GNTP notification"
122
+ growl.register(:notifications => [
123
+ :name => notification[:name]
124
+ ])
125
+ growl.notify(notification)
126
+ end
127
+
128
+ private
129
+
130
+ #
131
+ # send and recieve
132
+ #
133
+ def send_and_recieve msg
134
+ msg.gsub!(/\n/, "\r\n")
135
+ print msg if $DEBUG
136
+
137
+ sock = TCPSocket.open(@target_host, @target_port)
138
+ sock.write msg
139
+
140
+ ret = nil
141
+ while rcv = sock.gets
142
+ break if rcv == "\r\n"
143
+ print ">#{rcv}" if $DEBUG
144
+ ret = $1 if /GNTP\/1.0\s+-(\S+)/ =~ rcv
145
+ end
146
+ sock.close
147
+
148
+ return 'OK' == ret
149
+ end
150
+
151
+ #
152
+ # get notification icon
153
+ #
154
+ def get_notification_icon(name)
155
+ notification = @notifications.find {|n| n[:name] == name}
156
+ return nil unless notification
157
+ return notification[:icon]
158
+ end
159
+ end
160
+
161
+ #----------------------------
162
+ # self test code
163
+ if __FILE__ == $0
164
+ #--- Use standard notification method ('register' first then 'notify')
165
+ growl = GNTP.new("Ruby/GNTP self test")
166
+ growl.register({:notifications => [{
167
+ :name => "notify",
168
+ :enabled => true,
169
+ }]})
170
+
171
+ growl.notify({
172
+ :name => "notify",
173
+ :title => "Congraturation",
174
+ :text => "Congraturation! You are successful install ruby_gntp.",
175
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
176
+ :sticky=> true,
177
+ })
178
+
179
+ #--- Use instant notification method (just 'notify')
180
+ GNTP.notify({
181
+ :app_name => "Instant notify",
182
+ :title => "Instant notification",
183
+ :text => "Instant notification available now.",
184
+ :icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
185
+ })
186
+ end
187
+
188
+ # vim: ts=2 sw=2 expandtab fdm=marker
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_gntp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - snaka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-20 00:00:00 +09: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
+ - example/twitter_notifier.rb
27
+ - example/gntp-notify
28
+ - README
29
+ - TODO
30
+ - ChangeLog
31
+ has_rdoc: false
32
+ homepage: http://snaka.github.com/ruby_gntp/
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.2
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Ruby library for GNTP(Growl Notification Transport Protocol) client
59
+ test_files: []
60
+