Created
April 13, 2012 04:46
-
-
Save Shinpeim/2373763 to your computer and use it in GitHub Desktop.
https://gist.github.com/2367607 これのparseのとこ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Test::More; | |
use resize; | |
subtest "valid case (with option)" => sub{ | |
my $valid_uri = "http://example.com/scale/c.120x240.sushi.png"; | |
my $file_info = resize::parse_uri($valid_uri); | |
is $file_info->{option}, "c", "option"; | |
is $file_info->{width}, 120, "width"; | |
is $file_info->{height}, 240, "height"; | |
is $file_info->{ext}, "png", "ext"; | |
is $file_info->{original_filename}, "sushi.png", "original_filaname"; | |
$valid_uri = "http://example.com/scale/c.120x240.sushi.tabetai.png"; #case when filename includes "." | |
$file_info = resize::parse_uri($valid_uri); | |
is $file_info->{option}, "c", "option"; | |
is $file_info->{width}, 120, "width"; | |
is $file_info->{height}, 240, "height"; | |
is $file_info->{ext}, "png", "ext"; | |
is $file_info->{original_filename}, "sushi.tabetai.png", "original_filaname"; | |
}; | |
subtest "invalid case (with option)" => sub{ | |
my @invalid_uris = ( | |
"http://example.com/scale/c12.120x240.sushi.png", #invalid options | |
"http://example.com/scale/c.120aa240.sushi.png", #invalid dimension | |
"http://example.com/scale/c.120x240.sushi/hoge.png", #case when filename includes "/" | |
); | |
for my $invalid_uri (@invalid_uris) { | |
my $ret = resize::parse_uri($invalid_uri); | |
ok !$ret, "invalid dimension"; | |
} | |
}; | |
subtest "valid case (without option)" => sub{ | |
my $valid_uri = "http://example.com/scale/120x240.sushi.png"; | |
my $file_info = resize::parse_uri($valid_uri); | |
is $file_info->{option}, undef, "option"; | |
is $file_info->{width}, 120, "width"; | |
is $file_info->{height}, 240, "height"; | |
is $file_info->{ext}, "png", "ext"; | |
is $file_info->{original_filename}, "sushi.png", "original_filaname"; | |
$valid_uri = "http://example.com/scale/120x240.sushi.tabetai.png"; #case when filename includes "." | |
$file_info = resize::parse_uri($valid_uri); | |
is $file_info->{option}, undef, "option"; | |
is $file_info->{width}, 120, "width"; | |
is $file_info->{height}, 240, "height"; | |
is $file_info->{ext}, "png", "ext"; | |
is $file_info->{original_filename}, "sushi.tabetai.png", "original_filaname"; | |
}; | |
subtest "invalid case (without option)" => sub{ | |
my @invalid_uris = ( | |
"http://example.com/scale/120aa240.sushi.png", #invalid dimension | |
"http://example.com/scale/120x240.sushi/hoge.png", #case when filename includes "/" | |
); | |
for my $invalid_uri (@invalid_uris) { | |
my $ret = resize::parse_uri($invalid_uri); | |
ok !$ret, "invalid dimension"; | |
} | |
}; | |
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package resize; | |
use strict; | |
use warnings; | |
# http://example.com/scale/c.600x150.sushi.png | |
# http://example.com/scale/128x128.sushi.png | |
sub parse_uri{ | |
my $uri = shift; | |
return unless $uri =~ m{scale/([a-z]\.)?\d+x\d+\.[^/]+.[a-z]+$}; | |
my ($filename) = reverse split("/", $uri); | |
my $has_option = ($filename =~ m{^[a-z]} ? 1 : 0); | |
my @splitted_filename = split ('\.', $filename); | |
my $option = undef; | |
if ($has_option) { | |
$option = shift @splitted_filename; | |
} | |
my $dimensions = shift @splitted_filename; | |
my ($width, $height) = split "x", $dimensions; | |
my $original_filename = join(".", @splitted_filename); | |
my $ext = pop @splitted_filename; | |
return +{ | |
option => $option, | |
width => $width, | |
height => $height, | |
ext => $ext, | |
original_filename => $original_filename, | |
}; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment