SAJAX.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« SAJAX/Exception/Response.pm ↑ Summary
1 package Net::SAJAX;
2  
3 1 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 ###############################################################################
8 # METADATA
9 our $AUTHORITY = 'cpan:DOUGDUDE';
10 our $VERSION = '0.107';
11  
12 ###############################################################################
13 # MOOSE
14 0 ▶use Moose 0.77;
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use MooseX::StrictConstructor 0.08;
16  
17 ###############################################################################
18 # MOOSE TYPES
19 use MooseX::Types::URI 0.02 qw(Uri);
20  
21 ###############################################################################
22 # MODULE IMPORTS
23 use Carp qw(croak);
24 use English qw(-no_match_vars);
25 use HTTP::Request::Common 5.814 (); # No imports
26 use JE 0.033;
27 use List::MoreUtils qw(any);
28 use LWP::UserAgent 5.819;
29 use Net::SAJAX::Exception 0.103;
30 use URI 1.22;
31 use URI::QueryParam;
32  
33 ###############################################################################
34 # ALL IMPORTS BEFORE THIS WILL BE ERASED
35 use namespace::clean 0.04 -except => [qw(meta)];
36  
37 ###############################################################################
38 # ATTRIBUTES
39 has autoclean_garbage => (
40 is => 'rw',
41 isa => 'Bool',
42 default => 0,
43 documentation => q{Whether or not to try automatic cleaning of garbage in the response},
44 );
45 has javascript_engine => (
46 is => 'ro',
47 isa => 'JE',
48 default => sub { JE->new(max_ops => 1000) },
49 documentation => q{JE object used for executing returned JavaScript},
50 );
51 has send_rand_key => (
52 is => 'rw',
53 isa => 'Bool',
54 default => 0,
55 documentation => q{Whether or not to send a random key with the request},
56 );
57 has target_id => (
58 is => 'rw',
59 isa => 'Str',
60 clearer => 'clear_target_id',
61 predicate => 'has_target_id',
62 documentation => q{The target ID to send with the request},
63 );
64 has url => (
65 is => 'rw',
66 isa => Uri,
67 coerce => 1,
68 required => 1,
69 documentation => q{The URL to send the request to},
70 );
71 has user_agent => (
72 is => 'rw',
73 isa => 'LWP::UserAgent',
74 default => sub { LWP::UserAgent->new },
75 documentation => q{The user agent that will be used to make the requests},
76 );
77  
78 ###############################################################################
79 # METHODS
80 sub call {
81 my ($self, %args) = @_;
82  
83 # Build a request object
84 my $request = $self->_build_request_for_call(%args);
85  
86 # Get a response
87 my $response = $self->user_agent->request($request);
88  
89 if (!$response->is_success) {
90 # The response was not successful
91 Net::SAJAX::Exception->throw(
92 class => 'Response',
93 message => 'An error occurred in the response',
94 response => $response,
95 );
96 }
97  
98 # Get the status and data from the response
99 my ($status, $data) = $self->_parse_data_from_response($response);
100  
101 if ($status eq q{-}) {
102 # This is an error
103 Net::SAJAX::Exception->throw(
104 class => 'RemoteError',
105 message => $data,
106 );
107 }
108  
109 # Evaluate the data
110 my $je_object = $self->javascript_engine->eval($data);
111  
112 if ($EVAL_ERROR) {
113 # JavaScript error when running code
114 Net::SAJAX::Exception->throw(
115 class => 'JavaScriptEvaluation',
116 javascript_error => $EVAL_ERROR,
117 javascript_string => $data,
118 message => 'JavaScript error occurred while running code',
119 );
120 }
121  
122 # Get the perl data structure
123 my $perl_ref = eval { $self->_unwrap_je_object($je_object) };
124  
125 if ($EVAL_ERROR) {
126 # An error occurred while expanding the JavaScript structure.
127 if (blessed $EVAL_ERROR eq 'Net::SAJAX::Exception::JavaScriptConversion') {
128 # Rethrow the error but with the over all JE object
129 Net::SAJAX::Exception->throw(
130 class => 'JavaScriptConversion',
131 javascript_object => $data,
132 message => 'Failed converting JavaScript object to Perl',
133 );
134 }
135 else {
136 croak $EVAL_ERROR;
137 }
138 }
139  
140 return $perl_ref;
141 }
142  
143 ###############################################################################
144 # PRIVATE METHODS
145 sub _build_request_for_call {
146 my ($self, %args) = @_;
147  
148 # Splice out the variables
149 my ($function, $arguments, $method)
150 = @args{qw(function arguments method)};
151  
152 # Set the default value for method. Perl 5.8 doesn't know //=
153 $method ||= 'GET';
154  
155 if (!defined $function) {
156 # No function was specified
157 Net::SAJAX::Exception->throw(
158 class => 'MethodArguments',
159 argument => 'function',
160 argument_value => $function,
161 message => 'No function was specified to call',
162 method => 'build_request_for_call',
163 );
164 }
165  
166 # Change the method to uppercase
167 $method = uc $method;
168  
169 if ($method ne 'GET' && $method ne 'POST') {
170 # SAJAX only supports GET and POST
171 Net::SAJAX::Exception->throw(
172 class => 'MethodArguments',
173 argument => 'method',
174 argument_value => $method,
175 message => 'SAJAX only supports the GET and POST methods',
176 method => 'build_request_for_call',
177 );
178 }
179  
180 if (defined $arguments) {
181 if (ref $arguments ne 'ARRAY') {
182 # Arguments must refer to an ARRAYREF
183 Net::SAJAX::Exception->throw(
184 class => 'MethodArguments',
185 argument => 'arguments',
186 argument_value => $arguments,
187 message => 'Must pass arguments as an ARRAYREF',
188 method => 'build_request_for_call',
189 );
190 }
191  
192 if(any {ref $_ ne q{}} @{$arguments}) {
193 # No argument can be a reference
194 Net::SAJAX::Exception->throw(
195 class => 'MethodArguments',
196 argument => 'arguments',
197 argument_value => $arguments,
198 message => 'No arguments can be a reference',
199 method => 'build_request_for_call',
200 );
201 }
202 }
203  
204 # Clone the URL
205 my $call_url = $self->url->clone;
206  
207 # Build the SAJAX arguments
208 my %sajax_arguments = (rs => $function);
209  
210 if ($self->has_target_id) {
211 # Add the target ID
212 $sajax_arguments{rst} = $self->target_id;
213 }
214  
215 if ($self->send_rand_key) {
216 # Add in a random key to the request
217 $sajax_arguments{rsrnd} = scalar time;
218 }
219  
220 if (defined $arguments) {
221 # Add the arguments to the request
222 $sajax_arguments{'rsargs[]'} = $arguments;
223 }
224  
225 # Hold the request
226 my $request;
227  
228 if ($method eq 'GET') {
229 # Add the SAJAX arguments to the URL for a GET request
230 $call_url->query_form_hash(%{$call_url->query_form_hash}, %sajax_arguments, );
231  
232 # Make the GET request
233 $request = HTTP::Request::Common::GET($call_url);
234 }
235 else {
236 # Make the POST request
237 $request = HTTP::Request::Common::POST($call_url, \%sajax_arguments);
238 }
239  
240 # Return the request
241 return $request;
242 }
243 sub _parse_data_from_response {
244 my ($self, $response) = @_;
245  
246 # Copy the content for manipulation
247 my $content = $response->content;
248  
249 if ($self->autoclean_garbage) {
250 # Clean out garbage found at the beginning
251 if ($content =~ m{^ \s* ([+-] : .*) \z}msx) {
252 $content = $1;
253 }
254  
255 # For the PHP SAJAX, attempt to parse out the exact response
256 if ($content =~ m{([+-] : var \s res \s? = \s? .*? ; \s? res;)}msx) {
257 $content = $1;
258 }
259 }
260  
261 # Parse out the status and data from the content
262 my ($status, $data) = $content
263 =~ m{\A \s* (.) . (.*?) \s* \z}msx;
264  
265 if (!defined $status) {
266 # The response was bad
267 Net::SAJAX::Exception->throw(
268 class => 'Response',
269 message => 'Received a bad response',
270 response => $response,
271 );
272 }
273  
274 # Return the status and data as an array
275 return ($status, $data);
276 }
277 sub _unwrap_je_object {
278 my ($self, $je_object) = @_;
279  
280 # Specify the HASH that maps object types with a subroutine that will
281 # convert the object into a Perl scalar.
282 my %object_value_map = (
283 'JE::Boolean' => sub { return shift->value },
284 'JE::LValue' => sub { return $self->_unwrap_je_object(shift->get) },
285 'JE::Null' => sub { return shift->value },
286 'JE::Number' => sub { return shift->value },
287 'JE::String' => sub { return shift->value },
288 'JE::Undefined' => sub { return shift->value },
289 'JE::Object' => sub {
290 my $hash_ref = shift->value;
291  
292 # Iterate through each HASH element and unwrap the value
293 foreach my $key (keys %{$hash_ref}) {
294 $hash_ref->{$key} = $self->_unwrap_je_object($hash_ref->{$key});
295 }
296  
297 return $hash_ref;
298 },
299 'JE::Object::Array' => sub {
300 return [ map { $self->_unwrap_je_object($_) } @{shift->value} ];
301 },
302 'JE::Object::Boolean' => sub { return shift->value },
303 'JE::Object::Date' => sub { return "$_[0]" },
304 'JE::Object::Number' => sub { return shift->value },
305 'JE::Object::RegExp' => sub { return shift->value },
306 'JE::Object::String' => sub { return shift->value },
307 );
308  
309 # Get the code reference for converting the object
310 my $convert_coderef = $object_value_map{ref $je_object};
311  
312 if (!defined $convert_coderef) {
313 Net::SAJAX::Exception->throw(
314 class => 'JavaScriptConversion',
315 javascript_object => $je_object,
316 message => 'Failed converting JavaScript object to Perl',
317 );
318 }
319  
320 return $convert_coderef->($je_object);
321 }
322  
323 ###############################################################################
324 # MAKE MOOSE OBJECT IMMUTABLE
325 __PACKAGE__->meta->make_immutable;
326  
327 1;
328  
329 __END__
« SAJAX/Exception/Response.pm ↑ Summary