File Coverage

blib/lib/Mojolicious/Plugin/GistGithubProxy.pm
Criterion Covered Total %
statement 20 28 71.4
branch 3 4 75.0
condition n/a
subroutine 4 5 80.0
pod 1 1 100.0
total 28 38 73.6


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::GistGithubProxy;
2              
3             # ABSTRACT: Mojolicious::Plugin::GistGithubProxy - a small proxy that can be useful when you embed gists in your website
4              
5 2     2   1434 use Mojo::Base 'Mojolicious::Plugin';
  2         6  
  2         17  
6              
7             our $VERSION = '0.01';
8              
9             our $GIST_URL_FORMAT = 'https://gist.github.com/%s/%s.js?file=%s';
10              
11             sub register {
12 2     2 1 86 my ($self, $app, $config) = @_;
13              
14             $app->hook(
15             after_render => sub {
16 5     5   41987 my ($c, $content, $format) = @_;
17              
18 5 50       23 return if !$format;
19 5 100       20 return if $format ne 'html';
20              
21 1         10 $$content =~ s{
22             https://gist\.github\.com/(.*?)/(.*?)\.js\?file=(.*?)"
23 1         6 }{$c->url_for( 'github-proxy-gist', user => $1, id => $2, file => $3 ) . '"';}xge;
24             }
25 2         22 );
26              
27             $app->routes->get( '/github/gist/:user/:id/*file', $config )->to( cb => sub {
28 2     2   26572 my $c = shift;
29              
30 2         9 $c->render_later;
31              
32 2         52 my $url = sprintf $GIST_URL_FORMAT, $c->param('user'), $c->param('id'), $c->param('file');
33              
34             $c->ua->get( $url => sub {
35 2         5235 my ($ua, $tx) = @_;
36              
37 2         20 my $body = $tx->res->body;
38 2         49 $body =~ s{
39             https://assets-cdn.github.com/assets/gist-embed-(.*?)\.css
40 0         0 }{$c->url_for('github-proxy-gist-asset', id => $1)}xmse;
41              
42 2         12 return $c->render( data => $body, format => 'js' );
43 2         181 });
44 2         46 })->name( 'github-proxy-gist' );
45              
46             $app->routes->get( '/github/gist/assets/:id' )->to( cb => sub {
47 0     0     my $c = shift;
48              
49 0           $c->render_later;
50              
51 0           my $url = sprintf q~https://assets-cdn.github.com/assets/gist-embed-%s.css~, $c->param('id');
52             $c->ua->get( $url => sub {
53 0           my ($ua, $tx) = @_;
54              
55 0           my $body = $tx->res->body;
56 0           return $c->render( data => $body, format => 'css' );
57 0           });
58 2         1111 })->name( 'github-proxy-gist-asset' );
59              
60             }
61              
62             1;
63              
64             __END__