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