File Coverage

blib/lib/Feed/Data/Stream.pm
Criterion Covered Total %
statement 35 49 71.4
branch 2 8 25.0
condition n/a
subroutine 10 12 83.3
pod 0 5 0.0
total 47 74 63.5


line stmt bran cond sub pod time code
1             package Feed::Data::Stream;
2              
3 13     13   100 use Moo;
  13         31  
  13         91  
4 13     13   5120 use Carp qw/croak/;
  13         32  
  13         814  
5 13     13   10487 use LWP::UserAgent;
  13         570605  
  13         519  
6 13     13   133 use HTTP::Request;
  13         31  
  13         1595  
7 13     13   6436 use Compiled::Params::OO qw/cpo/;
  13         388192  
  13         153  
8              
9 13     13   1418 use Types::Standard qw/Str Any Object/;
  13         30  
  13         106  
10             our $validate;
11             BEGIN {
12 13     13   11903 $validate = cpo(
13             open_stream => [Object],
14             open_url => [Object],
15             open_file => [Object],
16             open_string => [Object],
17             write_file => [Object, Str]
18             );
19             }
20              
21             has 'stream' => (
22             is => 'rw',
23             isa => Str,
24             lazy => 1,
25             default => q{}
26             );
27              
28             has 'stream_type' => (
29             is => 'ro',
30             isa => Str,
31             default => sub {
32             my $self = shift;
33             return 'url' if $self->stream =~ m{^http}xms;
34             return 'string' if $self->stream =~ m{\<\?xml}xms;
35             return 'file' if $self->stream =~ m{(\.xml|\.html|\.txt|\.json|\.csv|\.yml)}xms;
36             }
37             );
38              
39             sub open_stream {
40 13     13 0 774 my ($self) = $validate->open_stream->(@_);
41 13         206 my $type = 'open_' . $self->stream_type;
42 13         74 return $self->$type;
43             }
44              
45             sub open_url {
46 0     0 0 0 my ($self) = $validate->open_url->(@_);
47 0         0 my $stream = $self->stream;
48 0         0 my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
49 0         0 $ua->env_proxy;
50 0         0 $ua->agent("Mozilla/8.0");
51 0         0 my $req = HTTP::Request->new( GET => $stream );
52 0         0 $req->header( 'Accept-Encoding', 'gzip' );
53 0 0       0 my $res = $ua->request($req) or croak "Failed to fetch URI: $stream";
54 0 0       0 if ( $res->code == 410 ) {
55 0         0 croak "This feed has been permantly removed";
56             }
57 0         0 my $content = $res->decoded_content(charset => 'utf8');
58 0         0 return \$content;
59             }
60              
61             sub open_file {
62 13     13 0 97 my ($self) = $validate->open_file->(@_);
63              
64 13         347 my $stream = $self->stream;
65              
66 13 50       818 open ( my $fh, '<', $stream ) or croak "could not open file: $stream";
67              
68 13         43 my $content = do { local $/; <$fh> };
  13         72  
  13         4995  
69 13         219 close $fh;
70              
71 13         255 return \$content;
72             }
73              
74             sub open_string {
75 0     0 0 0 my ($self) = $validate->open_string(@_);
76 0         0 return shift->stream;
77             }
78              
79             sub write_file {
80 6     6 0 10620 my ($self, $feed) = $validate->write_file->(@_);
81 6         239 my $stream = $self->stream;
82 6 50       884 open my $FILE, ">", $stream or croak "could not open file: $stream";
83 6         130 print $FILE $feed;
84 6         391 close $FILE;
85             }
86              
87             1; # End of Feed::Data