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   1472 use Mojo::Base 'Mojolicious::Plugin';
  2         5  
  2         15  
6              
7             our $VERSION = '0.02';
8              
9             our $GIST_URL_FORMAT = 'https://gist.github.com/%s/%s.js?file=%s';
10              
11             sub register {
12 2     2 1 82 my ($self, $app, $config) = @_;
13              
14             $app->hook(
15             after_render => sub {
16 5     5   44893 my ($c, $content, $format) = @_;
17              
18 5 50       22 return if !$format;
19 5 100       20 return if $format ne 'html';
20              
21 1         12 $$content =~ s{
22             https://gist\.github\.com/(.*?)/(.*?)\.js\?file=(.*?)"
23 1         5 }{$c->url_for( 'github-proxy-gist', user => $1, id => $2, file => $3 ) . '"';}xge;
24             }
25 2         22 );
26              
27             $app->routes->get( '/github/gist-assets/:id' )->to( cb => sub {
28 0     0   0 my $c = shift;
29              
30 0         0 $c->render_later;
31              
32 0         0 my $url = sprintf q~https://assets-cdn.github.com/assets/gist-embed-%s.css~, $c->param('id');
33             $c->ua->get( $url => sub {
34 0         0 my ($ua, $tx) = @_;
35              
36 0         0 my $body = $tx->res->body;
37 0         0 return $c->render( data => $body, format => 'css' );
38 0         0 });
39 2         46 })->name( 'github-proxy-gist-asset' );
40              
41             $app->routes->get( '/github/gist/:user/:id/*file', $config )->to( cb => sub {
42 2     2   28701 my $c = shift;
43              
44 2         10 $c->render_later;
45              
46 2         51 my $url = sprintf $GIST_URL_FORMAT, $c->param('user'), $c->param('id'), $c->param('file');
47              
48             $c->ua->get( $url => sub {
49 2         5496 my ($ua, $tx) = @_;
50              
51 2         11 my $body = $tx->res->body;
52 2         47 $body =~ s{
53             https://assets-cdn.github.com/assets/gist-embed-(.*?)\.css
54 0         0 }{$c->url_for('github-proxy-gist-asset', id => $1)}xmse;
55              
56 2         13 return $c->render( data => $body, format => 'js' );
57 2         150 });
58 2         1041 })->name( 'github-proxy-gist' );
59             }
60              
61             1;
62              
63             __END__