| 1 | package WebService::CIA::Source::Web; | ||
| 2 | |||
| 3 | require 5.005_62; | ||
| 4 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use Crypt::SSLeay; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use WebService::CIA; | ||
| 10 | use WebService::CIA::Parser; | ||
| 11 | use WebService::CIA::Source; | ||
| 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 | my $proto = shift; | ||
| 22 | my $class = ref($proto) || $proto; | ||
| 23 | my $args = shift || {}; | ||
| 24 | if ( ! ref $args || ref $args ne "HASH" ) { | ||
| 25 | croak "Arguments to new() must be a hashref"; | ||
| 26 | } | ||
| 27 | my $self = {}; | ||
| 28 | $self->{CACHED} = ""; | ||
| 29 | $self->{CACHE} = {}; | ||
| 30 | $self->{PARSER} = WebService::CIA::Parser->new; | ||
| 31 | bless ($self, $class); | ||
| 32 | if ( exists $args->{ user_agent } ) { | ||
| 33 | $self->ua( $args->{ user_agent } ); | ||
| 34 | } | ||
| 35 | return $self; | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | sub value { | ||
| 40 | |||
| 41 | my $self = shift; | ||
| 42 | my ($cc, $f) = @_; | ||
| 43 | |||
| 44 | unless ($self->cached eq $cc) { | ||
| 45 | unless ($self->get($cc)) { | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | if (exists $self->cache->{$f}) { | ||
| 51 | return $self->cache->{$f}; | ||
| 52 | } else { | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | } | ||
| 57 | |||
| 58 | sub all { | ||
| 59 | |||
| 60 | my $self = shift; | ||
| 61 | my $cc = shift; | ||
| 62 | |||
| 63 | unless ($self->cached eq $cc) { | ||
| 64 | unless ($self->get($cc)) { | ||
| 65 | return {}; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | return $self->cache; | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | sub get { | ||
| 74 | |||
| 75 | my $self = shift; | ||
| 76 | my $cc = shift; | ||
| 77 | my $response = $self->ua->get($WebService::CIA::base_url . "print/$cc.html"); | ||
| 78 | $self->last_response( $response ); | ||
| 79 | if ($response->is_success) { | ||
| 80 | my $data = $self->parser->parse($cc, $response->content); | ||
| 81 | $self->cache($data); | ||
| 82 | $self->cached($cc); | ||
| 83 | return 1; | ||
| 84 | } else { | ||
| 85 | return 0; | ||
| 86 | } | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | sub ua { | ||
| 91 | |||
| 92 | my ( $self, $ua ) = @_; | ||
| 93 | if ( defined $ua ) { | ||
| 94 | $self->{ UA } = $ua; | ||
| 95 | } | ||
| 96 | if ( ! defined $self->{ UA } ) { | ||
| 97 | $self->{ UA } = LWP::UserAgent->new; | ||
| 98 | $self->{ UA }->env_proxy; | ||
| 99 | } | ||
| 100 | return $self->{UA}; | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | sub parser { | ||
| 105 | |||
| 106 | my $self = shift; | ||
| 107 | return $self->{PARSER}; | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | sub cached { | ||
| 112 | |||
| 113 | my $self = shift; | ||
| 114 | if (@_) { | ||
| 115 | $self->{CACHED} = shift; | ||
| 116 | } | ||
| 117 | return $self->{CACHED}; | ||
| 118 | |||
| 119 | } | ||
| 120 | |||
| 121 | sub cache { | ||
| 122 | |||
| 123 | my $self = shift; | ||
| 124 | if (@_) { | ||
| 125 | $self->{CACHE} = shift; | ||
| 126 | } | ||
| 127 | return $self->{CACHE}; | ||
| 128 | |||
| 129 | } | ||
| 130 | |||
| 131 | sub last_response { | ||
| 132 | my ( $self, $response ) = @_; | ||
| 133 | if ( defined $response ) { | ||
| 134 | $self->{ LAST_RESPONSE } = $response; | ||
| 135 | } | ||
| 136 | return $self->{ LAST_RESPONSE }; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | __END__ | ||