| 1 | package WWW::BigDoor; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use Digest::SHA qw(sha256_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use REST::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use UUID::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | #use Smart::Comments -ENV; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 18 | ▶ | use base qw(Class::Accessor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 18 | ▶ | use version; our $VERSION = qv( '0.1.1' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 18 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | 18 | foreach my $method ( qw(GET POST PUT DELETE) ) { | |||||||||||||||||||||||||||
| 21 | 18 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | #my $full_method_name = __PACKAGE__.'::'.$method; | ||||||||||||||||||||||||||||
| 24 | ## full method name: $full_method_name | ||||||||||||||||||||||||||||
| 25 | 72 | ▶ | *{__PACKAGE__ . '::' . $method} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 26 | 185 | my $response_body = do_request( shift, $method, @_ ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 185 | ▶ | my $decoded_response_body = | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$response_body and $response_body ne ""
MC/DC50%1/2
$response_body and $response_body ne ""
$response_body
$response_body ne ""
Truth table66%2/3
A and B
A= $response_bodyB= $response_body ne ""
| |||||||||||||||||||||||||||||
| 29 | $response_body && $response_body ne q{} | ||||||||||||||||||||||||||||
| 30 | ? decode_json( $response_body ) | ||||||||||||||||||||||||||||
| 31 | : undef; # TODO test for response_body eq q{} | ||||||||||||||||||||||||||||
| 32 | ## decoded_response_body: $decoded_response_body | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 185 | return $decoded_response_body; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 72 | } | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 18 | __PACKAGE__->follow_best_practice; | |||||||||||||||||||||||||||
| 40 | 18 | __PACKAGE__->mk_accessors( | |||||||||||||||||||||||||||
| 41 | qw(app_secret app_key api_host base_url request_result response_code response_content) ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 17 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 17 | my ( $class, $app_secret, $app_key, $api_host ) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 17 | my $self = {}; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 17 | bless( $self, $class ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | ### check: defined $app_secret | ||||||||||||||||||||||||||||
| 52 | ### check: defined $app_key | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 17 | $self->set_app_secret( $app_secret ); # TODO test for empty or undefined app_secret or app_key | |||||||||||||||||||||||||||
| 55 | 17 | $self->set_app_key( $app_key ); | |||||||||||||||||||||||||||
| 56 | 17 | ▶ | $self->set_api_host( $api_host || 'http://api.bigdoor.com' ); # TODO test for empty $api_host | ||||||||||||||||||||||||||
|
Condition50%1/2
$api_host || 'http://api.bigdoor.com'
MC/DC0%0/1
$api_host || 'http://api.bigdoor.com'
$api_host
Truth table50%1/2
A
A= $api_host
| |||||||||||||||||||||||||||||
| 57 | 17 | $self->set_base_url( sprintf "/api/publisher/%s", $app_key ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 17 | return $self; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 185 | ▶ | sub do_request { | ||||||||||||||||||||||||||
|
Subroutine
do_request: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 185 | my ( $self, $method, $endpoint, $params, $payload ) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 185 | my $rc = REST::Client->new( {host => $self->get_api_host} ); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 185 | my $url = $self->get_base_url . '/' . $endpoint; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ## method: $method | ||||||||||||||||||||||||||||
| 70 | ## url: $url | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 185 | ▶ | my $par = defined $params ? {%{$params}} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 185 | ▶ | my $pay = defined $payload ? {%{$payload}} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 185 | ( $par, $pay ) = $self->_sign_request( $method, $url, $par, $pay ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | ### check: defined $par | ||||||||||||||||||||||||||||
| 78 | # should be always defined by _sign_request | ||||||||||||||||||||||||||||
| 79 | 185 | my $args = $rc->buildQuery( $par ); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | ## args: $args | ||||||||||||||||||||||||||||
| 82 | ## payload: Dumper($pay) | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 185 | my $headers = { | |||||||||||||||||||||||||||
| 85 | 'User-Agent' => sprintf( 'BigDoorKit-Perl/%s', $VERSION ), | ||||||||||||||||||||||||||||
| 86 | 'Content-Type' => 'application/x-www-form-urlencoded', | ||||||||||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 185 | my $post_body = q{}; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 185 | ▶ | if ( defined $pay ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 64 | require URI; | |||||||||||||||||||||||||||
| 93 | 64 | my $uri_encoded = URI->new( 'http:' ); | |||||||||||||||||||||||||||
| 94 | 64 | $uri_encoded->query_form( $pay ); | |||||||||||||||||||||||||||
| 95 | 64 | $post_body = $uri_encoded->query; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | ## post_body: $post_body | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | ### URL: $url . $args | ||||||||||||||||||||||||||||
| 101 | 185 | my $result = $rc->request( $method, $url . $args, $post_body, $headers ); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 185 | $self->set_request_result( $result ); | |||||||||||||||||||||||||||
| 104 | 185 | $self->set_response_code( $result->responseCode ); | |||||||||||||||||||||||||||
| 105 | 185 | $self->set_response_content( $result->responseContent ); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | ### check: defined $result | ||||||||||||||||||||||||||||
| 108 | 185 | ▶ | return unless defined $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | ### result: Dumper($result->{_res}) | ||||||||||||||||||||||||||||
| 111 | ### response code: $result->responseCode() | ||||||||||||||||||||||||||||
| 112 | ### check: $result->responseCode < 300 | ||||||||||||||||||||||||||||
| 113 | 185 | ▶ | return if $result->responseCode >= 300; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | ## response content: $result->responseContent() | ||||||||||||||||||||||||||||
| 116 | ## response headers: Dumper($result->responseHeaders()) | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 183 | my $response_body = $result->responseContent(); | |||||||||||||||||||||||||||
| 119 | ### check: defined $response_body | ||||||||||||||||||||||||||||
| 120 | ### response_body: $response_body | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 183 | return $response_body; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | } ## end sub do_request | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 185 | ▶ | sub _sign_request { | ||||||||||||||||||||||||||
|
Subroutine
_sign_request: called CC=10 SCAR=23.7
| |||||||||||||||||||||||||||||
| 127 | 185 | my ( $self, $method, $url, $params, $payload ) = @_; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # FIXME use content copy | ||||||||||||||||||||||||||||
| 130 | 185 | my $is_postish = $method =~ /^(POST)|(PUT)$/ix; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 185 | ▶ | if ( $is_postish && exists $payload->{'time'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$is_postish and exists $payload->{"time"}
MC/DC0%0/2
$is_postish and exists $payload->{"time"}
$is_postish
exists $payload->{"time"}
Truth table66%2/3
A and B
A= $is_postishB= exists $payload->{"time"}
| |||||||||||||||||||||||||||||
| 133 | 0 | $params->{'time'} = $payload->{'time'}; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 185 | ▶ | unless ( exists $params->{'time'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 185 | $params->{'time'} = time; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 185 | ▶ | if ( $is_postish && !exists $payload->{'time'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$is_postish and not exists $payload->{"time"}
MC/DC50%1/2
$is_postish and not exists $payload->{"time"}
$is_postish
not exists $payload->{"time"}
Truth table66%2/3
A and not(B)
A= $is_postishB= not exists $payload->{"time"}
| |||||||||||||||||||||||||||||
| 139 | 64 | $payload->{'time'} = $params->{'time'}; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 185 | ▶ | if ( $is_postish && !exists $payload->{'token'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$is_postish and not exists $payload->{"token"}
MC/DC50%1/2
$is_postish and not exists $payload->{"token"}
$is_postish
not exists $payload->{"token"}
Truth table66%2/3
A and not(B)
A= $is_postishB= not exists $payload->{"token"}
| |||||||||||||||||||||||||||||
| 142 | 64 | $payload->{'token'} = $self->generate_token(); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 185 | ▶ | if ( $method =~ /^DELETE$/ix && !exists $params->{'delete_token'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$method =~ /^DELETE$/xi and not exists $params->{"delete_token"}
MC/DC50%1/2
$method =~ /^DELETE$/xi and not exists $params->{"delete_token"}
$method =~ /^DELETE$/xi
not exists $params->{"delete_token"}
Truth table66%2/3
A and not(B)
A= $method =~ /^DELETE$/xiB= not exists $params->{"delete_token"}
| |||||||||||||||||||||||||||||
| 145 | 45 | $params->{'delete_token'} = $self->generate_token(); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 185 | $params->{'sig'} = $self->generate_signature( $url, $params, $payload ); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | ### check: defined $params | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 185 | return ( $params, $payload ); | |||||||||||||||||||||||||||
| 153 | } ## end sub _sign_request | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 253 | ▶ | sub _flatten_params { | ||||||||||||||||||||||||||
|
Subroutine
_flatten_params: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 156 | 253 | my ( $params ) = @_; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 253 | my $result = q{}; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 253 | foreach my $k ( sort keys %{$params} ) { | |||||||||||||||||||||||||||
| 161 | 773 | ▶ | next if $k eq 'sig' || $k eq 'format'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$k eq "sig" or $k eq "format"
MC/DC100%2/2
$k eq "sig" or $k eq "format"
$k eq "sig"
$k eq "format"
Truth table100%3/3
A or B
A= $k eq "sig"B= $k eq "format"
| |||||||||||||||||||||||||||||
| 162 | 678 | $result .= sprintf '%s%s', $k, $params->{$k}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 253 | return $result; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 109 | ▶ | sub generate_token { | ||||||||||||||||||||||||||
|
Subroutine
generate_token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 109 | return unpack( "H*", create_UUID( UUID_V4 ) ); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 189 | ▶ | sub generate_signature { | ||||||||||||||||||||||||||
|
Subroutine
generate_signature: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 189 | my ( $self, $url, $params, $payload ) = @_; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 189 | my $signature = $url; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 189 | ▶ | $signature .= _flatten_params( $params ) if defined $params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 189 | ▶ | $signature .= _flatten_params( $payload ) if defined $payload; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 189 | $signature .= $self->get_app_secret(); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | ### signature: $signature | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 189 | return sha256_hex( $signature ); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 18 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||