line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
######### |
2
|
|
|
|
|
|
|
# Author: rmp |
3
|
|
|
|
|
|
|
# Maintainer: $Author: rmp $ |
4
|
|
|
|
|
|
|
# Created: 2008-08-13 |
5
|
|
|
|
|
|
|
# Last Modified: $Date$ |
6
|
|
|
|
|
|
|
# Id: $Id$ |
7
|
|
|
|
|
|
|
# $HeadURL$ |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
package Net::Plazes::Base; |
10
|
3
|
|
|
3
|
|
16
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
79
|
|
11
|
3
|
|
|
3
|
|
13
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
71
|
|
12
|
3
|
|
|
3
|
|
14
|
use base qw(Class::Accessor); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
3799
|
|
13
|
3
|
|
|
3
|
|
6721
|
use Carp; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
211
|
|
14
|
3
|
|
|
3
|
|
14
|
use English qw(-no_match_vars); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
22
|
|
15
|
3
|
|
|
3
|
|
3830
|
use HTTP::Request; |
|
3
|
|
|
|
|
142748
|
|
|
3
|
|
|
|
|
107
|
|
16
|
3
|
|
|
3
|
|
3968
|
use LWP::UserAgent; |
|
3
|
|
|
|
|
79785
|
|
|
3
|
|
|
|
|
112
|
|
17
|
3
|
|
|
3
|
|
1907
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use Lingua::EN::Inflect qw(PL); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(fields()); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub new { |
25
|
|
|
|
|
|
|
my ($class, $ref) = @_; |
26
|
|
|
|
|
|
|
$ref ||= {}; |
27
|
|
|
|
|
|
|
bless $ref, $class; |
28
|
|
|
|
|
|
|
return $ref; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub service { |
32
|
|
|
|
|
|
|
return q[]; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub doc_element { |
36
|
|
|
|
|
|
|
my $self = shift; |
37
|
|
|
|
|
|
|
my ($ref) = (ref $self || $self) =~ /([^:]+)$/mx; |
38
|
|
|
|
|
|
|
my $el = lc $ref; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return $el; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub fields { |
44
|
|
|
|
|
|
|
return (); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub useragent { |
48
|
|
|
|
|
|
|
my $self = shift; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
if(!$self->{useragent}) { |
51
|
|
|
|
|
|
|
$self->{useragent} = LWP::UserAgent->new(); |
52
|
|
|
|
|
|
|
$self->{useragent}->env_proxy(); |
53
|
|
|
|
|
|
|
$self->{useragent}->agent(qq[Net::Plazes v$VERSION]); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
return $self->{useragent}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub parser { |
60
|
|
|
|
|
|
|
my $self = shift; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
if(!$self->{parser}) { |
63
|
|
|
|
|
|
|
$self->{parser} = XML::LibXML->new(); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
return $self->{parser}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub process_dom { |
70
|
|
|
|
|
|
|
my ($self, $obj, $dom) = @_; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
for my $field ($self->fields()) { |
73
|
|
|
|
|
|
|
my $els = [$dom->getElementsByTagName($field)]; |
74
|
|
|
|
|
|
|
if($els->[0]) { |
75
|
|
|
|
|
|
|
my $fc = $els->[0]->getFirstChild(); |
76
|
|
|
|
|
|
|
if($fc) { |
77
|
|
|
|
|
|
|
$obj->{$field} = $fc->getData(); |
78
|
|
|
|
|
|
|
} else { |
79
|
|
|
|
|
|
|
$obj->{$field} = q[]; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
return $obj; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub has_many { |
88
|
|
|
|
|
|
|
my $class = shift; |
89
|
|
|
|
|
|
|
my $plural = PL($class->doc_element()); |
90
|
|
|
|
|
|
|
my $ns = "${class}::$plural"; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
no strict 'refs'; ## no critic |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
*{$ns} = sub { |
95
|
|
|
|
|
|
|
my $self = shift; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
if(!$self->{$plural}) { |
98
|
|
|
|
|
|
|
$self->list(); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
return $self->{$plural}; |
102
|
|
|
|
|
|
|
}; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
use strict; |
105
|
|
|
|
|
|
|
return 1; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub get { |
109
|
|
|
|
|
|
|
my ($self, $field) = @_; |
110
|
|
|
|
|
|
|
if(defined $self->{$field}) { |
111
|
|
|
|
|
|
|
return $self->{$field}; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
if($self->{id}) { |
115
|
|
|
|
|
|
|
$self->read(); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
} else { |
118
|
|
|
|
|
|
|
$self->list(); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
return $self->{$field}; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub read { ## no critic (Subroutines::ProhibitBuiltinHomonyms) |
125
|
|
|
|
|
|
|
my $self = shift; |
126
|
|
|
|
|
|
|
my $obj_uri = sprintf q[%s/%s], $self->service(), $self->id(); |
127
|
|
|
|
|
|
|
if($obj_uri !~ /\.xml$/mx) { |
128
|
|
|
|
|
|
|
$obj_uri .= q[.xml]; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
my $req = HTTP::Request->new('GET', $obj_uri, ['Accept' => 'text/xml']); |
131
|
|
|
|
|
|
|
my $response = $self->useragent->request($req); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
if(!$response->is_success()) { |
134
|
|
|
|
|
|
|
croak $response->status_line() . " fetching $obj_uri"; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
my $dom; |
138
|
|
|
|
|
|
|
eval { |
139
|
|
|
|
|
|
|
$dom = $self->parser->parse_string($response->content()); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
} or do { |
142
|
|
|
|
|
|
|
croak q[Error parsing response] . $response->content(). qq[\nRequest was: $obj_uri]; |
143
|
|
|
|
|
|
|
}; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$self->process_dom($self, $dom); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
return $dom; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub list { |
151
|
|
|
|
|
|
|
my ($self, $obj_uri) = @_; |
152
|
|
|
|
|
|
|
$obj_uri ||= $self->service(); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
if($obj_uri !~ /\.xml$/mx) { |
155
|
|
|
|
|
|
|
$obj_uri .= q[.xml]; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my $req = HTTP::Request->new('GET', $obj_uri, ['Accept' => 'text/xml']); |
159
|
|
|
|
|
|
|
my $response = $self->useragent->request($req); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
my $dom; |
162
|
|
|
|
|
|
|
eval { |
163
|
|
|
|
|
|
|
$dom = $self->parser->parse_string($response->content()); |
164
|
|
|
|
|
|
|
1; |
165
|
|
|
|
|
|
|
} or do { |
166
|
|
|
|
|
|
|
croak q[Error parsing response: ] . $response->content(). qq[\nRequest was: $obj_uri]; |
167
|
|
|
|
|
|
|
}; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
my $objs = []; |
170
|
|
|
|
|
|
|
my $els = [$dom->getElementsByTagName($self->doc_element())]; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
for my $el (@{$els||[]}) { |
173
|
|
|
|
|
|
|
my $pkg = ref $self; |
174
|
|
|
|
|
|
|
push @{$objs}, $self->process_dom($pkg->new(), $el); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
$self->{PL($self->doc_element())} = $objs; |
178
|
|
|
|
|
|
|
return $dom; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
1; |
182
|
|
|
|
|
|
|
__END__ |