| 1 | package WebService::Ares; | ||||||||||
| 2 | |||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 5 | ▶ | use Class::Utils qw(set_params); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Error::Pure qw(err); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 5 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 5 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 5 | our $VERSION = 0.03; | |||||||||
| 12 | |||||||||||
| 13 | # Constructor. | ||||||||||
| 14 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 15 | 5 | my ($class, @params) = @_; | |||||||||
| 16 | |||||||||||
| 17 | # Create object. | ||||||||||
| 18 | 5 | my $self = bless {}, $class; | |||||||||
| 19 | |||||||||||
| 20 | # User agent. | ||||||||||
| 21 | 5 | $self->{'agent'} = 'WebService::Ares/'.$VERSION; | |||||||||
| 22 | |||||||||||
| 23 | # Debug. | ||||||||||
| 24 | 5 | $self->{'debug'} = 0; | |||||||||
| 25 | |||||||||||
| 26 | # Params. | ||||||||||
| 27 | 5 | set_params($self, @params); | |||||||||
| 28 | |||||||||||
| 29 | # Commands. | ||||||||||
| 30 | 3 | $self->{'commands'} = { | |||||||||
| 31 | 'standard' => { | ||||||||||
| 32 | 'attr' => [ | ||||||||||
| 33 | 'ic', | ||||||||||
| 34 | ], | ||||||||||
| 35 | 'method' => 'GET', | ||||||||||
| 36 | 'url' => 'http://wwwinfo.mfcr.cz/cgi-bin/ares'. | ||||||||||
| 37 | '/darv_std.cgi', | ||||||||||
| 38 | }, | ||||||||||
| 39 | }; | ||||||||||
| 40 | |||||||||||
| 41 | # Error string. | ||||||||||
| 42 | 3 | $self->{'error'} = undef; | |||||||||
| 43 | |||||||||||
| 44 | # User agent. | ||||||||||
| 45 | 3 | $self->{'ua'} = LWP::UserAgent->new; | |||||||||
| 46 | 3 | $self->{'ua'}->agent($self->{'agent'}); | |||||||||
| 47 | |||||||||||
| 48 | # Object. | ||||||||||
| 49 | 3 | return $self; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | # Get web service commands. | ||||||||||
| 53 | 1 | ▶ | sub commands { | ||||||||
|
Subroutine
commands: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 54 | 1 | my $self = shift; | |||||||||
| 55 | 1 | return sort keys %{$self->{'commands'}}; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | # Get error. | ||||||||||
| 59 | 1 | ▶ | sub error { | ||||||||
|
Subroutine
error: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 60 | 1 | my ($self, $clean) = @_; | |||||||||
| 61 | 1 | my $error = $self->{'error'}; | |||||||||
| 62 | 1 | ▶ | if ($clean) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | $self->{'error'} = undef; | |||||||||
| 64 | } | ||||||||||
| 65 | 1 | return $error; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | # Get data. | ||||||||||
| 69 | 0 | ▶ | sub get { | ||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 70 | 0 | my ($self, $command, $def_hr) = @_; | |||||||||
| 71 | |||||||||||
| 72 | # Get XML data. | ||||||||||
| 73 | 0 | my $data = $self->get_xml($command, $def_hr); | |||||||||
| 74 | |||||||||||
| 75 | # Parse XML. | ||||||||||
| 76 | 0 | my $data_hr; | |||||||||
| 77 | 0 | ▶ | if ($command eq 'standard') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | require WebService::Ares::Standard; | |||||||||
| 79 | 0 | $data_hr = WebService::Ares::Standard::parse($data); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | # Result. | ||||||||||
| 83 | 0 | return $data_hr; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | # Get XML file. | ||||||||||
| 87 | 0 | ▶ | sub get_xml { | ||||||||
|
Subroutine
get_xml: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 88 | 0 | my ($self, $command, $def_hr) = @_; | |||||||||
| 89 | |||||||||||
| 90 | # Command structure. | ||||||||||
| 91 | 0 | my $c_hr = $self->{'commands'}->{$command}; | |||||||||
| 92 | |||||||||||
| 93 | # Create url. | ||||||||||
| 94 | 0 | my $url = $c_hr->{'url'}; | |||||||||
| 95 | 0 | foreach my $key (keys %{$def_hr}) { | |||||||||
| 96 | # TODO Control | ||||||||||
| 97 | # TODO Better create. | ||||||||||
| 98 | 0 | ▶ | if ($key eq 'ic') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | $url .= '?ico='.$def_hr->{$key}; | |||||||||
| 100 | } | ||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | # Get XML data. | ||||||||||
| 104 | 0 | return $self->_get_page($c_hr->{'method'}, $url); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | # Get page. | ||||||||||
| 108 | 0 | ▶ | sub _get_page { | ||||||||
|
Subroutine
_get_page: not called CC=4 SCAR=30.0
| |||||||||||
| 109 | 0 | my ($self, $method, $url) = @_; | |||||||||
| 110 | |||||||||||
| 111 | # Debug. | ||||||||||
| 112 | 0 | ▶ | if ($self->{'debug'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | print "Method: $method\n"; | |||||||||
| 114 | 0 | print "URL: $url\n"; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | # Request. | ||||||||||
| 118 | 0 | my $req; | |||||||||
| 119 | 0 | ▶ | if ($method eq 'GET') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | $req = HTTP::Request->new('GET' => $url); | |||||||||
| 121 | } else { | ||||||||||
| 122 | 0 | err "Method '$method' is unimplemenited."; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | # Response. | ||||||||||
| 126 | 0 | my $res = $self->{'ua'}->request($req); | |||||||||
| 127 | |||||||||||
| 128 | # Get page. | ||||||||||
| 129 | 0 | ▶ | if ($res->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | return $res->content; | |||||||||
| 131 | } else { | ||||||||||
| 132 | 0 | $self->{'error'} = $res->status_line; | |||||||||
| 133 | 0 | return; | |||||||||
| 134 | } | ||||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 5 | 1; | |||||||||
| 138 | |||||||||||
| 139 | __END__ | ||||||||||