ruby-growlをつかう

Railsをgemでupdateするついでに,gemにどんなgemがあるか一覧を見てたら*1ruby-growlとかゆー素敵そうなのがあったので試す.

とりあえず

とりあえず,ソースコードに書いてあった例をやってみるぞ.

require 'rubygems'
require 'ruby-growl'

g = Growl.new "localhost", "ruby-growl", ["ruby-growl Notification"],nil,"hoge"
g.notify "ruby-growl Notification", "It Came From Ruby-Growl", "Greetings!"

実行結果はこんな感じです.


使えるメソッドは,クラスメソッドのnewとインスタンスメソッドのnotifyだけ.

メソッド

newの引数は順番に,

  • host : is the host to contact.
  • app_name : is the name of the application sending the notifications.
  • all_notifies : is a list of notification types your application sends.
  • default_notifies : is a list of notification types that are turned on by default.
  • password : is the password needed to send notifications to host.

# I'm not sure about what +default_notifies+ is supposed to be set to, since
# there is a comment that says "not a subset of all_notifies" in the code.

default_notifiesとpasswordは書かなくても良い.ただ,default_notifiesは意味わかんない.


notifyの引数は順番に,

  • notify_type : is the type of notification to send.
  • title : is a title for the notification.
  • message : is the body of the notification.
  • priority : is the priorty of message to send.
  • sticky : makes the notification stick until clicked.

これも,priorityとstickyは書かなくても良い.

素数をかぞえる

とりあえず,Macにだって素数を数える権利はある - はこべにっき ♨をもとに,こんなの作った.

require 'rubygems'
require 'ruby-growl'

def primes(n)
  prime = (2 .. n).to_a
  prime.each{|e|
    next if e == nil
    break if (n/e) < 2
    (2 .. (n/e)).each{|x|
      prime[(e * x) - 2] = nil
    }
  }
  prime.compact!
end

primes = primes(100)

g = Growl.new "localhost", "ruby-growl", ["greeting","count prime"], [], "password"

g.notify "greeting", "Emilio Putti", "素数は孤独な数字だ.私に力を与えてくれる."

primes.each_with_index{|e,i|
  d =  i % 7 == 0 ? "落ち着け...素数を数えて落ち着くのだ...\n" : ""
  prime = "...#{e}"
  
  g.notify "count prime", d, prime, 2
}

しかし,これだとGrowlに一気にパケットを送っちゃうので,幾らか改良の余地有り.応急処置だと,sleepを挟むとか.

その他

gemを漁ると,gmailerとかいう素敵チックなgemもあるのですが,ソースコードを読んでるとwin32APIとか書いててゲンナリ.しかしながら,実際にREADMEを見ながらコーディングして見ると,引っかかるのはCookieのトコロ.意味わかんないなぁ・・・.


もっとgemもCPANみたくなればなぁ.

*1:gem q --remote