Data/Riak/Fast/HTTP.pm

filter: Statementstmt 89.0% 65 / 73 Branchbran 65.0% 13 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 66.6% 2 / 3 totaltotal 85.9% 98 / 114 CC 12 SCAR 12.5
CC
12
Cov
84%
_send
9.8
ping
2.1
send
2.0
CRAP 3.5
« Test/Data/Riak/Fast.pm ↑ Summary Data/Riak/Fast/MapReduce.pm »
1 package Data::Riak::Fast::HTTP;
2 # ABSTRACT: An interface to a Riak server, using its HTTP (REST) interface
3  
4 22 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Net::DNS::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Cache::LRU;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 ▶use Data::Riak::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Data::Riak::Fast::HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use Data::Riak::Fast::HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head2 host
18  
19 The host the Riak server is on. Can be set via the environment variable
20 DATA_RIAK_HTTP_HOST, and defaults to 127.0.0.1.
21  
22 =cut
23  
24 has host => (
25 is => 'ro',
26 isa => 'Str',
27 18 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
28 18 ▶ $ENV{'DATA_RIAK_HTTP_HOST'} || '127.0.0.1';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DATA_RIAK_HTTP_HOST"} or "127.0.0.1" 1 17 18
29 }
30 22 );
31  
32 =head2 port
33  
34 The port of the host that the riak server is on. Can be set via the environment
35 variable DATA_RIAK_HTTP_PORT, and defaults to 8098.
36  
37 =cut
38  
39 has port => (
40 is => 'ro',
41 isa => 'Int',
42 18 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
43 18 ▶ $ENV{'DATA_RIAK_HTTP_PORT'} || '8098';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DATA_RIAK_HTTP_PORT"} or 8098 1 17 18
44 }
45 22 );
46  
47 =head2 timeout
48  
49 The maximum value (in seconds) that a request can go before timing out. Can be set
50 via the environment variable DATA_RIAK_HTTP_TIMEOUT, and defaults to 15.
51  
52 =cut
53  
54 has timeout => (
55 is => 'ro',
56 isa => 'Num',
57 18 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
58 18 ▶ $ENV{'DATA_RIAK_HTTP_TIMEOUT'} || '15';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DATA_RIAK_HTTP_TIMEOUT"} or 15 1 17 18
59 }
60 22 );
61  
62 =head2 user_agent
63  
64 This is the instance of L<LWP::UserAgent> we use to talk to Riak.
65  
66 =cut
67  
68 =head1 METHOD
69 =head2 base_uri
70  
71 The base URI for the Riak server.
72  
73 =cut
74  
75 17 ▶sub base_uri {
Subroutine base_uri: called CC=1 SCAR=0.0
Pod undocumented
76 17 my $self = shift;
77 17 return sprintf('http://%s:%s/', $self->host, $self->port);
78 }
79  
80 =head2 ping
81  
82 Tests to see if the specified Riak server is answering. Returns 0 for no, 1 for yes.
83  
84 =cut
85  
86 17 ▶sub ping {
Subroutine ping: called CC=2 SCAR=7.6
Pod documented
87 17 my $self = shift;
88 17 my ($response,) = $self->send({ method => 'GET', uri => 'ping' });
89 17 ▶ return 0 unless($response->code eq '200');
Branch: 1 branch
BranchTrueFalseTotal
unless $response->code eq 200 17 0 17
90 0 return 1;
91 }
92  
93 =head2 send ($request)
94  
95 Send a Data::Riak::Fast::HTTP::Request to the server. If you pass in a hashref, it will
96 create the Request object for you on the fly.
97  
98 =cut
99  
100 17 ▶sub send {
Subroutine send: called CC=2 SCAR=7.0
Pod documented
101 17 my ($self, $request) = @_;
102 17 ▶ unless(blessed $request) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($request)) 17 0 17
103 17 $request = Data::Riak::Fast::HTTP::Request->new($request);
104 }
105 17 my ($response, $url) = $self->_send($request);
106 17 return $response, $url;
107 }
108  
109 17 ▶sub _send {
Subroutine _send: called CC=7 SCAR=22.9
110 17 my ($self, $request) = @_;
111  
112 17 my $uri = URI->new( sprintf('%s%s', $self->base_uri, $request->uri) );
113  
114 17 ▶ if ($request->has_query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->has_query) 0 17 17
115 0 $uri->query_form($request->query);
116 }
117  
118 17 my @headers;
119 17 ▶ push @headers, 'Accept' => $request->accept if $request->method eq 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $request->method eq "GET" 17 0 17
120 17 ▶ push @headers, 'Content-Type' => $request->content_type if $request->method =~ /^(POST|PUT)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $request->method =~ /^(POST|PUT)$/ 0 17 17
121 17 ▶ if(my $links = $request->links) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $links = $request->links) 0 17 17
122 0 push @headers, 'Link' => $request->links;
123 }
124  
125 17 ▶ if(my $indexes = $request->indexes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $indexes = $request->indexes) 0 17 17
126 0 foreach my $index (@{$indexes}) {
127 0 my $field = $index->{field};
128 0 my $values = $index->{values};
129 0 push @headers, ":X-Riak-Index-$field" => $values;
130 }
131 }
132  
133 17 $Net::DNS::Lite::CACHE = Cache::LRU->new(
134 size => 256,
135 );
136  
137 17 my $furl = Furl::HTTP->new(
138 agent => "Data::Riak::Fast/$Data::Riak::Fast::VERSION",
139 timeout => $self->timeout,
140 inet_aton => \&Net::DNS::Lite::inet_aton,
141 );
142 17 my ( $mv, $code, $msg, $headers, $content ) = $furl->request(
143 method => $request->method,
144 url => $uri->as_string,
145 headers => \@headers,
146 content => $request->data,
147 );
148 17 my $http_response = HTTP::Response->new($code, $msg, $headers, $content);
149  
150 17 my $response = Data::Riak::Fast::HTTP::Response->new({
151 http_response => $http_response
152 });
153  
154 17 return $response, $uri;
155 }
156  
157 =begin :postlude
158  
159 =head1 ACKNOWLEDGEMENTS
160  
161  
162 =end :postlude
163  
164 =cut
165  
166 22 __PACKAGE__->meta->make_immutable;
167 22 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 22 1;
« Test/Data/Riak/Fast.pm ↑ Summary Data/Riak/Fast/MapReduce.pm »