1 |
package Frey::Pipe; |
2 |
use Moose; |
3 |
extends 'Frey::ClassLoader'; |
4 |
with 'Frey::Web'; |
5 |
|
6 |
use Frey::Action; |
7 |
|
8 |
=head1 DESCRIPTION |
9 |
|
10 |
Shell pipes for structured data |
11 |
|
12 |
=head1 EXAMPLE |
13 |
|
14 |
Frey::Feed/as_data + uri=http://blog.rot13.org/index.xml | Frey::View::Dumper/as_markup |
15 |
|
16 |
this will produce following code: |
17 |
|
18 |
Frey::View::Dumper->new( data => |
19 |
Frey::Feed->new( uri => 'http://blog.rot13.org/index.xml' )->as_data |
20 |
)->as_markup |
21 |
|
22 |
=cut |
23 |
|
24 |
has pipe => ( |
25 |
is => 'rw', |
26 |
required => 1, |
27 |
default => 'Frey::Feed/as_data+uri=http://blog.rot13.org/index.xml|Frey::View::Dumper/as_markup', |
28 |
); |
29 |
|
30 |
use Data::Dump qw/dump/; |
31 |
|
32 |
sub as_markup { |
33 |
my ($self) = @_; |
34 |
|
35 |
my $out; |
36 |
my $pipe; |
37 |
|
38 |
$self->title( ref($self) . ' | ' . $self->pipe ); |
39 |
|
40 |
foreach my $part ( split(/\|/, $self->pipe ) ) { |
41 |
warn "# part: '$part'"; |
42 |
if ( $part =~ m{^([^/]+)/([^\+\?]+)(.*)?$} ) { |
43 |
my ( $class, $method, $args ) = ( $1, $2, $3 ); |
44 |
push @{ $self->status }, { $class => { method => $method, args => $args } }; |
45 |
my $params = $pipe; |
46 |
$params = {} unless defined $params; |
47 |
if ( defined $args ) { |
48 |
$args =~ s{^[\?\+\s]}{}; |
49 |
warn "# class $class method $method args '$args'", $pipe ? " pipe args " . join(',',keys %$pipe) : '' if $self->debug; |
50 |
push @{ $self->status }, { $class =>$args }; |
51 |
map { |
52 |
my ( $name, $value ) = ( $1, $2 ) if m{^([^=]+)=(.+)$} || confess "can't parse '$_'"; |
53 |
$params->{$name} = $value; |
54 |
} split(/\s*\+\s/, $args) |
55 |
} |
56 |
|
57 |
my ( $html, $default ) = Frey::Action->new( class => $class, params => $params )->params_form; |
58 |
warn "$class need more params than ",dump( $default ) if $html && $self->debug; # FIXME replace with query |
59 |
|
60 |
warn "# pipe $part" if $self->debug; |
61 |
my $o = $self->new_frey_class( $class, $default ); |
62 |
my $result = $o->$method; |
63 |
warn "# result ",length( $result ), " bytes ", ref($result); |
64 |
|
65 |
$self->content_type( $o->content_type ) if $o->can('content_type'); |
66 |
|
67 |
$out = $result; |
68 |
$method =~ s{^as_}{}; |
69 |
$pipe = { $method => $result }; |
70 |
} else { |
71 |
die "don't know what to do with '$part' from ",$self->pipe; |
72 |
} |
73 |
} |
74 |
|
75 |
warn "# pipe ", $self->title; |
76 |
|
77 |
return $out; |
78 |
} |
79 |
|
80 |
1; |