Skip to content

Instantly share code, notes, and snippets.

@ne-sachirou
Created March 17, 2011 22:02
Show Gist options
  • Save ne-sachirou/875223 to your computer and use it in GitHub Desktop.
Save ne-sachirou/875223 to your computer and use it in GitHub Desktop.
HttpGrowl.rb
# HTTP POSTをWEBRickで受け、Growl通知します。
# POSTのkeyには、 title, text, twitter_screen_name, icon_uri, sticky を使用します。
# ruby_gntp と nokogiri に依存します。
require 'webrick'
require 'ruby_gntp'
require 'open-uri'
require 'nokogiri'
require 'json'
OPTION = {
:DocumentRoot => '.',
:BindAddress => '127.0.0.1',
:Port => 10080
}
def getTwitterIconUri screen_name
open "http://twitter.com/#{screen_name}" do |file|
doc = Nokogiri::HTML file
doc.css('#profile-image')[0]['src']
end
end
class GrowlServlet < WEBrick::HTTPServlet::AbstractServlet
@@growl = GNTP.new 'HTTP GNTP'
@@growl.register({
:notifications => [
{
:name => 'notify',
:enabled => true
}
]
})
def do_POST req, res
res['Content-Type'] = 'text/plain'
begin
@@growl.notify({
:name => 'notify',
:title => req.query['title'],
:text => req.query['text'],
:icon => if req.query['twitter_screen_name'] != ''
getTwitterIconUri req.query['twitter_screen_name']
else
req.query['icon_uri']
end,
:sticky => !!req.query['sticky']
})
res.body = "{\"ok\": \"growl ok\", \"request\": #{req.query.to_json}}"
rescue
res.body = "{\"error\": \"#$!\", \"request\": #{req.query.to_json}}"
end
end
end
server = WEBrick::HTTPServer.new OPTION
server.mount '/gntp', GrowlServlet
server.start
<!docutype html>
<form action="http://localhost:10080/gntp" method="post">
title: <input type="text" value="title" name="title" size="20"/><br/>
text: <input type="text" value="text" name="text" size="30"/><br/>
icon_uri: <input type="text" value="" name="icon_uri" size="60"/><br/>
twitter_screen_name: <input type="text" value="ne_sachirou" name="twitter_screen_name" size="20"/><br/>
sticky: <input type="checkbox" name="sticky"/><br/>
<input type="submit" value="POST"/>
</form>
@ne-sachirou
Copy link
Author

Commit 445d1d1328aa28f9f4b9b69f8e4f290f296b621a to ne-sachirou/AzureaVim - GitHub ne-sachirou/AzureaVim@445d1d1
AzureaVimへ移動

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment