Archive for the ‘Google’ Category
RubyでGmailを利用したメール送信
GmailのSMTPを利用してメール送信するためにTLSが必要ですが、Ruby 1.8系列では標準で利用できません。以下にRuby 1.8系列でTLSを利用するための方法をメモしておきます。
tlsmailというライブラリを利用するので、あらかじめインストールしておく必要があります。
sudo gem install tlsmail
ライブラリがインストールされた状態で以下のように記述すればGmailを利用したメール送信が可能です。
require 'rubygems' require 'net/smtp' require 'openssl' require 'tlsmail' host = 'smtp.gmail.com' port = 587 helo = 'gmail.com' user = 'abc@gmail.com' password = 'password' Net::SMTP.enable_tls OpenSSL::SSL::VERIFY_NONE Net::SMTP.start(host, port, helo, user, password, :plain) do |smtp| # メール送信 end
ちなみに、Gmailでは拡張子が.exe、.zip、.tar、.tgz、.z、.gzのファイルを添付ファイルとして送信することはできません。
downloaderでは、この問題に対してファイル名の末尾に_(アンダーバー)を付加することで強引に対処しています。
Google Chromeのtextareaを外部エディタで編集する (Ruby版)
Google Chrome のテキストエリアを外部エディタで編集する Edit with Emacs
自分のPCにはPerlがインストールされていないので、Rubyで書いてみました。Perlは分からないので勘で移植しましたが、一応動作しているようです。
#!/usr/bin/ruby -Ku
require 'webrick'
require 'tempfile'
require 'kconv'
$EDITOR = 'C:\vim\gvim.exe'
$PORT = 9292
server = WEBrick::HTTPServer.new(:Port => $PORT)
trap('INT') { server.shutdown }
server.mount_proc('/status') do |req, res|
res.status = 200
end
server.mount_proc('/edit') do |req, res|
temp = Tempfile.new('editwith_')
temp << req.body
temp.close false
system $EDITOR, temp.path
temp.open
res.body = temp.read.toutf8
temp.close
res.status = 200
res['Content-Type'] = 'text/plain'
res['Content-Length'] = res.body.size
end
server.start
$EDITORと$PORTを適切に設定すれば動作するんじゃないかと思います。