Skip to content

Instantly share code, notes, and snippets.

@yappo
Created July 16, 2013 06:38
Show Gist options
  • Save yappo/6006302 to your computer and use it in GitHub Desktop.
Save yappo/6006302 to your computer and use it in GitHub Desktop.
# LTSV って言いたいだけな設問でマジひどいw
package Log;
use strict;
use warnings;
use Time::Piece;
sub new {
my($class, %opts) = @_;
bless { %opts }, $class;
}
sub _parse_request {
my $class = shift;
my($method, $path, $protocol) = $class->{req} =~ /\A([^\s]+)\s(.+)\s([^\s]+)\z/;
+{
method => $method,
path => $path,
protocol => $protocol,
};
}
sub method { ($_[0]->{request} //= $_[0]->_parse_request)->{method} }
sub path { ($_[0]->{request} //= $_[0]->_parse_request)->{path} }
sub protocol { ($_[0]->{request} //= $_[0]->_parse_request)->{protocol} }
sub uri {
my $class = shift;
$class->{uri} //= do {
my $request = $class->{request} // $class->_parse_request;
join('',
(1 ? 'http://' : 'https://'), # XXX: http or https check
$class->{host},
$request->{path}
);
};
}
sub time {
my $class = shift;
$class->{time} //= do{
gmtime($class->{epoch})->strftime('%Y-%m-%dT%H:%M:%S');
};
}
=pod
・req の値に含まれている HTTP メソッド名を返す method メソッド
 ・req の値に含まれているリクエストパスを返す path メソッド
 ・req の値に含まれているプロトコル名を返す protocol メソッド
 ・host と req の値からリクエストされた uri を組み立てて返すuri メソッド
 ・epoch が表している日付を YYYY-MM-DDThh:mm:ss というフォーマットの文字列に変換して返す time メソッド
   ・日付を扱うモジュールを用いてかまいません
   ・タイムゾーンは GMT として下さい
=cut
if ($0 =~ /Log.pm/) {
require Test::More;
Test::More->import;
my $log = Log->new(
host => '127.0.0.1',
user => 'frank',
epoch => '1372694390',
req => 'GET /apache_pb.gif HTTP/1.0',
status => '200',
size => '2326',
referer => 'http://www.hatena.ne.jp/',
);
is($log->method, 'GET');
is($log->path, '/apache_pb.gif');
is($log->protocol, 'HTTP/1.0');
is($log->uri, 'http://127.0.0.1/apache_pb.gif');
is($log->time, '2013-07-01T15:59:50');
done_testing();
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment