| 1 | package Feed::Data::Stream; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Carp qw/croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Compiled::Params::OO qw/cpo/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Types::Standard qw/Str Any Object/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | our $validate; | |||||||||
| 11 | BEGIN { | ||||||||||
| 12 | 1 | ▶ | $validate = cpo( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 1 | 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 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 33 | 0 | ▶ | return 'url' if $self->stream =~ m{^http}xms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | ▶ | return 'string' if $self->stream =~ m{\<\?xml}xms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | ▶ | return 'file' if $self->stream =~ m{(\.xml|\.html|\.txt|\.json|\.csv|\.yml)}xms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | } | ||||||||||
| 37 | 1 | ); | |||||||||
| 38 | |||||||||||
| 39 | sub open_stream { | ||||||||||
| 40 | 0 | ▶ | my ($self) = $validate->open_stream->(@_); | ||||||||
|
Subroutine
open_stream: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 41 | 0 | my $type = 'open_' . $self->stream_type; | |||||||||
| 42 | 0 | return $self->$type; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | sub open_url { | ||||||||||
| 46 | 0 | ▶ | my ($self) = $validate->open_url->(@_); | ||||||||
|
Subroutine
open_url: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 47 | 0 | my $stream = $self->stream; | |||||||||
| 48 | 0 | my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 }); | |||||||||
| 49 | 0 | $ua->env_proxy; | |||||||||
| 50 | 0 | $ua->agent("Mozilla/8.0"); | |||||||||
| 51 | 0 | my $req = HTTP::Request->new( GET => $stream ); | |||||||||
| 52 | 0 | $req->header( 'Accept-Encoding', 'gzip' ); | |||||||||
| 53 | 0 | ▶ | my $res = $ua->request($req) or croak "Failed to fetch URI: $stream"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | ▶ | if ( $res->code == 410 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | croak "This feed has been permantly removed"; | |||||||||
| 56 | } | ||||||||||
| 57 | 0 | my $content = $res->decoded_content(charset => 'utf8'); | |||||||||
| 58 | 0 | return \$content; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | sub open_file { | ||||||||||
| 62 | 0 | ▶ | my ($self) = $validate->open_file->(@_); | ||||||||
|
Subroutine
open_file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 63 | |||||||||||
| 64 | 0 | my $stream = $self->stream; | |||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | open ( my $fh, '<', $stream ) or croak "could not open file: $stream"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | |||||||||||
| 68 | 0 | my $content = do { local $/; <$fh> }; | |||||||||
| 69 | 0 | close $fh; | |||||||||
| 70 | |||||||||||
| 71 | 0 | return \$content; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | sub open_string { | ||||||||||
| 75 | 0 | ▶ | my ($self) = $validate->open_string(@_); | ||||||||
|
Subroutine
open_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 76 | 0 | return shift->stream; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | sub write_file { | ||||||||||
| 80 | 0 | ▶ | my ($self, $feed) = $validate->write_file->(@_); | ||||||||
|
Subroutine
write_file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 81 | 0 | my $stream = $self->stream; | |||||||||
| 82 | 0 | ▶ | open my $FILE, ">", $stream or croak "could not open file: $stream"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | print $FILE $feed; | |||||||||
| 84 | 0 | close $FILE; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 1 | 1; # End of Feed::Data | |||||||||