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