Skip to content

Commit

Permalink
make the database stuff work
Browse files Browse the repository at this point in the history
  • Loading branch information
jrockway committed Apr 6, 2009
1 parent 9e2c759 commit fe28fc2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/Unshorten.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Unshorten with MooseX::Getopt with MooseX::Runnable with HTTP::Engine::Rol

my $long = eval { $self->model->unshorten($short) };
if(!$long || $@){
$self->model->delete("$short"); # just to make sure bad data doesn't stay
return HTTP::Engine::Response->new(
code => 500,
content_type => 'text/plain',
Expand Down
23 changes: 12 additions & 11 deletions lib/Unshorten/Model.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,40 @@ class Unshorten::Model extends KiokuX::Model {
use AnyEvent::HTTP qw(http_head);

method unshorten(Uri $url) {
return $self->_lookup_or_cache($url, sub {
$self->_lookup(@_),
return $self->_resolve_or_cache($url, sub {
$self->_resolve_url(@_),
});
}

method _lookup(Uri $url) {
method _resolve_url(Uri $url) {
my $done = AnyEvent->condvar;

http_head $url, sub {
my ($data, $headers) = @_;
my $long_url = eval { URI->new($headers->{URL}) };
confess 'Failed to get a URL' unless $url;
confess 'Failed to get a URL' unless $long_url;
$done->send($long_url);
};

return $done->recv;
}

method _lookup_or_cache(Uri $url, CodeRef $shortener) {
method _resolve_or_cache(Uri $url, CodeRef $expander) {
my $scope = $self->new_scope;
my $shortened = $self->lookup("$url");
return $shortened if $shortened;
my $expanded = $self->lookup("$url");
return $expanded->{redirects_to} if $expanded;

$shortened = $shortener->($url);
# needs to be expanded
$expanded = $expander->($url);

# "Unknown reason" because the shortener should die if it knows
# the reason
confess "Unable to shorten '$url': Unknown reason"
unless $shortened;
unless $expanded;

$self->store("$url" => $shortened);
$self->insert("$url" => { redirects_to => $expanded });

return $shortened;
return $expanded;
}

};

0 comments on commit fe28fc2

Please sign in to comment.