File Coverage

blib/lib/Web/AssetLib/InputEngine/RemoteFile.pm
Criterion Covered Total %
statement 61 61 100.0
branch 14 24 58.3
condition n/a
subroutine 15 15 100.0
pod n/a
total 90 100 90.0


line stmt bran cond sub pod time code
1             package Web::AssetLib::InputEngine::RemoteFile;
2              
3 6     6   8667453 use Method::Signatures;
  6         50720  
  6         41  
4 6     6   2711 use Moose;
  6         294231  
  6         44  
5 6     6   25169 use Carp;
  6         12  
  6         373  
6              
7 6     6   2709 use HTTP::Request;
  6         93539  
  6         140  
8 6     6   3262 use LWP::UserAgent;
  6         94114  
  6         160  
9 6     6   82 use Digest::MD5 'md5_hex';
  6         10  
  6         459  
10              
11             extends 'Web::AssetLib::InputEngine';
12              
13             has 'ua' => (
14             is => 'rw',
15             isa => 'LWP::UserAgent',
16             lazy_build => 1
17             );
18              
19 6 50   6   4392 method _build_ua () {
  3     3   4  
  3         11  
20 3         38 my $ua = LWP::UserAgent->new;
21 3         6371 return $ua;
22             }
23              
24 6 50   6   98635 method load ($asset!) {
  8 50   8   10  
  8         26  
  8         14  
  8         25  
25             croak sprintf( "%s requires 'url' asset input_arg", ref($self) )
26 8 50       246 unless $asset->input_args->{url};
27              
28 8 100       230 if ( $asset->isPassthru ) {
29 1         29 $asset->link_path( $asset->input_args->{url} );
30 1         2 return;
31             }
32             else {
33             my $request
34 7         172 = $self->_buildRequest( url => $asset->input_args->{url} );
35 7         19743 my $contents = $self->_doRequest( request => $request );
36              
37 6         26334 my $digest = md5_hex $contents;
38 6         491 $self->addAssetToCache( $digest => $contents );
39              
40 6         58 $self->storeAssetContents(
41             asset => $asset,
42             digest => $digest,
43             contents => $contents
44             );
45             }
46             }
47              
48 6 50   6   11264 method _buildRequest (:$url!) {
  7 50   7   16  
  7 50       32  
  7         25  
  7         17  
  7         14  
  7         21  
  7         25  
49 7         65 return HTTP::Request->new( GET => $url );
50             }
51              
52 6 50   6   10077 method _doRequest (:$request!) {
  7 50   7   13  
  7 50       33  
  7         27  
  7         14  
  7         14  
  7         31  
  7         20  
53 7         248 my $res = $self->ua->request($request);
54 7 100       1172276 if ( $res->code == 200 ) {
55 6         90 return $res->decoded_content;
56             }
57             else {
58 1         10 croak $res->decoded_content;
59             }
60              
61             }
62              
63 6     6   976 no Moose;
  6         9  
  6         48  
64             1;
65              
66             =pod
67            
68             =encoding UTF-8
69            
70             =head1 NAME
71              
72             Web::AssetLib::InputEngine::RemoteFile - allows importing an asset via a URL
73              
74             =head1 SYNOPSIS
75              
76             my $library = My::AssetLib::Library->new(
77             input_engines => [
78             Web::AssetLib::InputEngine::RemoteFile->new()
79             ]
80             );
81              
82             my $asset = Web::AssetLib::Asset->new(
83             type => 'javascript',
84             input_engine => 'RemoteFile',
85             input_args => { url => "http://somecdn.com/asset.js", }
86             );
87              
88             $library->compile( asset => $asset );
89              
90             =head1 USAGE
91              
92             No configuration required. Simply instantiate, and include in your library's
93             list of input engines.
94              
95             Assets using the RemoteFile input engine must provide C<< url >> input arg.
96              
97             =head1 SEE ALSO
98              
99             L<Web::AssetLib::InputEngine>
100              
101             L<Web::AssetLib::InputEngine::LocalFile>
102              
103             L<Web::AssetLib::InputEngine::Content>
104              
105             =head1 AUTHOR
106            
107             Ryan Lang <rlang@cpan.org>
108              
109             =cut