File Coverage

blib/lib/Purple/Server/REST.pm
Criterion Covered Total %
statement 21 72 29.1
branch 0 8 0.0
condition 1 2 50.0
subroutine 6 15 40.0
pod 4 4 100.0
total 32 101 31.6


line stmt bran cond sub pod time code
1             package Purple::Server::REST;
2              
3 1     1   6 use warnings;
  1         2  
  1         39  
4 1     1   5 use strict;
  1         2  
  1         60  
5             our $VERSION = '0.9';
6              
7 1     1   6 use base qw(HTTP::Server::Simple::CGI);
  1         2  
  1         1533  
8 1     1   24507 use Purple;
  1         2  
  1         20  
9 1     1   4 use URI::Escape;
  1         2  
  1         784  
10              
11             sub _New {
12 1     1   2 my $class = shift;
13 1         3 my %p = @_;
14              
15 1   50     4 $p{port} ||= 9999;
16 1         9 my $self = $class->SUPER::new( $p{port} );
17 1         31 $self->{purple} = Purple->new( store => $p{store} );
18 1         19 return $self;
19             }
20              
21             sub handle_request {
22 0     0 1   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 0         if ( $self->can($method) ) {
30 0           eval {
31             # XXX trap empty output when no match
32 0           $output = $self->$method($cgi);
33 0           $status = '200';
34             };
35 0 0         if ($@) {
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             sub _get_info {
53 0     0     my $self = shift;
54 0           my $path = shift;
55 0           $path =~ s{^/}{};
56 0           return uri_unescape($path);
57             }
58              
59             sub GET {
60 0     0 1   my $self = shift;
61 0           my $cgi = shift;
62 0           my $info = $self->_get_info( $cgi->path_info );
63              
64 0 0         if ( $info =~ m{^/?\w+:} ) {
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             sub PUT {
72 0     0 1   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             sub DELETE {
82 0     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             sub POST {
89 0     0 1   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 0         if ($nid) {
97 0           return $self->{purple}->updateURL( $uri, $nid );
98             }
99              
100 0           return $self->{purple}->getNext($uri);
101             }
102              
103             sub _handle_get_nid {
104 0     0     my $self = shift;
105 0           my $uri = shift;
106 0           return $self->{purple}->getNIDs($uri);
107             }
108              
109             sub _handle_get_uri {
110 0     0     my $self = shift;
111 0           my $nid = shift;
112 0           return $self->{purple}->getURL($nid);
113             }
114              
115             sub _get_content {
116 0     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, Ecdent@burningchrome.comE
155              
156             Eugene Eric Kim, Eeekim@blueoxen.comE
157              
158             =head1 BUGS
159              
160             Please report any bugs or feature requests to
161             C, or through the web interface at
162             L.
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             1; # End of Purple