2009/02/12


hexdigestでなくdigestですね。:-)
mod_access_tokenを入れてみた - まめ畑

ここまでは良かったのですが、Signatureを生成するスクリプトを作ろうと思ったのですが、Rubyで上手くいかない・・・。

http://d.hatena.ne.jp/con_mame/20090209
あとはbase64に付く改行とターミネータをカットすればOKですね。
require 'openssl'
require 'base64'
require 'uri'
require 'time'

access_key = 'foo'
secret = 'bar'
url = 'http://localhost:8080/access_token/example.jpg'
exp = (Time.new+300).to_i.to_s
uri = URI.parse(url)
plain = "GET#{uri.path}#{exp}#{access_key}"
hmac = OpenSSL::HMAC::digest(OpenSSL::Digest::SHA1.new, secret, plain)
sig = Base64.encode64(hmac).chomp.sub(/=*$/, '')
puts "#{url}?Signature=#{URI.escape(sig)}&AccessKey=#{URI.escape(access_key)}&Expires=#{URI.escape(exp.to_s)}"
結果はこんな感じ。
http://localhost:8080/access_token/example.jpg?Signature=Rt2cKnnOYuDiE/XYKTLTaOuY4Wg&AccessKey=foo&Expires=1234417692 ちなみにBase64.b64encodeはエンコード結果を表示してしまうのでencode64が良さそうですね。
  def b64encode(bin, len = 60)
    encode64(bin).scan(/.{1,#{len}}/) do
      print $&, "\n"
    end
  end 
Posted at by



2008/10/10


ようやく動いた。
Apache2/mod_rubyでtDiaryを動かそうと思ったけど、エラーが出て動かなかった。どうやらrhtmlをERBでパースする所で出てるみたい。
今日はこの不可解な問題と向き合ったお話。
まずはmod_rubyをインストールするに当たりバイナリを取ってきたけどDLLのエントリポイントが見つからないとエラーが出たのでビルドする。
svn co http://svn.modruby.net/repos/mod_ruby/trunk mod_ruby
で最新を取って来て、以下のパッチを当てる。
Index: bucket.c
===================================================================
--- bucket.c    (revision 141)
+++ bucket.c    (working copy)
@@ -28,6 +28,10 @@
 #include "mod_ruby.h"
 #include "apachelib.h"
 
+#ifdef _WIN32
+# undef read
+#endif
+
 #ifdef APACHE2
 
 VALUE rb_cApacheBucket;
そして以下のMakefile.w32をmod_ruby直下に置く
APACHE_ROOT=C:\Program Files\Apache Software Foundation\Apache2.2
RUBY_ROOT=C:\Ruby
SRCS = \
    apache_cookie.c \
    apache_multipart_buffer.c \
    apache_request.c \
    apachelib.c \
    array_header.c \
    bucket.c \
    connection.c \
    cookie.c \
    error.c \
    mod_ruby.c \
    multival.c \
    paramtable.c \
    request.c \
    ruby_config.c \
    ruby_shared_stub.c \
    server.c \
    table.c \
    upload.c \
    uri.c

OBJS = $(SRCS:.c=.obj)
CFLAGS=/I"$(APACHE_ROOT)\include" /I"$(RUBY_ROOT)\lib\ruby\1.8\i386-mswin32" /DWIN32 /nologo
LDFLAGS=/LIBPATH:"$(APACHE_ROOT)\lib" /LIBPATH:"$(RUBY_ROOT)\lib"
LIBS= libapr-1.lib libaprutil-1.lib libhttpd.lib msvcrt-ruby18.lib

.SUFFIXES: .c .obj

all : mod_ruby.so

.c.obj:
    cl /c $(CFLAGS) /Fo$@ $<

mod_ruby.so : $(OBJS)
    link /nologo /DLL /OUT:$@ /EXPORT:ruby_module $(OBJS) $(LDFLAGS) $(LIBS)

clean :
    -@del /Q *.obj *.so
パス等は環境に合わせて修正して下さい。
コマンドラインからnmakeでビルド(mingw32の場合はもう一工夫いります)。出来上がったmod_ruby.soをApacheのmodulesディレクトリに配置。httpd.confは適当に設定。 次にtDiary。最新版を svn co https://tdiary.svn.sourceforge.net/svnroot/tdiary/trunk/core tdiary
で取得して設定を済ませる。デフォルトの.htaccessはCGI版になっているので Options +ExecCGI

# if you run tDiary with symbolic link, use settings below.
#Options +FollowSymLinks

# if making anchor style as 'YYYYMMDD.html', add some settings below.
# SEE header of html_anchor.rb plugin.

#AddHandler cgi-script .rb
AddHandler ruby-script .rb
DirectoryIndex index.rb
AddType application/xml .rdf

<Files "*.rhtml*">
    deny from all
</Files>

<Files "tdiary.*">
    deny from all
</Files>

<Files update.rb>
    AuthName      tDiary
    AuthType      Basic
    AuthUserFile  /home/foo/.htpasswd
    Require user  foo
    SetHandler ruby-object
    RubyHandler Apache::RubyRun.instance
</Files>

<Files index.rb>
    SetHandler ruby-object
    RubyHandler Apache::RubyRun.instance
</Files>
以上の様に修正する。さて実行...とまでは良かったが冒頭で書いたエラーが発生。色々調べている内にERBのresult(binding)メソッドでエラーが発生している事が分かった。
しかもコマンドラインから実行すると現象は発生しないけど、Apacheから動かすとエラーが発生する。さらに調べた所、rhtmlファイル内にあるタブ文字(0x09)が影響している事が分かり、tDiaryのskelディレクトリ内にある全てのrhtmlファイルに対してタブ文字からスペースに置き換える作用を行うと正しく動作する。
これでようやくApache2/mod_rubyで動くtDiaryが完成した。
tdiary-apache2-modruby-win32
ただ、どうしてもタブ文字でおかしくなる原因が分からない。RubyKanjiCodeを指定しても駄目。お手上げです。

誰か原因知りませんか?
Posted at by



2008/06/19


面白いもの見つけた。(via ku's twitter post)
Tumblr, RMagick and a Photo Frame! - igvita.com

Over the course of the past year or so, I got myself into a habit of using Tumblr for storing insightful snippets and quotes from my daily endeavours on the internet. Usually, this entails saving a paragraph (too long for tweet, and not enough for del.icio.us) so I can revisit it later.

However, having Tumblr to save the quotes is nice, but I rarely (if ever) went back to read them. Pondering this dilemma, I spotted my Phillips digital photo frame, and a quick weekend project was born: Tumblr API, RMagick to render the images, and an SD card full of wisdom and quotes for easy consumption!

どっちかって言うと、書いてる理由が面白い。要は「Tumblrの引用は面白いけど一々見に行くのめんどいよね!ならって事でデジタルフォトスタンドを使っちゃおうと思った」て感じかな。

動かすにはrubyのImageMagick拡張rmagickが必要。
私はPhillips digital photo frameは持ってないけど、chumbyとか使うと良く似た事出来るのかな?
ソースはそのまま使えました。日本語はImageMagickがghostscriptを使ってるのでTrueTypeフォント直接指定すれば動きます。
--- render-tumblr.rb.orig   Sat Jun 07 19:32:46 2008
+++ render-tumblr.rb    Thu Jun 19 12:02:57 2008
@@ -62,7 +62,7 @@
        img.background_color = "transparent"
        img.pointsize = 23
        img.antialias = true
-       img.font = "Helvetica"
+       img.font = "c:\\windows\\fonts\\meiryo.ttc"
    end
    quote.write "text.png"
    
@@ -72,12 +72,12 @@
        img.background_color = "transparent"
        img.pointsize = 18
        img.antialias = true
-       img.font = "Helvetica"
+       img.font = "c:\\windows\\fonts\\meiryo.ttc"
    end
    source.write "source.png"  
 end
 
-doc = Hpricot.XML(open("http://igrigorik.tumblr.com/api/read?type=quote&num=5"))
+doc = Hpricot.XML(open("http://mattn.tumblr.com/api/read?type=quote&num=5"))
 (doc/'post').each do |post|
    text = CGI::unescapeHTML((post/'quote-text').inner_html).gsub(/&[^;]*;?/,'')
    source = CGI::unescapeHTML((post/'quote-source').inner_html).gsub(/\<.*?\>/, '').gsub(/&[^;]*;?/,'').strip

実行するとquotesディレクトリに以下の様なファイルが出来上がりました。
render-tumblr-34978042

render-tumblr-36274314

render-tumblr-37077963

render-tumblr-37111519

render-tumblr-38695135

面白いですね。あとは何とかしてこれをchumbyとかで動かせればいいのですが、いかんせんchumby持ってない!chumbyでrubyが動くかどうかも知らない。
って事で誰か後はよろしく。
Posted at by