File Coverage

blib/lib/WebService/CIA/Source/Web.pm
Criterion Covered Total %
statement 82 82 100.0
branch 24 26 92.3
condition 5 8 62.5
subroutine 17 17 100.0
pod 9 9 100.0
total 137 142 96.4


line stmt bran cond sub pod time code
1             package WebService::CIA::Source::Web;
2              
3             require 5.005_62;
4 2     2   492764 use strict;
  2         7  
  2         1174  
5 2     2   18 use warnings;
  2         3  
  2         74  
6 2     2   16 use Carp;
  2         7  
  2         184  
7 2     2   1114 use LWP::UserAgent;
  2         125931  
  2         63  
8 2     2   7153 use Crypt::SSLeay;
  2         10059  
  2         104  
9 2     2   1474 use WebService::CIA;
  2         7  
  2         60  
10 2     2   1865 use WebService::CIA::Parser;
  2         6  
  2         65  
11 2     2   1328 use WebService::CIA::Source;
  2         6  
  2         1590  
12              
13             @WebService::CIA::Source::Web::ISA = ("WebService::CIA::Source");
14              
15             our $VERSION = '1.4';
16              
17             # Preloaded methods go here.
18              
19             sub new {
20              
21 3     3 1 97635 my $proto = shift;
22 3   33     47 my $class = ref($proto) || $proto;
23 3   100     111 my $args = shift || {};
24 3 100 66     83 if ( ! ref $args || ref $args ne "HASH" ) {
25 1         762 croak "Arguments to new() must be a hashref";
26             }
27 2         23 my $self = {};
28 2         17 $self->{CACHED} = "";
29 2         83 $self->{CACHE} = {};
30 2         45 $self->{PARSER} = WebService::CIA::Parser->new;
31 2         10 bless ($self, $class);
32 2 100       56 if ( exists $args->{ user_agent } ) {
33 1         5 $self->ua( $args->{ user_agent } );
34             }
35 2         9 return $self;
36              
37             }
38              
39             sub value {
40              
41 5     5 1 10 my $self = shift;
42 5         23 my ($cc, $f) = @_;
43              
44 5 100       92 unless ($self->cached eq $cc) {
45 1 50       3 unless ($self->get($cc)) {
46 1         4 return;
47             }
48             }
49              
50 4 100       13 if (exists $self->cache->{$f}) {
51 2         7 return $self->cache->{$f};
52             } else {
53 2         12 return;
54             }
55              
56             }
57              
58             sub all {
59              
60 7     7 1 21 my $self = shift;
61 7         21 my $cc = shift;
62              
63 7 100       17 unless ($self->cached eq $cc) {
64 1 50       3 unless ($self->get($cc)) {
65 1         19 return {};
66             }
67             }
68              
69 6         15 return $self->cache;
70              
71             }
72              
73             sub get {
74              
75 4     4 1 2615 my $self = shift;
76 4         7 my $cc = shift;
77 4         15 my $response = $self->ua->get($WebService::CIA::base_url . "print/$cc.html");
78 4         4564 $self->last_response( $response );
79 4 100       16 if ($response->is_success) {
80 1         13 my $data = $self->parser->parse($cc, $response->content);
81 1         8 $self->cache($data);
82 1         5 $self->cached($cc);
83 1         9 return 1;
84             } else {
85 3         34 return 0;
86             }
87              
88             }
89              
90             sub ua {
91              
92 9     9 1 2465 my ( $self, $ua ) = @_;
93 9 100       37 if ( defined $ua ) {
94 2         6 $self->{ UA } = $ua;
95             }
96 9 100       85 if ( ! defined $self->{ UA } ) {
97 1         40 $self->{ UA } = LWP::UserAgent->new;
98 1         554511 $self->{ UA }->env_proxy;
99             }
100 9         2352405 return $self->{UA};
101              
102             }
103              
104             sub parser {
105              
106 2     2 1 5 my $self = shift;
107 2         29 return $self->{PARSER};
108              
109             }
110              
111             sub cached {
112              
113 17     17 1 541 my $self = shift;
114 17 100       127 if (@_) {
115 2         6 $self->{CACHED} = shift;
116             }
117 17         61 return $self->{CACHED};
118              
119             }
120              
121             sub cache {
122              
123 20     20 1 30 my $self = shift;
124 20 100       45 if (@_) {
125 2         7 $self->{CACHE} = shift;
126             }
127 20         86 return $self->{CACHE};
128              
129             }
130              
131             sub last_response {
132 6     6 1 11 my ( $self, $response ) = @_;
133 6 100       15 if ( defined $response ) {
134 4         10 $self->{ LAST_RESPONSE } = $response;
135             }
136 6         17 return $self->{ LAST_RESPONSE };
137             }
138              
139             1;
140              
141             __END__