File Coverage

blib/lib/WWW/Mixi/OO/Page.pm
Criterion Covered Total %
statement 25 153 16.3
branch 2 44 4.5
condition 2 16 12.5
subroutine 7 41 17.0
pod 35 35 100.0
total 71 289 24.5


) )(?>.*?
line stmt bran cond sub pod time code
1             # -*- cperl -*-
2             # copyright (C) 2005 Topia . all rights reserved.
3             # This is free software; you can redistribute it and/or modify it
4             # under the same terms as Perl itself.
5             # $Id: Page.pm 55 2005-02-01 19:16:17Z topia $
6             # $URL: file:///usr/minetools/svnroot/mixi/trunk/WWW-Mixi-OO/lib/WWW/Mixi/OO/Page.pm $
7             package WWW::Mixi::OO::Page;
8 2     2   14 use strict;
  2         3  
  2         67  
9 2     2   9 use warnings;
  2         4  
  2         44  
10 2     2   9 use Carp;
  2         3  
  2         151  
11 2     2   11 use base qw(Class::Accessor::Fast);
  2         3  
  2         2402  
12 2     2   38788 use base qw(WWW::Mixi::OO::Util);
  2         6  
  2         8151  
13              
14             =head1 NAME
15              
16             WWW::Mixi::OO::Page - WWW::Mixi::OO's Pages base class
17              
18             =head1 SYNOPSIS
19              
20             package WWW::Mixi::OO::Foo;
21             use base qw(WWW::Mixi::OO::Page);
22             sub uri {
23             shift->absolute_uri('foo.pl');
24             }
25             # some implementations...
26              
27             =head1 DESCRIPTION
28              
29             pages base class.
30              
31             =head1 METHODS
32              
33             =over 4
34              
35             =cut
36              
37             =item new
38              
39             # subclass
40             sub new {
41             my $this = shift->SUPER::new(@_);
42             # some initializations...
43             }
44              
45             # call
46             my $page = $pkg->new($session);
47              
48             constructor of page.
49             please override if you need some initializations.
50              
51             =cut
52              
53             sub new {
54 0     0 1 0 my ($class, $session) = @_;
55              
56 0 0       0 croak 'session argument is not found' unless defined $session;
57              
58 0         0 my $this = {
59             session => $session,
60             };
61 0         0 bless $this, $class;
62             }
63              
64             =item uri
65              
66             # subclass
67             sub uri {
68             my ($this, %options) = @_;
69             $this->SUPER::uri(_path => 'foo',
70             _params => {
71             a => b,
72             },
73             %options);
74             }
75              
76             # call
77             my $uri = $page->uri([opt => val], ...);
78              
79             return URI of page.
80              
81             =cut
82              
83             sub uri {
84 0     0 1 0 my $this = shift;
85 0         0 my $options = $this->_init_uri(@_);
86              
87 0         0 my $uri = $this->absolute_uri($options->{_path});
88 0         0 $this->copy_hash_val($options, $options->{_params}, 'id');
89 0         0 $uri->query_form($options->{_params});
90 0         0 $uri;
91             }
92              
93             sub _init_uri {
94 0     0   0 my $this = shift;
95 0         0 my $options;
96 0 0       0 if (@_ % 2) {
97 0         0 $options = shift;
98 0 0       0 if (@_ > 0) {
99 0         0 my $key;
100 0         0 while (@_) {
101 0         0 $key = shift;
102 0         0 $options->{$key} = shift;
103             }
104             }
105             } else {
106 0         0 $options = {@_};
107             }
108              
109 0 0       0 if (!defined $options->{_path}) {
110 0   0     0 $options->{_path} = $this->class_to_page(ref($this) || $this);
111             }
112 0   0     0 $options->{_params} ||= {};
113 0         0 $options;
114             }
115              
116             =item parse_uri
117              
118             # subclass
119             sub parse_uri {
120             my ($this, $data, %options) = @_;
121             $this->copy_hash_val($data->{params}, \%options, 'page');
122             if ($data->{path} eq "show_friend") {
123             # blah...
124             }
125             if ($data->{uri}->...) {
126             # maybe you won't use this
127             }
128             $this->SUPER::uri($data, %options);
129             }
130              
131             # call
132             my %options = $page->analyze_uri($uri);
133              
134             return page information of URI.
135              
136             =cut
137              
138             sub parse_uri {
139 0     0 1 0 my ($this, $data, %options) = @_;
140              
141 0         0 $this->copy_hash_val($data->{params}, \%options, 'id');
142 0 0       0 if ($data->{path} =~ /^(?:view|show)_(\w+)/) {
143 0         0 $options{type} = $1;
144             }
145 0         0 %options;
146             }
147              
148             =item parse
149              
150             # subclass
151             sub parse {
152             my ($this, %options) = @_;
153             # parse...
154             my $part = $this->parse_extract_parts(qw/.../);
155             return () unless defined $part;
156             # return
157             return ({ a => b, c => d }, { a => e, c => f }, ...);
158             }
159              
160             # call
161             my @datas = $page->parse;
162              
163             page parser. please return hashref array.
164              
165             =cut
166              
167 0     0 1 0 sub parse { shift->please_override_this }
168              
169             =item parse_banner
170              
171             my $data = $page->parse_banner;
172              
173             parse banner.
174             structure:
175              
176             link: link to ad page.
177             subject: subject of ad(banner's alt text).
178             image: image of banner
179             height: height of image
180             width: width of image
181              
182             =cut
183              
184             sub parse_banner {
185 0     0 1 0 my $this = shift;
186 0         0 my ($uri, $attrs) = $this->parse_extract_parts(
187             qr||i);
188 0 0 0     0 return () unless defined $uri and defined $attrs;
189 0         0 my %data;
190 0         0 $data{link} = $this->absolute_linked_uri($uri);
191              
192 0         0 my $temp = $this->generate_case_preserved_hash(
193             $this->html_attrs_to_hash($attrs));
194 0         0 $data{subject} = $temp->{alt};
195 0         0 $data{image} = $this->absolute_linked_uri($temp->{src});
196 0         0 $this->copy_hash_val($temp, \%data, qw(height width));
197 0         0 return \%data;
198             }
199              
200             =item parse_mainmenu
201              
202             my @data = $page->parse_mainmenu;
203              
204             parse mainmenu.
205             structure:
206              
207             link: link to page
208             subject: subject of page
209              
210             =cut
211              
212             sub parse_mainmenu {
213 0     0 1 0 my $this = shift;
214 0         0 my $part = $this->parse_extract_parts(
215             qr|(.+?)|is);
216 0 0       0 return () unless defined $part;
217 0         0 return map {
218 0         0 my(%data,$temp);
219 0         0 $temp = $this->generate_ignore_case_hash(
220             $this->html_attrs_to_hash($_));
221 0         0 $data{link} = $this->absolute_linked_uri($temp->{href});
222 0         0 $data{subject} = $this->rewrite($temp->{alt});
223 0         0 \%data;
224             } ($part =~ m||g);
225             }
226              
227             =item parse_tool_bar
228              
229             my @data = $page->parse_tool_bar;
230              
231             parse toolbar.
232             structure:
233              
234             link: link to page
235             subject: subject of page
236             image: image of toolbar.
237             height: height of image
238             width: width of image
239              
240             =cut
241              
242             sub parse_tool_bar {
243 0     0 1 0 my $this = shift;
244 0         0 my $attr_regex = $this->regex_parts->{html_attr};
245 0         0 my $attrval_regex = $this->regex_parts->{html_attrval};
246 0         0 my $maybe_attrs_regex = $this->regex_parts->{html_maybe_attrs};
247 0         0 my $part = $this->parse_extract_parts(
248             qr|(?>.*?
249             (?>.*?)
250             (.+)
251             (?>.*?)
252             (?>.*?
)|isx); 253 0 0       0 return () unless defined $part; 254 0         0 return map { 255 0         0 m||i; 256 0         0 my $temp = $this->generate_ignore_case_hash( 257             $this->html_attrs_to_hash($2)); 258 0         0 my %data = ( 259             link => $this->html_anchor_to_uri($1), 260             image => $this->absolute_linked_uri($temp->{src}), 261             subject => $this->rewrite($temp->{alt}), 262             ); 263 0         0 $this->copy_hash_val($temp, \%data, qw(height width)); 264 0         0 \%data; 265             } ($part =~ m|(?>(.+))|ig); 266             } 267               268             =item get 269               270             $page->get([opt => val], ...); 271               272             handy method. call ->set_content and ->parse. 273               274             =cut 275               276             __PACKAGE__->mk_get_method(''); 277               278             =item set_content 279               280             $page->set_content($uri); 281               282             or 283               284             $page->set_content(%options); 285               286             set content to specified by uri or options. 287               288             =cut 289               290             sub set_content { 291 0     0 1 0 my $this = shift; 292 0         0 my $uri; 293               294 0 0       0 if (scalar(@_) % 2) { 295             # odd 296 0         0 $uri = shift; 297             } else { 298 0   0     0 $uri = $this->uri(@_) || return undef; 299             } 300 0         0 $this->session->set_content($uri); 301             } 302               303             =back 304               305             =head1 UTILITY METHODS 306               307             methods to useful for subclass implementations 308               309             =over 4 310               311             =cut 312               313             =item parse_extract_parts 314               315             # array 316             my @parts = $this->parse_extract_parts(qr|....|); 317             return () unless @parts; 318             # more parse with @parts 319               320             or 321               322             # scalar 323             my $part = $this->parse_extract_parts(qr|....|); 324             return () unless defined $part; 325             # more parse with $part 326               327             extract part(s) from current content. 328               329             =cut 330               331             sub parse_extract_parts { 332 0     0 1 0 my ($this, $regex) = @_; 333               334 0         0 my $content = $this->content; 335 0 0       0 return () unless defined $content; 336 0         0 my @array; 337 0 0       0 return () unless @array = $content =~ m/$regex/; 338 0 0       0 if (wantarray) { 339 0         0 return @array; 340             } else { 341 0         0 return shift(@array); 342             } 343             } 344               345             =item html_attr_to_uri 346               347             $page->html_attr_to_uri('src', 'src="foobar" ...'); 348               349             parse html attrs string(C<< ->html_attrs_to_hash >>) and 350             extract attr(C<< ->generate_ignore_case_hash()->{$attrname} >>) and 351             resolve to absolute URI(C<< ->absolute_linked_uri >>). 352               353             =cut 354               355             sub html_attr_to_uri { 356 0     0 1 0 my ($this, $attrname, $attrvals) = @_; 357               358 0         0 $this->absolute_linked_uri( 359             $this->generate_ignore_case_hash( 360             $this->html_attrs_to_hash( 361             $attrvals))->{$attrname}); 362             } 363               364             =item html_anchor_to_uri 365               366             $page->html_anchor_to_uri("href='...'..."); 367               368             handy method. call ->html_attr_to_uri with 'href'. 369               370             =cut 371               372 0     0 1 0 sub html_anchor_to_uri { shift->html_attr_to_uri('href', @_) } 373               374             =item cache 375               376             my $cache = $pkg->cache; 377             if (defined $cache->{foo}) { 378             return $cache->{foo}; 379             } 380             ... 381               382             get modules's cache storage. 383               384             =cut 385               386             sub cache { 387 0     0 1 0 my $this = shift; 388 0         0 $this->session->cache(ref($this)); 389             } 390               391             =item mk_cached_parser 392               393             # from subclass 394             __PACKAGE__->mk_cached_parser(qw(foo bar)); 395             sub _parse_foo { 396             # ... 397             return $foo; # please return scalar value. 398             } 399               400             generate cached parser (proxy) method. 401             use _parse_(name) to real parser method. 402               403             =cut 404               405             sub mk_cached_parser { 406 2     2 1 7 my $this = shift; 407 2   33     17 my $pkg = ref($this) || $this; 408 2         6 foreach (@_) { 409 6 0   0 1 458 eval <<"END";   0 0   0 1 0     0 0   0 1 0     0         0     0         0     0         0     0         0     0         0     0         0     0         0   410             package $pkg; 411             sub parse_$_ \{ 412             my \$this = shift; 413             return \$this->cache->{$_} if defined \$this->cache->{$_}; 414             return \$this->cache->{$_} = \$this->_parse_$_; 415             \} 416             END 417             } 418             } 419               420             =item mk_get_method 421               422             # from subclass 423             __PACKAGE__->mk_get_method(qw(foo bar)); 424               425             generate get handy method. 426               427             =cut 428               429             sub mk_get_method { 430 4     4 1 10 my $this = shift; 431 4   33     40 my $pkg = ref($this) || $this; 432 4         11 foreach (@_) { 433 6         9 my $method = ''; 434 6 100       29 $method .= "_$_" if length; 435 6 0   0 1 641 eval <<"END";   0 0   0 1       0 0   0 1       0 0             0 0             0 0             0               0               0               0               0               0               0               0               0               0               0               0               0               0               0               0               0               0               0             436             package $pkg; 437             sub get$method \{ 438             my \$this = shift; 439             my \%options; 440               441             if (scalar(\@_) % 2) { 442             # odd 443             \$this->set_content(shift); 444             \%options = \@_; 445             } else { 446             \%options = \@_; 447             \$this->set_content(\%options) || return undef; 448             } 449             \$this->parse$method(\%options); 450             \} 451             END 452             } 453             } 454               455               456               457             =back 458               459             =head1 ACCESSOR 460               461             =over 4 462               463             =item session 464               465             parent L object. 466               467             =cut 468               469             __PACKAGE__->mk_ro_accessors(qw(session)); 470               471             =back 472               473             =head1 PROXY METHODS 474               475             =over 4 476               477             =item relative_uri 478               479             =item absolute_uri 480               481             =item absolute_linked_uri 482               483             =item refresh_content 484               485             =item post 486               487             =item response 488               489             =item content 490               491             =item page 492               493             =item class_to_page 494               495             =item page_to_class 496               497             =item analyze_uri 498               499             =item convert_from_http_content 500               501             =item convert_to_http_content 502               503             =item convert_login_time 504               505             =item convert_time 506               507             see L. 508               509             =cut 510               511             foreach (qw(relative_uri absolute_uri absolute_linked_uri refresh_content post 512             response content page class_to_page page_to_class analyze_uri 513             convert_from_http_content convert_to_http_content 514             convert_login_time convert_time 515             )) { 516 0     0 1   eval "sub $_ \{ shift->session->$_(\@_) }";   0     0 1       0     0 1       0     0 1       0     0 1       0     0 1       0     0 1       0     0 1       0     0 1       0     0 1       0     0 1       0     0 1       0     0 1       0     0 1       0     0 1     517             } 518               519             1; 520               521             __END__