| 1 | # -*- perl -*- | ||||||||||||
| 2 | ##---------------------------------------------------------------------------- | ||||||||||||
| 3 | ## Telegram API - ~/lib/Net/API/Telegram/Response.pm | ||||||||||||
| 4 | ## Version 0.2 | ||||||||||||
| 5 | ## Copyright(c) 2019 Jacques Deguest | ||||||||||||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||
| 7 | ## Created 2019/05/29 | ||||||||||||
| 8 | ## Modified 2019/06/02 | ||||||||||||
| 9 | ## All rights reserved | ||||||||||||
| 10 | ## | ||||||||||||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 12 | ## under the same terms as Perl itself. | ||||||||||||
| 13 | ##---------------------------------------------------------------------------- | ||||||||||||
| 14 | package Net::API::Telegram::Response; | ||||||||||||
| 15 | BEGIN | ||||||||||||
| 16 | { | ||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 1 | ▶ | use HTTP::Status (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 1 | ▶ | use IO::File; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | ▶ | our( $VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 1 | our( $VERBOSE, $DEBUG ); | |||||||||||
| 22 | 1 | @ISA = qw( Module::Generic ); | |||||||||||
| 23 | 1 | @EXPORT = qw( ); | |||||||||||
| 24 | 1 | %EXPORT_TAGS = (); | |||||||||||
| 25 | 1 | @EXPORT_OK = qw( ); | |||||||||||
| 26 | 1 | $VERSION = '0.2'; | |||||||||||
| 27 | 1 | ▶ | use Devel::Confess; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 28 | }; | ||||||||||||
| 29 | |||||||||||||
| 30 | { | ||||||||||||
| 31 | ## https://core.telegram.org/bots/api | ||||||||||||
| 32 | 1 | $DEBUG = 3; | |||||||||||
| 33 | 1 | $VERBOSE = 0; | |||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | sub init | ||||||||||||
| 37 | { | ||||||||||||
| 38 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
init: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 39 | 0 | ▶ | my $resp = shift( @_ ) || return( $self->error( "No server response was provided." ) ); | ||||||||||
|
Condition0%0/2
shift @_ || (return $self->error('No server response was provided.'))
MC/DC0%0/1
shift @_ || (return $self->error('No server response was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||
| 40 | 0 | ▶ | return( $self->error( "Object provided ($resp) is not a HTTP::Response object." ) ) if( !UNIVERSAL::isa( $resp, 'HTTP::Response' ) ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 0 | $self->{ 'data' } = ''; | |||||||||||
| 42 | 0 | $self->SUPER::init( @_ ); | |||||||||||
| 43 | 0 | ▶ | if( !$self->{ 'data' } ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | { | ||||||||||||
| 45 | 0 | ▶ | $self->{ 'data' } = $self->data2json( $resp->decoded_content ) || | ||||||||||
|
Condition0%0/2
$self->data2json($resp->decoded_content) || (return $self->error('Unable to parse the json data received from server: ', $self->error))
MC/DC0%0/1
$self->data2json($resp->decoded_content) || (return $self->error('Unable to parse the json data received from server: ', $self->error))
$self->data2json($resp->decoded_content)
Truth table0%0/2
A
A= $self->data2json($resp->decoded_content)
| |||||||||||||
| 46 | return( $self->error( "Unable to parse the json data received from server: ", $self->error ) ); | ||||||||||||
| 47 | } | ||||||||||||
| 48 | 0 | $self->{ 'resp' } = $resp; | |||||||||||
| 49 | 0 | return( $self ); | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | sub data2json | ||||||||||||
| 53 | { | ||||||||||||
| 54 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
data2json: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 55 | 0 | my $data = shift( @_ ); | |||||||||||
| 56 | 0 | my $unescape = shift( @_ ); | |||||||||||
| 57 | 0 | ▶ | return( $self->error( "No data provided to decode into json." ) ) if( !length( $data ) ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | ▶ | if( $unescape ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | { | ||||||||||||
| 60 | 0 | $data =~ s/\\\\r\\\\n/\n/gs; | |||||||||||
| 61 | 0 | $data =~ s/^\"|\"$//gs; | |||||||||||
| 62 | 0 | $data =~ s/\"\[|\]\"//gs; | |||||||||||
| 63 | } | ||||||||||||
| 64 | 0 | my $json; | |||||||||||
| 65 | eval | ||||||||||||
| 66 | 0 | { | |||||||||||
| 67 | 0 | ▶ | local $SIG{ '__WARN__' } = sub{ }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 68 | 0 | ▶ | local $SIG{ '__DIE__' } = sub{ }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 69 | 0 | $json = $self->{ 'json' }->decode( $data ); | |||||||||||
| 70 | }; | ||||||||||||
| 71 | 0 | ▶ | if( $@ ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | { | ||||||||||||
| 73 | 0 | my $fh = File::Temp->new( SUFFIX => '.js' ); | |||||||||||
| 74 | 0 | my $file = $fh->filename; | |||||||||||
| 75 | 0 | ▶ | my $io = IO::File->new( ">$file" ) || return( $self->error( "Unable to write to file $file: $!" ) ); | ||||||||||
|
Condition0%0/2
'IO::File'->new(">$file") || (return $self->error("Unable to write to file ${file}: $!"))
MC/DC0%0/1
'IO::File'->new(">$file") || (return $self->error("Unable to write to file ${file}: $!"))
'IO::File'->new(">$file")
Truth table0%0/2
A
A= 'IO::File'->new(">$file")
| |||||||||||||
| 76 | 0 | $io->binmode( ":utf8" ); | |||||||||||
| 77 | 0 | $io->autoflush( 1 ); | |||||||||||
| 78 | 0 | ▶ | $io->print( $data ) || return( $self->error( "Unable to write data to json file $file: $!" ) ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 0 | $io->close; | |||||||||||
| 80 | 0 | chmod( 0666, $file ); | |||||||||||
| 81 | 0 | return( $self->error( sprintf( "An error occured while attempting to parse %d bytes of data into json: $@\nFailed raw data was saved in file $file", length( $data ) ) ) ); | |||||||||||
| 82 | } | ||||||||||||
| 83 | 0 | return( $json ); | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 1 | 1; | |||||||||||
| 87 | |||||||||||||
| 88 | __END__ | ||||||||||||