Fork me on GitHub

2009/03/18

はてな
twitterのOAuthベータが開始されたので、さっそく軽量Web Application Framework「MENTA」でOAuthする物を作ってみた。
構成も簡単で
│  .htaccess
│  menta.cgi
├─app
│  ├─controller
│  │      callback.pl
│  │      request.pl
│  ├─data
│  └─static
├─bin
├─lib
└─plugins
        session.pl
こんな感じ。トップページからrequest.plに飛び、OAuthのcallbackとしてcallback.plがキックされる。
要のrequest.plは以下の様な感じ
use MENTA::Controller;
use URI;
use OAuth::Lite::Consumer;

sub run {
    my $consumer = OAuth::Lite::Consumer->new(
        consumer_key       => config()->{application}->{consumer_key},
        consumer_secret    => config()->{application}->{consumer_secret},
        site               => 'http://twitter.com/',
        request_token_path => 'http://twitter.com/oauth/request_token',
        access_token_path  => 'http://twitter.com/oauth/access_token',
        authorize_path     => 'http://twitter.com/oauth/authorize',
    );

    my $request_token = $consumer->get_request_token();
    my $uri           = URI->new( $consumer->{authorize_path} );
    $uri->query(
        $consumer->gen_auth_query(
            "GET", "http://twitter.com/", $request_token
        )
    );
    redirect( $uri->as_string );
}

そしてcallback.plはこんな感じ
use utf8;
use MENTA::Controller;
use OAuth::Lite::Consumer;
use JSON;

sub run {
    my $consumer = OAuth::Lite::Consumer->new(
        consumer_key       => config()->{application}->{consumer_key},
        consumer_secret    => config()->{application}->{consumer_secret},
        site               => 'http://twitter.com/',
        request_token_path => 'http://twitter.com/oauth/request_token',
        access_token_path  => 'http://twitter.com/oauth/access_token',
        authorize_path     => 'http://twitter.com/oauth/authorize',
    );

    my $access_token = $consumer->get_access_token( token => param('oauth_token') );
    my $res = $consumer->request(
        method => 'POST',
        url    => q{http://twitter.com/statuses/update.json},
        token  => $access_token,
        params =>
          { status => 'おうっふー', token => $access_token },
    );
    if ( $res->is_success ) {
        my $status = from_json( $res->content );
        redirect( "http://twitter.com/"
              . $status->{user}->{screen_name}
              . "/status/"
              . $status->{id} );
    }
    else {
        redirect("http://twitter.com/");
    }
}
ちなみにこのアプリケーションを"Accept"にするとtwitteのステータスラインに「おうっふー」がポストされます。自分で作る方はtwitterのアプリケーション登録画面から得られるconsumer_keyとconsumer_secretをmenta.cgiのapplication項目に設定するのをお忘れなく。
簡単ですね!

2008/11/13

はてな
id:tokuhiromが良い物作ってくれたので、それを使ったブログエンジン書いてみた。
MENTA というウェブアプリケーションフレームワークをかいてみた - TokuLog 改めB日記

「CGI 用のウェブアプリケーションフレームワークにはどういうものが最適か」という問いに対する自分なりの解答。

http://d.hatena.ne.jp/tokuhirom/20081111/1226418572

名前は、「MENTOS(メントス)」。
mentos-weblog-engine
実質コードは以下の量くらい。

sub read_entry {
    my $file = shift;
    my $pubdate = strftime("%y-%m-%d %H:%M:%S", localtime((stat $file)[9])),
    my $content = read_file($file);
    $file =~ s!.*?([^/]+)\.txt$!$1!;
    $content =~ /^([^\n]+)\n\n(.*)/ms;
    return {
        id => $file,
        title => $1,
        pubdate => $pubdate,
        description => $2,
    }
}

# あなたのプログラム
sub do_index {
    my $id = param('id') || '';
    my $data_dir = config()->{application}->{data_dir};

    if ($id =~ /^(\d+)$/) {
        render('entry.html', read_entry("${data_dir}/${id}.txt"));
    } else {
        my @entries;
        for my $file (glob("${data_dir}/*.txt")) {
            push @entries, read_entry($file);
        }
        render('entries.html', \@entries);
    }
}

sub do_edit {
    my $id = param('id') || '';
    my $data_dir = config()->{application}->{data_dir};
    my $msg = '';
    my $entry = {};

    if ($ENV{'REQUEST_METHOD'} eq 'POST') {
        $entry->{id} = param('id') || '';
        $entry->{title} = param('title') || '';
        $entry->{description} = param('description') || '';
        $entry->{title} =~ s!\r!!g;
        $entry->{description} =~ s!\r!!g;
        my $password = param('password') || '';
        my $admin_password = config()->{application}->{password};
        if ($entry->{id} =~ /^(\d+)$/ && $password eq $admin_password) {
            $entry->{id} = time unless $entry->{id};
            utf8::decode($entry->{title});
            utf8::decode($entry->{description});
            write_file("${data_dir}/${id}.txt", $entry->{title}."\n\n".$entry->{description});
            redirect(config()->{application}->{blog_url});
            return;
        } else {
            $msg = 'パスワードが違います';
        }
    } else {
        $entry = read_entry("${data_dir}/${id}.txt") if -f "${data_dir}/${id}.txt";
    }

    render('edit.html', $entry, $msg);
}
MENTAを使ってどれだけ小さなコード量でブログエンジンが出来上がるかを試して見たかっただけなので、ブログと言いながらカテゴリやコメント、トラックバック等はありません。一応編集機能は持ち合わせています。
コードはcodereposのこの辺に置いときますので、適当に弄って遊んで下さい。
暇があれば、id:yappoのYacafiや、id:kazuhoのNanoAでも作ってみたいなぁ。
Posted at 19:11 in ソフトウェア::lang::perl | WriteBacks (2)
Tagged as: blog, menta, perl
Bookmarks: このエントリーのtweets add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip | add to buzzurl add to buzzurl | add to fc2bookmark add to fc2bookmark | add to Yahoo Bookmark add to Yahoo Bookmark | add to Pookmark add to Pookmark