line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: /mirror/perl/WebService-Gnavi/trunk/lib/WebService/Gnavi.pm 7171 2007-05-11T09:10:30.913520Z daisuke $ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (c) 2007 Daisuke Maki |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package WebService::Gnavi; |
6
|
1
|
|
|
1
|
|
29777
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
61
|
|
7
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
8
|
1
|
|
|
1
|
|
1084
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
78150
|
|
|
1
|
|
|
|
|
34
|
|
9
|
1
|
|
|
1
|
|
10
|
use URI; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
10
|
1
|
|
|
1
|
|
632
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use WebService::Gnavi::SearchResult; |
12
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
13
|
|
|
|
|
|
|
our $BASE_URI = URI->new('http://api.gnavi.co.jp/'); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new |
16
|
|
|
|
|
|
|
{ |
17
|
|
|
|
|
|
|
my $class = shift; |
18
|
|
|
|
|
|
|
my %args = @_; |
19
|
|
|
|
|
|
|
$args{access_key} || die "WebService::Gnavi requires an access_key"; |
20
|
|
|
|
|
|
|
bless { |
21
|
|
|
|
|
|
|
version => 'ver1', |
22
|
|
|
|
|
|
|
access_key => $args{access_key}, |
23
|
|
|
|
|
|
|
}, $class; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub _user_agent |
27
|
|
|
|
|
|
|
{ |
28
|
|
|
|
|
|
|
my $self = shift; |
29
|
|
|
|
|
|
|
$self->{_lwp} ||= LWP::UserAgent->new(agent => "WebService::Gnavi/$VERSION"); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _libxml |
33
|
|
|
|
|
|
|
{ |
34
|
|
|
|
|
|
|
my $self = shift; |
35
|
|
|
|
|
|
|
$self->{_libxml} ||= XML::LibXML->new; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub search |
39
|
|
|
|
|
|
|
{ |
40
|
|
|
|
|
|
|
my $self = shift; |
41
|
|
|
|
|
|
|
my $args = shift; |
42
|
|
|
|
|
|
|
my $uri = $BASE_URI->clone; |
43
|
|
|
|
|
|
|
$uri->path("/$self->{version}/RestSearchAPI/"); |
44
|
|
|
|
|
|
|
$uri->query_form({ |
45
|
|
|
|
|
|
|
keyid => $self->{access_key}, |
46
|
|
|
|
|
|
|
%$args |
47
|
|
|
|
|
|
|
}); |
48
|
|
|
|
|
|
|
my $request = HTTP::Request->new(GET => $uri); |
49
|
|
|
|
|
|
|
$self->send_request('search', $request); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub areas |
53
|
|
|
|
|
|
|
{ |
54
|
|
|
|
|
|
|
my $self = shift; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $uri = $BASE_URI->clone; |
57
|
|
|
|
|
|
|
$uri->path("/$self->{version}/AreaSearchAPI/"); |
58
|
|
|
|
|
|
|
$uri->query_form({ |
59
|
|
|
|
|
|
|
keyid => $self->{access_key} |
60
|
|
|
|
|
|
|
}); |
61
|
|
|
|
|
|
|
my $request = HTTP::Request->new(GET => $uri); |
62
|
|
|
|
|
|
|
$self->send_request('areas', $request); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub prefectures |
66
|
|
|
|
|
|
|
{ |
67
|
|
|
|
|
|
|
my $self = shift; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $uri = $BASE_URI->clone; |
70
|
|
|
|
|
|
|
$uri->path("/$self->{version}/PrefSearchAPI/"); |
71
|
|
|
|
|
|
|
$uri->query_form({ |
72
|
|
|
|
|
|
|
keyid => $self->{access_key} |
73
|
|
|
|
|
|
|
}); |
74
|
|
|
|
|
|
|
my $request = HTTP::Request->new(GET => $uri); |
75
|
|
|
|
|
|
|
$self->send_request('prefectures', $request); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub category_large |
79
|
|
|
|
|
|
|
{ |
80
|
|
|
|
|
|
|
my $self = shift; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $uri = $BASE_URI->clone; |
83
|
|
|
|
|
|
|
$uri->path("/$self->{version}/CategoryLargeSearchAPI/"); |
84
|
|
|
|
|
|
|
$uri->query_form({ |
85
|
|
|
|
|
|
|
keyid => $self->{access_key} |
86
|
|
|
|
|
|
|
}); |
87
|
|
|
|
|
|
|
my $request = HTTP::Request->new(GET => $uri); |
88
|
|
|
|
|
|
|
$self->send_request('category_large', $request); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub category_small |
92
|
|
|
|
|
|
|
{ |
93
|
|
|
|
|
|
|
my $self = shift; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $uri = $BASE_URI->clone; |
96
|
|
|
|
|
|
|
$uri->path("/$self->{version}/CategorySmallSearchAPI/"); |
97
|
|
|
|
|
|
|
$uri->query_form({ |
98
|
|
|
|
|
|
|
keyid => $self->{access_key} |
99
|
|
|
|
|
|
|
}); |
100
|
|
|
|
|
|
|
my $request = HTTP::Request->new(GET => $uri); |
101
|
|
|
|
|
|
|
$self->send_request('category_small', $request); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub send_request |
105
|
|
|
|
|
|
|
{ |
106
|
|
|
|
|
|
|
my ($self, $type, $req) = @_; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $ua = $self->_user_agent(); |
109
|
|
|
|
|
|
|
my $res = $ua->request($req); |
110
|
|
|
|
|
|
|
return $self->_parse_response($type, $res); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub _parse_response |
114
|
|
|
|
|
|
|
{ |
115
|
|
|
|
|
|
|
my ($self, $type, $res) = @_; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my $parser = $self->_libxml(); |
118
|
|
|
|
|
|
|
my $xml = $parser->parse_string($res->content); |
119
|
|
|
|
|
|
|
my ($code) = $xml->findnodes('/gnavi/error/code'); |
120
|
|
|
|
|
|
|
if ($code) { |
121
|
|
|
|
|
|
|
die WebService::Gnavi::Exception->new(code => $code->textContent()); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
if ($type eq 'search') { |
125
|
|
|
|
|
|
|
return WebService::Gnavi::SearchResult->parse($xml); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
my $method = "_parse_$type"; |
129
|
|
|
|
|
|
|
$self->$method($xml); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub _parse_areas |
133
|
|
|
|
|
|
|
{ |
134
|
|
|
|
|
|
|
my ($self, $xml) = @_; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my @list; |
137
|
|
|
|
|
|
|
foreach my $a ($xml->findnodes('/response/area')) { |
138
|
|
|
|
|
|
|
push @list, { |
139
|
|
|
|
|
|
|
area_code => $a->findvalue('area_code'), |
140
|
|
|
|
|
|
|
area_name => $a->findvalue('area_name') |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
return @list; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub _parse_prefectures |
147
|
|
|
|
|
|
|
{ |
148
|
|
|
|
|
|
|
my ($self, $xml) = @_; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
my @list; |
151
|
|
|
|
|
|
|
foreach my $a ($xml->findnodes('/response/pref')) { |
152
|
|
|
|
|
|
|
push @list, { |
153
|
|
|
|
|
|
|
pref_code => $a->findvalue('pref_code'), |
154
|
|
|
|
|
|
|
pref_name => $a->findvalue('pref_name'), |
155
|
|
|
|
|
|
|
area_code => $a->findvalue('area_code'), |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
return @list; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub _parse_category_large |
162
|
|
|
|
|
|
|
{ |
163
|
|
|
|
|
|
|
my ($self, $xml) = @_; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
my @list; |
166
|
|
|
|
|
|
|
foreach my $a ($xml->findnodes('/response/category_l')) { |
167
|
|
|
|
|
|
|
push @list, { |
168
|
|
|
|
|
|
|
map { ($_ => $a->findvalue($_)) } |
169
|
|
|
|
|
|
|
qw(category_l_code category_l_name) |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
return @list; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub _parse_category_small |
176
|
|
|
|
|
|
|
{ |
177
|
|
|
|
|
|
|
my ($self, $xml) = @_; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
my @list; |
180
|
|
|
|
|
|
|
foreach my $a ($xml->findnodes('/response/category_s')) { |
181
|
|
|
|
|
|
|
push @list, { |
182
|
|
|
|
|
|
|
map { ($_ => $a->findvalue($_)) } |
183
|
|
|
|
|
|
|
qw(category_s_code category_s_name category_l_code) |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
return @list; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
package WebService::Gnavi::Exception; |
190
|
|
|
|
|
|
|
use strict; |
191
|
|
|
|
|
|
|
use warnings; |
192
|
|
|
|
|
|
|
use overload |
193
|
|
|
|
|
|
|
"" => \&as_string |
194
|
|
|
|
|
|
|
; |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub new |
197
|
|
|
|
|
|
|
{ |
198
|
|
|
|
|
|
|
my $class = shift; |
199
|
|
|
|
|
|
|
my %args = @_; |
200
|
|
|
|
|
|
|
bless { %args }, $class; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub code { shift->{code} } |
204
|
|
|
|
|
|
|
sub as_string { shift->{code} } |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
__END__ |