Skip to content

Commit

Permalink
fixed small charset bug in get command
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 23, 2012
1 parent 2fe9722 commit 496d4e1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,7 +1,8 @@

3.13 2012-07-22
3.13 2012-07-23
- Improved Mojolicious::Plugin::Config log messages. (jberger)
- Improved documentation.
- Fixed small charset bug in get command.

3.12 2012-07-20
- Deprecated Mojo::Home->app_class.
Expand Down
33 changes: 12 additions & 21 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -6,7 +6,6 @@ use Mojo::DOM;
use Mojo::IOLoop;
use Mojo::JSON;
use Mojo::JSON::Pointer;
use Mojo::Transaction::HTTP;
use Mojo::UserAgent;
use Mojo::Util qw(decode encode);

Expand Down Expand Up @@ -45,7 +44,7 @@ sub run {
# Options
local @ARGV = @_;
my ($method, $content, @headers) = ('GET', '');
my ($charset, $redirect, $verbose) = 0;
my ($charset, $redirect, $verbose);
GetOptions(
'C|charset=s' => sub { $charset = $_[1] },
'c|content=s' => sub { $content = $_[1] },
Expand All @@ -58,15 +57,10 @@ sub run {

# Headers
my %headers;
for my $header (@headers) {
next unless $header =~ /^\s*([^:]+)\s*:\s*([^:]+)\s*$/;
$headers{$1} = $2;
}
/^\s*([^:]+)\s*:\s*([^:]+)\s*$/ and $headers{$1} = $2 for @headers;

# URL and selector
my $url = shift @ARGV;
die $self->usage unless $url;
$url = decode 'UTF-8', $url;
die $self->usage unless my $url = decode 'UTF-8', shift @ARGV // '';
my $selector = shift @ARGV;

# Fresh user agent
Expand All @@ -80,8 +74,7 @@ sub run {
else { $ua->app($ENV{MOJO_APP} || 'Mojo::HelloWorld') }

# Start
my $v;
my $buffer = '';
my $v = my $buffer = '';
$ua->on(
start => sub {
my $tx = pop;
Expand Down Expand Up @@ -110,17 +103,16 @@ sub run {
warn "HTTP/$version $code $message\n$res_headers\n\n";

# Finished
$v = 0;
$v = undef;
};

# Progress
$tx->res->on(progress => sub { $cb->(shift) });
$tx->res->on(progress => $cb);

# Stream content
$tx->res->body(
sub {
my $res = shift;
$cb->($res);
$cb->(my $res = shift);

# Ignore intermediate content
return if $redirect && $res->is_status_class(300);
Expand All @@ -133,9 +125,8 @@ sub run {
);

# Get
my $tx = $ua->build_tx($method, $url, \%headers, $content);
STDOUT->autoflush(1);
$tx = $ua->start($tx);
my $tx = $ua->start($ua->build_tx($method, $url, \%headers, $content));

# Error
my ($message, $code) = $tx->error;
Expand All @@ -145,17 +136,17 @@ sub run {
# JSON Pointer
return unless $selector;
return _json($buffer, $selector)
if ($tx->res->headers->content_type || '') =~ /JSON/i;
if ($tx->res->headers->content_type || '') =~ /json/i;

# Selector
_select($buffer, $charset // $tx->res->content->charset, $selector);
_select($buffer, $selector, $charset // $tx->res->content->charset);
}

sub _json {
my $json = Mojo::JSON->new;
return unless my $data = $json->decode(shift);
return unless defined($data = Mojo::JSON::Pointer->get($data, shift));
ref $data ~~ ['HASH', 'ARRAY'] ? say($json->encode($data)) : _say($data);
ref $data ~~ [qw(HASH ARRAY)] ? say($json->encode($data)) : _say($data);
}

sub _say {
Expand All @@ -164,7 +155,7 @@ sub _say {
}

sub _select {
my ($buffer, $charset, $selector) = @_;
my ($buffer, $selector, $charset) = @_;

# Find
my $dom = Mojo::DOM->new->charset($charset)->parse($buffer);
Expand Down

0 comments on commit 496d4e1

Please sign in to comment.