| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::Pandora::Method; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 26037 | use strict; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 131 |  | 
| 4 | 4 |  |  | 4 |  | 23 | use warnings; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 99 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 4 |  |  | 4 |  | 2110 | use WebService::Pandora::Cryptor; | 
|  | 4 |  |  |  |  | 42 |  | 
|  | 4 |  |  |  |  | 2110 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 4 |  |  | 4 |  | 62136 | use URI; | 
|  | 4 |  |  |  |  | 40195 |  | 
|  | 4 |  |  |  |  | 137 |  | 
| 9 | 4 |  |  | 4 |  | 3736 | use JSON; | 
|  | 4 |  |  |  |  | 68574 |  | 
|  | 4 |  |  |  |  | 44 |  | 
| 10 | 4 |  |  | 4 |  | 7971 | use HTTP::Request; | 
|  | 4 |  |  |  |  | 105162 |  | 
|  | 4 |  |  |  |  | 160 |  | 
| 11 | 4 |  |  | 4 |  | 15740 | use LWP::UserAgent; | 
|  | 4 |  |  |  |  | 204890 |  | 
|  | 4 |  |  |  |  | 157 |  | 
| 12 | 4 |  |  | 4 |  | 104 | use Data::Dumper; | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 3321 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | ### constructor ### | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub new { | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 1 |  |  | 1 | 1 | 15 | my $caller = shift; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 1 |  |  |  |  | 2 | my $class = ref( $caller ); | 
| 21 | 1 | 50 |  |  |  | 5 | $class = $caller if ( !$class ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 1 |  |  |  |  | 16 | my $self = {'name' => undef, | 
| 24 |  |  |  |  |  |  | 'partnerAuthToken' => undef, | 
| 25 |  |  |  |  |  |  | 'userAuthToken' => undef, | 
| 26 |  |  |  |  |  |  | 'partnerId' => undef, | 
| 27 |  |  |  |  |  |  | 'userId' => undef, | 
| 28 |  |  |  |  |  |  | 'syncTime' => undef, | 
| 29 |  |  |  |  |  |  | 'host' => undef, | 
| 30 |  |  |  |  |  |  | 'ssl' => 0, | 
| 31 |  |  |  |  |  |  | 'encrypt' => 1, | 
| 32 |  |  |  |  |  |  | 'cryptor' => undef, | 
| 33 |  |  |  |  |  |  | 'timeout' => 10, | 
| 34 |  |  |  |  |  |  | 'params' => {}, | 
| 35 |  |  |  |  |  |  | @_}; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 1 |  |  |  |  | 4 | bless( $self, $class ); | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # create and store user agent object | 
| 40 | 1 |  |  |  |  | 16 | $self->{'ua'} = LWP::UserAgent->new( timeout => $self->{'timeout'} ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | # create and store json object | 
| 43 | 1 |  |  |  |  | 5523 | $self->{'json'} = JSON->new(); | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 1 |  |  |  |  | 6 | return $self; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | ### getters/setters ### | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub error { | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 2 |  |  | 2 | 1 | 786 | my ( $self, $error ) = @_; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 2 | 100 |  |  |  | 9 | $self->{'error'} = $error if ( defined( $error ) ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 2 |  |  |  |  | 8 | return $self->{'error'}; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | ### public methods ### | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub execute { | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 1 |  |  | 1 | 1 | 9 | my ( $self ) = @_; | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | # make sure both name and host were given | 
| 66 | 1 | 50 | 33 |  |  | 13 | if ( !defined( $self->{'name'} ) || !defined( $self->{'host'} ) ) { | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 1 |  |  |  |  | 6 | $self->error( 'Both the name and host must be provided to the constructor.' ); | 
| 69 | 1 |  |  |  |  | 3 | return; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | # craft the json data accordingly | 
| 73 | 0 |  |  |  |  |  | my $json_data = {}; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 | 0 |  |  |  |  | if ( defined( $self->{'userAuthToken'} ) ) { | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 |  |  |  |  |  | $json_data->{'userAuthToken'} = $self->{'userAuthToken'}; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 0 | 0 |  |  |  |  | if ( defined( $self->{'syncTime'} ) ) { | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 |  |  |  |  |  | $json_data->{'syncTime'} = int( $self->{'syncTime'} ); | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | # merge the two required params with the additional user-supplied args | 
| 86 | 0 |  |  |  |  |  | $json_data = {%$json_data, %{$self->{'params'}}}; | 
|  | 0 |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | # encode it to json | 
| 89 | 0 |  |  |  |  |  | $json_data = $self->{'json'}->encode( $json_data ); | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | # encrypt it, if needed | 
| 92 | 0 | 0 |  |  |  |  | if ( $self->{'encrypt'} ) { | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 |  |  |  |  |  | $json_data = $self->{'cryptor'}->encrypt( $json_data ); | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | # detect error decrypting | 
| 97 | 0 | 0 |  |  |  |  | if ( !defined( $json_data ) ) { | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 0 |  |  |  |  |  | $self->error( 'An error occurred encrypting our JSON data: ' . $self->{'cryptor'}->error() ); | 
| 100 | 0 |  |  |  |  |  | return; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | # http or https? | 
| 105 | 0 | 0 |  |  |  |  | my $protocol = ( $self->{'ssl'} ) ? 'https://' : 'http://'; | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | # craft the full URL, protocol + host + path | 
| 108 | 0 |  |  |  |  |  | my $url = $protocol . $self->{'host'} . '/services/json/'; | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | # create URI object | 
| 111 | 0 |  |  |  |  |  | my $uri = URI->new( $url ); | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | # create all url params for POST request | 
| 114 | 0 |  |  |  |  |  | my $url_params = ['method' => $self->{'name'}]; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # set user_auth_token if provided | 
| 117 | 0 | 0 |  |  |  |  | if ( defined( $self->{'userAuthToken'} ) ) { | 
|  |  | 0 |  |  |  |  |  | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 0 |  |  |  |  |  | push( @$url_params, 'auth_token' => $self->{'userAuthToken'} ); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | # set partner_auth_token if provided and user_auth_token was not | 
| 123 |  |  |  |  |  |  | elsif ( defined( $self->{'partnerAuthToken'} ) ) { | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 0 |  |  |  |  |  | push( @$url_params, 'auth_token' => $self->{'partnerAuthToken'} ); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | # set partner_id if provided | 
| 129 | 0 | 0 |  |  |  |  | if ( defined( $self->{'partnerId'} ) ) { | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 |  |  |  |  |  | push( @$url_params, 'partner_id' => $self->{'partnerId'} ); | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | # set user_id if provided | 
| 135 | 0 | 0 |  |  |  |  | if ( defined( $self->{'userId'} ) ) { | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 0 |  |  |  |  |  | push( @$url_params, 'user_id' => $self->{'userId'} ); | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | # add the params to the URI | 
| 141 | 0 |  |  |  |  |  | $uri->query_form( $url_params ); | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | # create and store the POST request accordingly | 
| 144 | 0 |  |  |  |  |  | my $request = HTTP::Request->new( 'POST', $uri ); | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | # json data is the POST content | 
| 147 | 0 |  |  |  |  |  | $request->content( $json_data ); | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | # issue the HTTP request | 
| 150 | 0 |  |  |  |  |  | my $response = $self->{'ua'}->request( $request ); | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | # handle request error | 
| 153 | 0 | 0 |  |  |  |  | if ( !$response->is_success() ) { | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 0 |  |  |  |  |  | $self->error( $response->status_line() ); | 
| 156 | 0 |  |  |  |  |  | return; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | # decode the raw content of the response | 
| 160 | 0 |  |  |  |  |  | my $content = $response->decoded_content(); | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | # decode the JSON content | 
| 163 | 0 |  |  |  |  |  | $json_data = $self->{'json'}->decode( $content ); | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | # handle pandora error | 
| 166 | 0 | 0 |  |  |  |  | if ( $json_data->{'stat'} ne 'ok' ) { | 
| 167 |  |  |  |  |  |  |  | 
| 168 | 0 |  |  |  |  |  | $self->error( "$self->{'name'} error $json_data->{'code'}: $json_data->{'message'}" ); | 
| 169 | 0 |  |  |  |  |  | return; | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | # return all result data, if any exists | 
| 173 | 0 | 0 |  |  |  |  | return $json_data->{'result'} if ( defined( $json_data->{'result'} ) ); | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | # otherwise just return a true value to indicate success | 
| 176 | 0 |  |  |  |  |  | return 1; | 
| 177 |  |  |  |  |  |  | } | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | 1; |