Client.pm

filter: Statementstmt 48.0% 36 / 75 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 83.3% 10 / 12 Podpod 100% 2 / 2 totaltotal 41.3% 48 / 116 CC 11 SCAR 31.5
CC
11
Cov
37%
send
42.0
query
42.0
BEGIN
1.0
CRAP 23.4
↑ Summary EventList.pm »
1 package Osgood::Client;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Osgood::EventList;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Osgood::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 subtype 'Osgood.Client.URI' => as class_type('URI');
13 coerce 'Osgood.Client.URI'
14 => from Str
15 1 ▶ => via { URI->new($_, 'http') };
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 2 has 'error' => ( is => 'rw', isa => 'Str' );
18 has 'url' => (
19 is => 'rw',
20 isa => 'Osgood.Client.URI',
21 1 ▶ default => sub { URI->new('http://localhost') },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 coerce => 1
23 );
24 2 has 'list' => ( is => 'rw', isa => 'Maybe[Osgood::EventList]' );
25 2 has 'timeout' => ( is => 'rw', isa => 'Int', predicate => 'has_timeout' );
26  
27 2 our $VERSION = '2.0.7';
28 2 our $AUTHORITY = 'cpan:GPHAT';
29  
30 =head1 NAME
31  
32 Osgood::Client - Client for the Osgood Passive, Persistent Event Queue
33  
34 =head1 DESCRIPTION
35  
36 Provides a client for sending events to or retrieving events from an Osgood
37 queue.
38  
39 =head1 SYNOPSIS
40  
41 To send some events:
42  
43 my $event = Osgood::Event->new(
44 object => 'Moose',
45 action => 'farted',
46 date_occurred => DateTime->now
47 );
48 my $list = Osgood::EventList->new(events => [ $event ])
49 my $client = Osgood::Client->new(
50 url => 'http://localhost',
51 list => $list
52 );
53 my $retval = $client->send;
54 if($list->size == $retval) {
55 print "Success :)\n";
56 } else {
57 print "Failure :(\n";
58 }
59  
60 To query for events
61  
62 use DateTime;
63 use Osgood::Client;
64 use URI;
65  
66 my $client = Osgood::Client->new(
67 url => URI->new('http://localhost:3000'),
68 );
69 $client->query({ object => 'Moose', action => 'farted' });
70 if($client->list->size == 1) {
71 print "Success\n";
72 } else {
73 print "Failure\n";
74 }
75  
76 =head1 METHODS
77  
78 =head2 new
79  
80 Creates a new Osgood::Client object.
81  
82 =head2 list
83  
84 Set/Get the EventList. For sending events, you should set this. For
85 retrieving them, this will be populated by query returns.
86  
87 =head2 send
88  
89 Send events to the server.
90  
91 =cut
92 0 ▶sub send {
Subroutine send: not called CC=6 SCAR=37.4
Pod documented
93 0 my ($self) = @_;
94  
95 0 my $ua = LWP::UserAgent->new;
96 0 ▶ if($self->has_timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_timeout) 0 0 0
97 0 $ua->timeout($self->timeout);
98 }
99  
100 0 my $req = HTTP::Request->new(POST => $self->url->canonical.'/event');
101 0 $req->content_type('application/json');
102 0 $req->content($self->list->freeze);
103  
104 0 my $res = $ua->request($req);
105  
106 0 ▶ if($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
107 0 my $data = JSON::XS->new->decode($res->content);
108 0 ▶ if(!defined($data) || !(ref($data) eq 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data and ref $data eq "HASH") 0 0 0
Condition0%0/3
defined $data and ref $data eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and ref $data eq "HASH"
defined $data ref $data eq "HASH"
Truth table0%0/3
A and B
A= defined $dataB= ref $data eq "HASH"
ABresult
0X0
100
111
109 0 $self->error('Unable to parse JSON response');
110 0 return 0;
111 }
112 0 my $count = $data->{count};
113 0 ▶ if($data->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"error"}) 0 0 0
114 0 $self->error($data->error);
115 }
116  
117 0 return $count;
118 } else {
119 0 $self->error($res->status_line);
120 0 return 0;
121 }
122 }
123  
124 =head2 query
125  
126 Query the Osgood server for events. Takes a hashref in the following format:
127  
128 {
129 id => X,
130 object => 'obj',
131 action => 'foo',
132 date => '2007-12-11'
133 }
134  
135 At least one key is required.
136  
137 A true or false value is returned to denote the success of failure of the
138 query. If false, then the error will be set in the error accessor. On
139 success the list may be retrived from the list accessor.
140  
141 Implicitly sets $self->list(undef), to clear previous results.
142  
143 =cut
144  
145 0 ▶sub query {
Subroutine query: not called CC=6 SCAR=37.4
Pod documented
146 0 my ($self, $params) = @_;
147  
148 0 $self->list(undef);
149  
150 0 ▶ if((ref($params) ne 'HASH') || !scalar(keys(%{ $params }))) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params ne "HASH" or not scalar keys %{$params;}) 0 0 0
Condition0%0/3
ref $params ne "HASH" or not scalar keys %{$params;}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $params ne "HASH" or not scalar keys %{$params;}
ref $params ne "HASH" not scalar keys %{$params;}
Truth table0%0/3
A or not(B)
A= ref $params ne "HASH"B= not scalar keys %{$params;}
ABresult
1X1
011
000
151 0 die('Must supply a hash of parameters to query.');
152 }
153  
154 0 my $ua = LWP::UserAgent->new;
155 0 ▶ if($self->has_timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_timeout) 0 0 0
156 0 $ua->timeout($self->timeout);
157 }
158  
159 0 ▶ my $evtparams = delete($params->{params}) || {};
Condition0%0/2
delete $params->{'params'} || {}
l!l
00
MC/DC0%0/1
delete $params->{'params'} || {}
delete $params->{'params'}
Truth table0%0/2
A
A= delete $params->{'params'}
Aresult
11
00
160 my $query = join('&',
161 0 map({ "$_=".$params->{$_} } keys(%{ $params })),
162 0 map({ "parameter.$_=$evtparams->{$_}" } keys %$evtparams)
163 );
164  
165 0 my $req = HTTP::Request->new(GET => $self->url->canonical.'/event?'.$query);
166  
167 0 my $res = $ua->request($req);
168  
169 0 ▶ if($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
170 0 $self->list(Osgood::EventList->thaw($res->content));
171 0 return 1;
172 } else {
173 0 $self->error($res->status_line);
174 0 return 0;
175 }
176 }
177  
178 =head2 timeout
179  
180 The number of seconds to wait before timing out.
181  
182 =head2 url
183  
184 The url of the Osgood queue we should contact. Expects an instance of URI.
185  
186 =head2 error
187  
188 Returns the error message (if there was one) for this client. This should
189 be called if C<query> or C<send> do not return what you expect.
190  
191 =head1 PERFORMANCE
192  
193 Originally Osgood used a combination of XML::DOM and XML::XPath for
194 serialization. After some testing it has switched to using JSON, as JSON::XS
195 is considerably faster. In tests on my machine (dual quad-core xeon) it takes
196 about 10 seconds to deserialize 10_000 simple events.
197  
198 Please keep in mind that the sending of events will also have a cost, as
199 insertion into the database takes time. See the accompanying PERFORMANCE
200 section of L<Osgood::Server>.
201  
202 =head1 AUTHOR
203  
204 Cory 'G' Watson <gphat@cpan.org>
205  
206 =head1 CONTRIBUTORS
207  
208 Mike Eldridge (diz)
209  
210 =head1 SEE ALSO
211  
212 perl(1), Osgood::Event, Osgood::EventList
213  
214 =head1 COPYRIGHT AND LICENSE
215  
216 Copyright 2008-2009 by Magazines.com, LLC
217  
218 You can redistribute and/or modify this code under the same terms as Perl
219 itself.
220  
221 =cut
222  
223 2 __PACKAGE__->meta->make_immutable;
224  
225 2 1;
↑ Summary EventList.pm »