6月 06 2008

open-uri を Basic認証のあるサイトに使う

Published by HoLY under tech

Ruby の便利標準ライブラリ筆頭(個人的感想)である open-uri ですが、 いまだに「open-uri はBasic認証のあるサイトには使えない」という都市伝説(?)が。

こんな風に :http_basic_authentication オプションを与えることでできるよ。

require 'open-uri'
open('http://www.example.com/authenticated_page/',
     :http_basic_authentication => ['username', 'password'])

現在のマニュアルには載っていない ので注意。 新しい方にも特に書いてはいないみたい

ちなみにプログレスバーのために :content_length_proc と :progress_proc が使えるが、簡単に作るとこんな感じに。

open(downloading-filename, 'w') {|f|
  f.write open(url,
               :content_length_proc => proc{|len|
                 @len = len
               },
               :progress_proc => proc{|now|
                 per = now * 100 / @len
                 $stdout.print "%15u bytes (%3u%%) |" % [now, per]
                 $stdout.print "=" * per
                 $stdout.print ">" if per < 100
                 $stdout.print "-" * (99-per) if per < 99
                 $stdout.print "\r"
                 $stdout.flush
               }
  ).read
}

まだコメントはありません

5月 18 2008

ドロップシャドウをつけるRubyスクリプト

Published by HoLY under tech

三国志大戦3Master のデータを作っていて、既存の大量の画像にドロップシャドウを付けたものが必要になったので、RMagick を使ったRubyスクリプトなどを書いてみた。

続きを読む »

まだコメントはありません