File Coverage

blib/lib/Net/Rexster/Request.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Net::Rexster::Request;
2              
3 1     1   1187 use warnings;
  1         2  
  1         336  
4 1     1   6 use strict;
  1         1  
  1         27  
5 1     1   5 use Carp;
  1         2  
  1         52  
6              
7 1     1   416 use Moose;
  0            
  0            
8             use LWP::UserAgent;
9             use Net::Rexster::Response;
10             use Encode;
11             use JSON;
12             use URI::Escape;
13             use utf8;
14              
15             has 'ua' => (is => 'ro', isa => "LWP::UserAgent", default => sub { LWP::UserAgent->new } );
16              
17             __PACKAGE__->meta->make_immutable;
18             no Moose;
19              
20             sub get { shift->_call('GET', @_) }
21             sub post { shift->_call('POST', @_) }
22             sub put { shift->_call('PUT', @_) }
23             sub delete { shift->_call('DELETE', @_) }
24              
25             # Send query to server by LWP::UserAgent
26             sub _call {
27             my ($self, $method, $uri, $data, $args) = @_;
28              
29             # URI encoding for # and the language which needs ecoding, e.g. Japanese
30             $uri = uri_escape_utf8($uri, "^A-Za-z0-9\/\:\&\+\=\?\(\)\'\,\*\;");
31             my $req = HTTP::Request->new($method, $uri);
32              
33             my $response = $self->ua->request($req);
34             unless ($response->is_success){
35             warn "Failed to get response from server...\n";
36             return Net::Rexster::Response->new(content => {});
37             }
38              
39             # Decode from JSON and utf8
40             return Net::Rexster::Response->new(content => JSON->new->utf8->decode(decode_utf8($response->content)));
41             }
42              
43             1; # Magic true value required at end of module