snaka-ruby_gntp 0.0.2 → 0.1.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.
- data/ChangeLog +4 -0
- data/README +29 -0
- data/example/gntp-notify +19 -0
- data/example/twitter_notifier.rb +107 -0
- data/lib/ruby_gntp.rb +44 -24
- metadata +3 -1
data/ChangeLog
CHANGED
data/README
CHANGED
|
@@ -2,3 +2,32 @@ Ruby library for GNTP(Growl Notification Transport Protocol)
|
|
|
2
2
|
|
|
3
3
|
Sorry, document is not avilable now.
|
|
4
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/example/gntp-notify
ADDED
|
@@ -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
CHANGED
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
25
25
|
# THE SOFTWARE.
|
|
26
26
|
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
27
|
+
# Japanese:
|
|
28
|
+
# http://sourceforge.jp/projects/opensource/wiki/licenses%2FMIT_license
|
|
29
29
|
#
|
|
30
30
|
#}}}
|
|
31
31
|
require 'socket'
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
#$DEBUG = true
|
|
34
34
|
|
|
35
35
|
class TooFewParametersError < Exception
|
|
36
36
|
end
|
|
@@ -49,25 +49,24 @@ class GNTP
|
|
|
49
49
|
# register
|
|
50
50
|
#
|
|
51
51
|
def register(params)
|
|
52
|
-
@
|
|
53
|
-
|
|
54
|
-
raise TooFewParametersError, "Need least one 'notification' for register" unless @notifies
|
|
52
|
+
@notifications = params[:notifications]
|
|
53
|
+
raise TooFewParametersError, "Need least one 'notification' for register" unless @notifications
|
|
55
54
|
|
|
56
55
|
@app_icon = params[:app_icon]
|
|
57
56
|
|
|
58
57
|
@message = <<EOF
|
|
59
58
|
GNTP/1.0 REGISTER NONE
|
|
60
59
|
Application-Name: #{@app_name}
|
|
61
|
-
Notifications-Count: #{@
|
|
60
|
+
Notifications-Count: #{@notifications.size}
|
|
62
61
|
EOF
|
|
63
62
|
@message << "Application-Icon: #{@app_icon}\n" if @app_icon
|
|
64
63
|
@message << "\n"
|
|
65
64
|
|
|
66
|
-
@
|
|
67
|
-
name =
|
|
68
|
-
disp_name =
|
|
69
|
-
enabled =
|
|
70
|
-
icon =
|
|
65
|
+
@notifications.each do |notification|
|
|
66
|
+
name = notification[:name]
|
|
67
|
+
disp_name = notification[:disp_name]
|
|
68
|
+
enabled = notification[:enabled] || true
|
|
69
|
+
icon = notification[:icon]
|
|
71
70
|
|
|
72
71
|
@message += <<EOF
|
|
73
72
|
Notification-Name: #{name}
|
|
@@ -112,6 +111,20 @@ EOF
|
|
|
112
111
|
end
|
|
113
112
|
end
|
|
114
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
|
+
|
|
115
128
|
private
|
|
116
129
|
|
|
117
130
|
#
|
|
@@ -139,7 +152,7 @@ EOF
|
|
|
139
152
|
# get notification icon
|
|
140
153
|
#
|
|
141
154
|
def get_notification_icon(name)
|
|
142
|
-
notification = @
|
|
155
|
+
notification = @notifications.find {|n| n[:name] == name}
|
|
143
156
|
return nil unless notification
|
|
144
157
|
return notification[:icon]
|
|
145
158
|
end
|
|
@@ -148,20 +161,27 @@ end
|
|
|
148
161
|
#----------------------------
|
|
149
162
|
# self test code
|
|
150
163
|
if __FILE__ == $0
|
|
151
|
-
|
|
152
|
-
growl.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
:name => "hoge",
|
|
164
|
+
#--- Use standard notification method ('register' first then 'notify')
|
|
165
|
+
growl = GNTP.new("Ruby/GNTP self test")
|
|
166
|
+
growl.register({:notifications => [{
|
|
167
|
+
:name => "notify",
|
|
156
168
|
:enabled => true,
|
|
157
|
-
|
|
158
|
-
})
|
|
169
|
+
}]})
|
|
159
170
|
|
|
160
171
|
growl.notify({
|
|
161
|
-
:name => "
|
|
162
|
-
:title => "
|
|
163
|
-
:text => "
|
|
164
|
-
:icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif"
|
|
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",
|
|
165
185
|
})
|
|
166
186
|
end
|
|
167
187
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: snaka-ruby_gntp
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- snaka
|
|
@@ -23,6 +23,8 @@ extra_rdoc_files: []
|
|
|
23
23
|
|
|
24
24
|
files:
|
|
25
25
|
- lib/ruby_gntp.rb
|
|
26
|
+
- example/twitter_notifier.rb
|
|
27
|
+
- example/gntp-notify
|
|
26
28
|
- README
|
|
27
29
|
- TODO
|
|
28
30
|
- ChangeLog
|