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   96 use Moo;
  13         35  
  13         86  
4 13     13   5053 use Carp qw/croak/;
  13         50  
  13         849  
5 13     13   9380 use LWP::UserAgent;
  13         613168  
  13         500  
6 13     13   113 use HTTP::Request;
  13         31  
  13         390  
7 13     13   6246 use Compiled::Params::OO qw/cpo/;
  13         380430  
  13         137  
8              
9 13     13   1413 use Types::Standard qw/Str Any Object/;
  13         32  
  13         98  
10             our $validate;
11             BEGIN {
12 13     13   10824 $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 773 my ($self) = $validate->open_stream->(@_);
41 13         206 my $type = 'open_' . $self->stream_type;
42 13         72 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 82 my ($self) = $validate->open_file->(@_);
63              
64 13         347 my $stream = $self->stream;
65              
66 13 50       722 open ( my $fh, '<', $stream ) or croak "could not open file: $stream";
67              
68 13         83 my $content = do { local $/; <$fh> };
  13         68  
  13         4904  
69 13         171 close $fh;
70              
71 13         288 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 9785 my ($self, $feed) = $validate->write_file->(@_);
81 6         264 my $stream = $self->stream;
82 6 50       828 open my $FILE, ">", $stream or croak "could not open file: $stream";
83 6         126 print $FILE $feed;
84 6         348 close $FILE;
85             }
86              
87             1; # End of Feed::Data