Skip to content

Commit

Permalink
add Plugin::Mobile::EmojiFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
nekokak committed Oct 24, 2009
1 parent 9ae6c40 commit bff69c3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ todo:
- # docomo guid filter
- HTTP::MobileAgentもつかえるように
- encode mobilejp
- emoji filter
- # emoji filter
- Plugin::Mobileにしてみるか

39 changes: 39 additions & 0 deletions lib/Kamui/Plugin/Mobile/EmojiFilter.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package Kamui::Plugin::Mobile::EmojiFilter;
use Kamui;
use base 'Kamui::Plugin';

sub register_method {
+{
mobile_emoji_filter => sub {
my $c = shift;
Kamui::Plugin::Mobile::EmojiFilter::Backend->new($c);
},
};
}

package Kamui::Plugin::Mobile::EmojiFilter::Backend;
use Kamui;
use Scalar::Util ();

sub new {
my ($class, $c) = @_;
bless {c => $c}, $class;
}

sub c { $_[0]->{c} }

sub finalize {
my ($self, $res) = @_;

if ( $res && $res->status == 200
&& $res->content_type =~ /html/
&& not( Scalar::Util::blessed( $res->body ) )
&& $res->body )
{
(my $body = $res->body) =~ s{ \{ (?:emoji|u): ([0-9A-F]{4}) \} }{ pack('U*', hex $1) }geixms;
$res->body($body);
}
}

1;

58 changes: 58 additions & 0 deletions t/030_plugin/mobile/emoji_filter.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use t::Utils;
use Test::Declare;
use Kamui::Web::Context;
use Mock::Web::Handler;

plan tests => blocks;

describe 'emoji filter tests' => run {
init {
my $plugins = [qw/Mobile::EmojiFilter/];
Kamui::Web::Context->load_plugins($plugins);
};

test 'emoji filter' => run {
my $env = +{
REQUEST_METHOD => 'GET',
SCRIPT_NAME => '/',
};

my $c = Kamui::Web::Context->new(
env => $env,
app => 'Mock::Web::Handler',
);

my $res = $c->res;
$res->status('200');
$res->headers([ 'Content-Type' => 'text/html' ]);
$res->body('{emoji:E21E}');

$c->mobile_emoji_filter; # call initializer.
$c->finalize($res);

is $res->body, "\x{e21e}";
};

test 'emoji filter' => run {
my $env = +{
REQUEST_METHOD => 'GET',
SCRIPT_NAME => '/',
};

my $c = Kamui::Web::Context->new(
env => $env,
app => 'Mock::Web::Handler',
);

my $res = $c->res;
$res->status('200');
$res->headers([ 'Content-Type' => 'text/html' ]);
$res->body('{emozi:E21E}');

$c->mobile_emoji_filter; # call initializer.
$c->finalize($res);

is $res->body, '{emozi:E21E}';
};
};

0 comments on commit bff69c3

Please sign in to comment.