blib/lib/Net/WOT.pm

filter: Statementstmt 80.0% 8 / 10 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 85.7% 12 / 14 CC 1 SCAR 0.7
CC
1
Cov
80%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Net::WOT;
2 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 $Net::WOT::VERSION = '0.02';
4 }
5 # ABSTRACT: Access Web of Trust (WOT) API
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use LWP::UserAgent;
11 use namespace::autoclean;
12  
13 # useragent to work with
14 has 'useragent' => (
15 is => 'ro',
16 isa => 'LWP::UserAgent',
17 handles => { ua_get => 'get' },
18 lazy_build => 1,
19 );
20  
21 # docs are at: http://www.mywot.com/wiki/API
22 has api_base_url => (
23 is => 'ro',
24 isa => 'Str',
25 default => 'api.mywot.com',
26 );
27  
28 has api_path => (
29 is => 'ro',
30 isa => 'Str',
31 default => 'public_query2',
32 );
33  
34 has version => (
35 is => 'ro',
36 isa => 'Num',
37 default => 0.4,
38 );
39  
40 has components => (
41 is => 'ro',
42 isa => 'HashRef[Str]',
43 traits => ['Hash'],
44 default => sub { {
45 0 => 'trustworthiness',
46 1 => 'vendor_reliability',
47 2 => 'privacy',
48 4 => 'child_safety',
49 } },
50  
51 handles => {
52 get_component_name => 'get',
53 get_all_component_names => 'values',
54 },
55 );
56  
57 has reputation_levels => (
58 is => 'ro',
59 isa => 'HashRef[Str]',
60 traits => ['Hash'],
61 default => sub { {
62 80 => 'excellent',
63 60 => 'good',
64 40 => 'unsatisfactory',
65 20 => 'poor',
66 0 => 'very poor',
67 } },
68  
69 handles => {
70 get_reputation_description => 'get',
71 get_reputation_levels => 'keys',
72 },
73 );
74  
75 has confidence_levels => (
76 is => 'ro',
77 isa => 'HashRef[Str]',
78 traits => ['Hash'],
79 default => sub { {
80 45 => '5',
81 34 => '4',
82 23 => '3',
83 12 => '2',
84 6 => '1',
85 0 => '0',
86 } },
87  
88 handles => {
89 get_confidence_level => 'get',
90 get_all_confidence_levels => 'values',
91 },
92 );
93  
94 # automatically create all reputation component attributes
95 foreach my $comp ( qw/
96 trustworthiness
97 vendor_reliability
98 privacy
99 child_safety
100 / ) {
101 foreach my $item ( qw/ score confidence / ) {
102 my $attr_name = "${comp}_$item";
103 has $attr_name => ( is => 'rw', isa => 'Int' );
104 }
105  
106 has "${comp}_description" => ( is => 'rw', isa => 'Str' );
107 }
108  
109 sub _build_useragent {
110 my $self = shift;
111 my $lwp = LWP::UserAgent->new();
112  
113 return $lwp;
114 }
115  
116 sub _create_link {
117 my ( $self, $target ) = @_;
118 my $version = $self->version;
119 my $api_base = $self->api_base_url;
120 my $api_path = $self->api_path;
121 my $link = "http://$api_base/$version/$api_path?target=$target";
122  
123 return $link;
124 }
125  
126 # <?xml version="1.0" encoding="UTF-8"?>
127 # <query target="google.com">
128 # <application c="93" name="0" r="94"/>
129 # <application c="92" name="1" r="95"/>
130 # <application c="88" name="2" r="93"/>
131 # <application c="88" name="4" r="93"/>
132 # </query>
133  
134 sub _request_wot {
135 my ( $self, $target ) = @_;
136 my $link = $self->_create_link($target);
137 my $response = $self->ua_get($link);
138 my $status = $response->status_line;
139  
140 $response->is_success or croak "Can't get reputation: $status\n";
141  
142 return $response->content;
143 }
144  
145 sub get_reputation {
146 my ( $self, $target ) = @_;
147 my $xml = $self->_request_wot($target);
148 my $twig = XML::Twig->new();
149  
150 $twig->parse($xml);
151  
152 my @children = $twig->root->children;
153 foreach my $child (@children) {
154 # checking a specific query
155 my $component = $child->att('name');
156 my $confidence = $child->att('c');
157 my $reputation = $child->att('r');
158  
159 my $component_name = $self->get_component_name($component);
160  
161 # component: 0
162 # confidence: 34
163 # reputation: 30
164 # trustworthiness_reputation
165 # trustworthiness_description
166 # trustworthiness_confidence
167  
168 my $score_attr = "${component_name}_score";
169 $self->$score_attr($reputation);
170  
171 my $conf_attr = "${component_name}_confidence";
172 $self->$conf_attr($confidence);
173  
174 my @rep_levels = sort { $b <=> $a } $self->get_reputation_levels;
175 my $desc_attr = "${component_name}_description";
176  
177 foreach my $reputation_level (@rep_levels) {
178 if ( $reputation >= $reputation_level ) {
179 my $rep_desc
180 = $self->get_reputation_description($reputation_level);
181  
182 $self->$desc_attr($rep_desc);
183  
184 last;
185 }
186 }
187 }
188  
189 return $self->_create_reputation_hash;
190 }
191  
192 sub _create_reputation_hash {
193 my $self = shift;
194 my %hash = ();
195  
196 foreach my $component ( $self->get_all_component_names ) {
197 foreach my $item ( qw/ score description confidence / ) {
198 my $attr = "${component}_$item";
199 my $value = $self->$attr;
200  
201 $value and $hash{$component}{$item} = $value;
202 }
203 }
204  
205 return %hash;
206 }
207  
208 no Moose;
209 __PACKAGE__->meta->make_immutable;
210  
211 1;
212  
213  
214  
215 =pod
216  
217 =head1 NAME
218  
219 Net::WOT - Access Web of Trust (WOT) API
220  
221 =head1 VERSION
222  
223 version 0.02
224  
225 =head1 SYNOPSIS
226  
227 This module provides an interface to I<Web of Trust>'s API.
228  
229 use Net::WOT;
230  
231 my $wot = Net::WOT->new;
232  
233 # get all details
234 my %all_details = $wot->get_reputation('example.com');
235  
236 # use specific details after get_reputations() method was called
237 print $wot->privacy_score, "\n";
238  
239 =head1 EXPORT
240  
241 Fully object oriented, nothing is exported.
242  
243 =head1 ATTRIBUTES
244  
245 These are attributes that can be set during the initialization of the WOT
246 object. The syntax is:
247  
248 my $wot = Net::WOT->new(
249 attr1 => 'value1',
250 attr2 => 'value2',
251 );
252  
253 =head2 api_base_url
254  
255 The basic url for the WOT API. Default: B<api.mywot.com>.
256  
257 =head2 api_path
258  
259 The path for the WOT API request. Default: B<public_query2>.
260  
261 =head2 version
262  
263 Version of the WOT API. Default: B<0.4>.
264  
265 These are subroutines you probably don't want to change but can still read from.
266  
267 B<Note:> Changing these might compromise the integrity of your information,
268 consider them as read-only.
269  
270 =head2 trustworthiness_score
271  
272 The trustworthiness score.
273  
274 =head2 trustworthiness_confidence
275  
276 The trustworthiness confidence.
277  
278 =head2 trustworthiness_description
279  
280 The trustworthiness description.
281  
282 =head2 vendor_reliability_score
283  
284 The vendor reliability score.
285  
286 =head2 vendor_reliability_confidence
287  
288 The vendor reliability confidence.
289  
290 =head2 vendor_reliability_description
291  
292 The vendor reliability description.
293  
294 =head2 privacy_score
295  
296 The privacy score.
297  
298 =head2 privacy_confidence
299  
300 The privacy confidence.
301  
302 =head2 privacy_description
303  
304 The privacy description.
305  
306 =head2 child_safety_score
307  
308 The child safety score.
309  
310 =head2 child_safety_confidence
311  
312 The child safety confidence.
313  
314 =head2 child_safety_description
315  
316 The child safety description.
317  
318 =head1 SUBROUTINES/METHODS
319  
320 =head2 get_reputation
321  
322 Get reputation.
323  
324 =head2 ua_get
325  
326 This is a shorthand to reach an internal useragent I<get> command. Why would you
327 want it? Who knows? It's there.
328  
329 =head2 get_component_name
330  
331 Retrieves a component name from the index number of it. For example:
332  
333 my $name = $wot->get_component_name(2);
334 # $name = 'privacy'
335  
336 =head2 get_all_component_names
337  
338 Returns a list of all component names.
339  
340 =head2 get_reputation_description
341  
342 Retrieves a reputation description from a certain level threshold. For example:
343  
344 my $threshold = 60;
345 my $description = $wot->get_reputation_description;
346  
347 # $description = 'good'
348  
349 =head2 get_reputation_levels
350  
351 Returns a list of all reputation levels.
352  
353 =head2 get_confidence_level
354  
355 Retrieves a confidence level from a certain threshold. For example:
356  
357 my $confidence_level = $wot->get_confidence_level(12);
358 # $confidence_level = '2'
359  
360 =head2 get_all_confidence_levels
361  
362 Returns a list of all confidence levels.
363  
364 =head1 AUTHOR
365  
366 Sawyer X, C<< <xsawyerx at cpan.org> >>
367  
368 =head1 BUGS
369  
370 Please report bugs and other issues on the bugtracker:
371  
372 L<http://github.com/xsawyerx/net-wot/issues>
373  
374 =head1 SUPPORT
375  
376 Hopefully.
377  
378 =head1 LICENSE AND COPYRIGHT
379  
380 Copyright 2010 Sawyer X.
381  
382 This program is free software; you can redistribute it and/or modify it
383 under the terms of either: the GNU General Public License as published
384 by the Free Software Foundation; or the Artistic License.
385  
386 See http://dev.perl.org/licenses/ for more information.
387  
388 =head1 AUTHOR
389  
390 Sawyer X <xsawyerx@cpan.org>
391  
392 =head1 COPYRIGHT AND LICENSE
393  
394 This software is copyright (c) 2010 by Sawyer X.
395  
396 This is free software; you can redistribute it and/or modify it under
397 the same terms as the Perl 5 programming language system itself.
398  
399 =cut
400  
401  
402 __END__
↑ Summary