Purple/Server/REST.pm

filter: Statementstmt 31.0% 23 / 74 Branchbran 0.0% 0 / 8 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 40.0% 6 / 15 Podpod 100% 4 / 4 totaltotal 32.6% 34 / 104 CC 6 SCAR 14.0
CC
6
Cov
29%
handle_request
12.0
GET
6.0
POST
6.0
CRAP 4.1
« Purple/SQLite.pm ↑ Summary Purple/Sequence.pm »
1 package Purple::Server::REST;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.9';
6  
7 2 ▶use base qw(HTTP::Server::Simple::CGI);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Purple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶sub _New {
Subroutine _New: called CC=2 SCAR=7.0
12 1 my $class = shift;
13 1 my %p = @_;
14  
15 1 ▶ $p{port} ||= 9999;
Condition50%1/2
$p{"port"} ||= 9999
l!l
10
MC/DC0%0/1
$p{"port"} ||= 9999
$p{"port"}
Truth table50%1/2
A
A= $p{"port"}
Aresult
11
00
16 1 my $self = $class->SUPER::new( $p{port} );
17 1 $self->{purple} = Purple->new( store => $p{store} );
18 1 return $self;
19 }
20  
21 0 ▶sub handle_request {
Subroutine handle_request: not called CC=3 SCAR=24.8
Pod documented
22 0 my $self = shift;
23 0 my $cgi = shift; # throwaway?
24  
25 0 my $method = $cgi->request_method;
26  
27 0 my $output;
28 my $status;
29 0 ▶ if ( $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 0 0 0
30 0 eval {
31 # XXX trap empty output when no match
32 0 $output = $self->$method($cgi);
33 0 $status = '200';
34 };
35 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
36 0 $status = '500';
37 0 $output = $@;
38 }
39 }
40 else {
41 0 $status = '500'; # XXX not right
42 0 $output = "Method $method not supported\n";
43  
44 }
45  
46 # empty response still means success
47 # XXX need to make this a real HTTP response
48 0 $output = "HTTP/1.0 $status OK\n\n" . $output;
49 0 print $output;
50 }
51  
52 0 ▶sub _get_info {
Subroutine _get_info: not called CC=1 SCAR=6.9
53 0 my $self = shift;
54 0 my $path = shift;
55 0 $path =~ s{^/}{};
56 0 return uri_unescape($path);
57 }
58  
59 0 ▶sub GET {
Subroutine GET: not called CC=2 SCAR=17.9
Pod documented
60 0 my $self = shift;
61 0 my $cgi = shift;
62 0 my $info = $self->_get_info( $cgi->path_info );
63  
64 0 ▶ if ( $info =~ m{^/?\w+:} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info =~ m[^/?\w+:]) 0 0 0
65 0 return $self->_handle_get_nid($info);
66 }
67 0 return $self->_handle_get_uri($info);
68 }
69  
70 # XXX not currently used
71 0 ▶sub PUT {
Subroutine PUT: not called CC=1 SCAR=6.9
Pod documented
72 0 my $self = shift;
73 0 my $cgi = shift;
74 0 my $nid = $self->_get_info( $cgi->path_info );
75  
76 0 my $uri = $self->_get_content($cgi);
77  
78 0 return $self->{purple}->updateURL( $nid, $uri );
79 }
80  
81 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
82 0 my $self = shift;
83 0 my $cgi = shift;
84 0 my $nid = $self->_get_info( $cgi->path_info );
85 0 return $self->{purple}->deleteNIDs($nid);
86 }
87  
88 0 ▶sub POST {
Subroutine POST: not called CC=2 SCAR=17.9
Pod documented
89 0 my $self = shift;
90 0 my $cgi = shift;
91 0 my $uri = $self->_get_content($cgi);
92 0 my $nid;
93  
94 0 ( $uri, $nid ) = split( '#', $uri );
95  
96 0 ▶ if ($nid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nid) 0 0 0
97 0 return $self->{purple}->updateURL( $uri, $nid );
98 }
99  
100 0 return $self->{purple}->getNext($uri);
101 }
102  
103 0 ▶sub _handle_get_nid {
Subroutine _handle_get_nid: not called CC=1 SCAR=6.9
104 0 my $self = shift;
105 0 my $uri = shift;
106 0 return $self->{purple}->getNIDs($uri);
107 }
108  
109 0 ▶sub _handle_get_uri {
Subroutine _handle_get_uri: not called CC=1 SCAR=6.9
110 0 my $self = shift;
111 0 my $nid = shift;
112 0 return $self->{purple}->getURL($nid);
113 }
114  
115 0 ▶sub _get_content {
Subroutine _get_content: not called CC=1 SCAR=6.9
116 0 my $self = shift;
117 0 my $cgi = shift;
118  
119 0 return $cgi->param('keywords');
120 }
121  
122 =head1 NAME
123  
124 Purple::Server - Server for Purple Numbers
125  
126 =head1 VERSION
127  
128 Version 0.9
129  
130 =head1 SYNOPSIS
131  
132 Server up some purple numbers of HTTP, in a RESTful way.
133  
134 =head1 METHODS
135  
136 =head2 handle_request
137  
138 Handles the request.
139  
140 =head2 GET
141  
142 Handles HTTP GET.
143  
144 =head2 POST
145  
146 Handles HTTP POST.
147  
148 =head2 PUT
149  
150 Handles HTTP POST.
151  
152 =head1 AUTHORS
153  
154 Chris Dent, E<lt>cdent@burningchrome.comE<gt>
155  
156 Eugene Eric Kim, E<lt>eekim@blueoxen.comE<gt>
157  
158 =head1 BUGS
159  
160 Please report any bugs or feature requests to
161 C<bug-purple@rt.cpan.org>, or through the web interface at
162 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Purple>.
163 I will be notified, and then you'll automatically be notified of progress on
164 your bug as I make changes.
165  
166 =head1 COPYRIGHT & LICENSE
167  
168 (C) Copyright 2006 Blue Oxen Associates. All rights reserved.
169  
170 This program is free software; you can redistribute it and/or modify it
171 under the same terms as Perl itself.
172  
173 =cut
174  
175 2 1; # End of Purple
« Purple/SQLite.pm ↑ Summary Purple/Sequence.pm »