Purple/Client.pm

filter: Statementstmt 98.6% 73 / 74 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 6 / 6 totaltotal 97.1% 101 / 104 CC 4 SCAR 2.6
CC
4
Cov
96%
_respond_or_die
2.1
_post
2.0
new
2.0
CRAP 1.3
« Purple.pm ↑ Summary Purple/DB_File.pm »
1 package Purple::Client;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Purple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
12 2 my $class = shift;
13 2 my %p = @_;
14  
15 # Use library directly if server_url is not set
16 2 ▶ unless ($p{server_url}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p{"server_url"}) 1 1 2
17 1 return Purple->new(store => $p{store});
18 }
19  
20 1 my $self = {};
21 1 $self->{server_url} = $p{server_url};
22 1 bless ($self, $class);
23 1 return $self;
24 }
25  
26 1 ▶sub getNext {
Subroutine getNext: called CC=1 SCAR=0.0
Pod documented
27 1 my $self = shift;
28 1 my $uri = shift;
29  
30 1 return _post( $self->{server_url}, $uri );
31 }
32  
33 1 ▶sub getNIDs {
Subroutine getNIDs: called CC=1 SCAR=0.0
Pod documented
34 1 my $self = shift;
35 1 my $url = shift;
36 1 return _get( $self->{server_url}, $url );
37 }
38  
39 1 ▶sub getURL {
Subroutine getURL: called CC=1 SCAR=0.0
Pod documented
40 1 my $self = shift;
41 1 my $nid = shift;
42 1 return _get( $self->{server_url}, $nid );
43 }
44  
45 # XXX just one for now, API can do multi
46 1 ▶sub deleteNIDs {
Subroutine deleteNIDs: called CC=1 SCAR=0.0
Pod documented
47 1 my $self = shift;
48 1 my $nid = shift;
49 1 return _delete( $self->{server_url}, $nid );
50 }
51  
52 1 ▶sub updateURL {
Subroutine updateURL: called CC=1 SCAR=0.0
Pod documented
53 1 my $self = shift;
54 1 my $new_uri = shift;
55 1 my $nid = shift;
56  
57 1 return _put($self->{server_url}, $new_uri, $nid);
58 }
59  
60 1 ▶sub _post {
Subroutine _post: called CC=2 SCAR=7.0
61 1 my $server = shift;
62 1 my $uri = shift;
63 1 my $nid = shift;
64 1 my $request = HTTP::Request->new( POST => $server );
65 1 ▶ $uri = $uri . '#' . $nid if $nid;
Branch: 1 branch
BranchTrueFalseTotal
if $nid 0 1 1
66 1 $request->content($uri);
67 1 _respond_or_die($request);
68 }
69  
70 2 ▶sub _get {
Subroutine _get: called CC=1 SCAR=0.0
71 2 my $server = shift;
72 2 my $arg = shift;
73 2 $arg = uri_escape($arg);
74 2 my $request = HTTP::Request->new( GET => $server . '/' . $arg );
75 2 _respond_or_die($request);
76 }
77  
78 1 ▶sub _delete {
Subroutine _delete: called CC=1 SCAR=0.0
79 1 my $server = shift;
80 1 my $nid = shift;
81 1 my $request = HTTP::Request->new( DELETE => $server . '/' . $nid );
82 1 _respond_or_die($request);
83 }
84  
85 1 ▶sub _put {
Subroutine _put: called CC=1 SCAR=0.0
86 1 my $server = shift;
87 1 my $uri = shift;
88 1 my $nid = shift;
89 1 my $request
90 = HTTP::Request->new( PUT => $server );
91 1 $request->content( $uri . '#' . $nid );
92 1 _respond_or_die($request);
93 }
94  
95 5 ▶sub _respond_or_die {
Subroutine _respond_or_die: called CC=2 SCAR=7.2
96 5 my $request = shift;
97 5 my $ua = _userAgent();
98  
99 5 my $response = $ua->request($request);
100 5 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 5 0 5
101 5 return $response->content;
102 }
103  
104 # in real client eval, set errstr, whatever
105 0 die $response->status_line;
106 }
107  
108 # XXX more flesh here
109 5 ▶sub _userAgent {
Subroutine _userAgent: called CC=1 SCAR=0.0
110 5 my $ua = LWP::UserAgent->new;
111 5 $ua->timeout(10);
112  
113 # blah blah
114 5 return $ua;
115 }
116  
117 2 1;
118  
119 =head1 NAME
120  
121 Purple::Client - Client to L<Purple> and L<Purple::Server>
122  
123 =head1 SYNOPSIS
124  
125 # use a remote Purple::Server
126 my $client_net = Purple::Client->new(server_url => $SERVER_URL);
127  
128 # access a local store through the library
129 my $client_lib = Purple::Client->new(store => 't/sql.lite');
130  
131 =head1 METHODS
132  
133 =head2 new(%options)
134  
135 Valid %options include:
136  
137 server_url => 'SERVER_URL' # for distributed Purple
138 store => 'STORE' # for local Purple
139  
140 If no options specified, defaults to local SQLite store.
141  
142 =head2 getNext($uri)
143  
144 Gets the next available NID, assigning it $uri in the database.
145  
146 =head2 getNIDs($uri)
147  
148 Gets all NIDs associated with $uri.
149  
150 =head2 getURL($nid)
151  
152 Gets the URL associated with NID $nid.
153  
154 =head2 deleteNIDs($nid)
155  
156 Deletes the NID $nid. Note that while the local API supports deleting
157 multiple NIDs at once, this does not (yet).
158  
159 =head2 updateURL($url, $nid)
160  
161 Updates the NID $nid with the URL $url. Note that while the local API
162 supports updating multiple NIDs at once, this does not (yet).
163  
164 =head1 AUTHOR
165  
166 Chris Dent, E<lt>cdent@burningchrome.comE<gt>
167  
168 Eugene Eric Kim, E<lt>eekim@blueoxen.comE<gt>
169  
170 =head1 BUGS
171  
172 Please report any bugs or feature requests to
173 C<bug-purple@rt.cpan.org>, or through the web interface at
174 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Purple>.
175 I will be notified, and then you'll automatically be notified of progress on
176 your bug as I make changes.
177  
178 =head1 COPYRIGHT & LICENSE
179  
180 (C) Copyright 2006 Blue Oxen Associates. All rights reserved.
181  
182 This program is free software; you can redistribute it and/or modify it
183 under the same terms as Perl itself.
184  
185 =cut
« Purple.pm ↑ Summary Purple/DB_File.pm »