Skip to content

Instantly share code, notes, and snippets.

@hidek
Created May 20, 2010 15:00
Show Gist options
  • Select an option

  • Save hidek/407664 to your computer and use it in GitHub Desktop.

Select an option

Save hidek/407664 to your computer and use it in GitHub Desktop.
package Foo;
use strict;
use warnings;
our $VERSION = '0.01';
use Plack::Request;
use Plack::Response;
use OAuth::Lite::Consumer;
use OAuth::Lite::Token;
use OAuth::Lite::Util qw/parse_auth_header/;
use JSON;
use Encode qw(encode from_to);
use Encode::JP::Mobile;
use HTTP::MobileAgent;
use URI;
use URI::QueryParam;
sub new {
my ($class, %args) = @_;
my $consumer = OAuth::Lite::Consumer->new(
consumer_key => $args{consumer_key},
consumer_secret => $args{consumer_secret},
);
bless {consumer => $consumer}, $class;
}
sub run {
my ($self, $env) = @_;
my $req = Plack::Request->new($env);
my ($realm, $params)
= parse_auth_header($req->headers->header('authorization'));
my $token = OAuth::Lite::Token->new(
token => $params->{oauth_token},
secret => $params->{oauth_token_secret},
);
my $uri = URI->new('http://sb.app.mbga.jp/api/restful/v1/people/@me/@self');
my $viewer_id = $req->query_parameters->get('opensocial_viewer_id');
$uri->query_param_append('xoauth_requestor_id' => $viewer_id);
my $method = 'GET';
my $json = $self->{consumer}->request(
method => $method,
url => $uri,
token => $token,
params => $uri->query_form_hash,
);
my $people = decode_json($json->content);
my $nickname = encode('utf8' => $people->{person}{nickname});
my $body = <<"HTML";
<html>
<body>
Hello $nickname !
</body>
</html>
HTML
my $agent = HTTP::MobileAgent->new($req->headers);
my $enc;
if ($agent->is_docomo) {
$enc = 'x-sjis-imode';
} elsif ($agent->is_ezweb) {
$enc = 'x-sjis-kddi-auto';
} elsif ($agent->is_vodafone) {
$enc = 'x-utf8-softbank';
}
from_to($body, 'utf8' => $enc);
my $res = Plack::Response->new(200, ['Content-Type' => 'text/html']);
$res->body($body);
return $res->finalize;
}
1;
__END__
=head1 NAME
Foo -
=head1 SYNOPSIS
use Foo;
=head1 DESCRIPTION
Foo is
=head1 AUTHOR
Hideo Kimura E<lt>hide@hide-k.netE<gt>
=head1 SEE ALSO
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment