Skip to content

Commit

Permalink
use Mojo::Util more
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 2, 2011
1 parent 3597a41 commit 98cd58b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 38 deletions.
13 changes: 5 additions & 8 deletions lib/Mojo/Path.pm
Expand Up @@ -5,7 +5,7 @@ use overload
'""' => sub { shift->to_string },
fallback => 1;

use Mojo::Util qw/url_escape url_unescape/;
use Mojo::Util qw/encode url_escape url_unescape/;
use Mojo::URL;

has [qw/leading_slash trailing_slash/];
Expand Down Expand Up @@ -114,13 +114,10 @@ sub to_string {
my $self = shift;

# Escape
my @path;
for my $part (@{$self->parts}) {
my $escaped = $part;
utf8::encode $escaped;
push @path,
url_escape($escaped, "$Mojo::URL::UNRESERVED$Mojo::URL::SUBDELIM\:\@");
}
my @path = map {
url_escape(encode('UTF-8', $_),
"$Mojo::URL::UNRESERVED$Mojo::URL::SUBDELIM\:\@")
} @{$self->parts};

# Format
my $path = join '/', @path;
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -6,7 +6,7 @@ use Mojo::DOM;
use Mojo::IOLoop;
use Mojo::Transaction::HTTP;
use Mojo::UserAgent;
use Mojo::Util 'decode';
use Mojo::Util qw/decode encode/;

has description => <<'EOF';
Perform HTTP 1.1 request.
Expand Down Expand Up @@ -139,7 +139,7 @@ sub run {

# Error
my ($message, $code) = $tx->error;
utf8::encode $url;
$url = encode 'UTF-8', $url;
warn qq/Problem loading URL "$url". ($message)\n/ if $message && !$code;

# Charset
Expand All @@ -153,8 +153,7 @@ sub run {

sub _say {
return unless length(my $value = shift);
utf8::encode $value;
say $value;
say encode('UTF-8', $value);
}

sub _select {
Expand Down
9 changes: 4 additions & 5 deletions lib/Mojolicious/Command/inflate.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base 'Mojo::Command';

use Getopt::Long 'GetOptions';
use Mojo::Loader;
use Mojo::Util 'encode';

has description => <<'EOF';
Inflate embedded files to real files.
Expand Down Expand Up @@ -40,11 +41,9 @@ sub run {
# Generate
my $all = $self->get_all_data($class);
for my $file (keys %$all) {
my $prefix = $file =~ /\.\w+\.\w+$/ ? $templates : $public;
my $path = $self->rel_file("$prefix/$file");
my $content = $all->{$file};
utf8::encode $content;
$self->write_file($path, $content);
my $prefix = $file =~ /\.\w+\.\w+$/ ? $templates : $public;
my $path = $self->rel_file("$prefix/$file");
$self->write_file($path, encode('UTF-8', $all->{$file}));
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugin/EPLRenderer.pm
Expand Up @@ -2,7 +2,7 @@ package Mojolicious::Plugin::EPLRenderer;
use Mojo::Base 'Mojolicious::Plugin';

use Mojo::Template;
use Mojo::Util 'md5_sum';
use Mojo::Util qw/encode md5_sum/;

# "Clever things make people feel stupid and unexpected things make them feel
# scared."
Expand All @@ -18,7 +18,7 @@ sub register {
my $inline = $options->{inline};
my $path = $r->template_path($options);
if (defined $inline) {
utf8::encode $inline;
$inline = encode('UTF-8', $inline);
$path = md5_sum $inline;
}
return unless defined $path;
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious/Plugin/EPRenderer.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base 'Mojolicious::Plugin';

use Mojo::Loader;
use Mojo::Template;
use Mojo::Util 'md5_sum';
use Mojo::Util qw/encode md5_sum/;

# "What do you want?
# I'm here to kick your ass!
Expand Down Expand Up @@ -31,8 +31,7 @@ sub register {
# Generate name
my $path = $r->template_path($options) || $options->{inline};
return unless defined $path;
my $id = join ', ', $path, sort keys %{$c->stash};
utf8::encode $id;
my $id = encode 'UTF-8', join(', ', $path, sort keys %{$c->stash});
my $key = $options->{cache} = md5_sum $id;

# Cache
Expand Down
9 changes: 3 additions & 6 deletions lib/Mojolicious/Plugin/JSONConfig.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base 'Mojolicious::Plugin::Config';

use Mojo::JSON;
use Mojo::Template;
use Mojo::Util 'encode';

# "And so we say goodbye to our beloved pet, Nibbler, who's gone to a place
# where I, too, hope one day to go. The toilet."
Expand Down Expand Up @@ -44,12 +45,8 @@ sub render {
$prepend .= q/use Mojo::Base -strict;/;

# Render
my $mt = Mojo::Template->new($conf->{template} || {});
$mt->prepend($prepend);
$content = $mt->render($content, $app);
utf8::encode $content;

return $content;
my $mt = Mojo::Template->new($conf->{template} || {})->prepend($prepend);
return encode 'UTF-8', $mt->render($content, $app);
}

1;
Expand Down
15 changes: 5 additions & 10 deletions lib/Test/Mojo.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base -base;
use Mojo::IOLoop;
use Mojo::Message::Response;
use Mojo::UserAgent;
use Mojo::Util 'decode';
use Mojo::Util qw/decode encode/;
use Test::More ();

has ua => sub { Mojo::UserAgent->new->ioloop(Mojo::IOLoop->singleton) };
Expand Down Expand Up @@ -231,9 +231,7 @@ sub post_form_ok {

$self->tx($self->ua->post_form($url, @_));
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $desc = "post $url";
utf8::encode $desc;
Test::More::ok $self->tx->is_finished, $desc;
Test::More::ok $self->tx->is_finished, encode('UTF-8', "post $url");

return $self;
}
Expand Down Expand Up @@ -313,8 +311,6 @@ sub websocket_ok {
my $self = shift;
my $url = shift;

my $desc = "websocket $url";
utf8::encode $desc;
$self->{messages} = [];
$self->{finished} = 0;
$self->ua->websocket(
Expand All @@ -328,7 +324,8 @@ sub websocket_ok {
);
Mojo::IOLoop->start;
local $Test::Builder::Level = $Test::Builder::Level + 1;
Test::More::ok $self->tx->res->code eq 101, $desc;
Test::More::ok $self->tx->res->code eq 101,
encode('UTF-8', "websocket $url");

return $self;
}
Expand Down Expand Up @@ -362,10 +359,8 @@ sub _request_ok {
$self->tx($self->ua->$method($url, %$headers, $body));
local $Test::Builder::Level = $Test::Builder::Level + 2;
my ($error, $code) = $self->tx->error;
my $desc = "$method $url";
utf8::encode $desc;
Test::More::diag $error if !(my $ok = !$error || $code) && $error;
Test::More::ok $ok, $desc;
Test::More::ok $ok, encode('UTF-8', "$method $url");

return $self;
}
Expand Down

0 comments on commit 98cd58b

Please sign in to comment.