annotate lib/IMPL/Web/Handler/JSONView.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56:53 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Web::Handler::JSONView;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use JSON;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::lang qw(is);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 HttpResponse => 'IMPL::Web::HttpResponse',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 ViewResult => '-IMPL::Web::ViewResult',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 Loader => 'IMPL::Code::Loader'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 'IMPL::Object::Serializable' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 'IMPL::Object::Autofill' => '@_'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 transform => PROP_RW
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 sub contentType {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 'application/json'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 sub Invoke {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 my ($this,$action,$next) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 my $result = $next ? $next->($action) : undef;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 my $model = ( ref $result and is($result,ViewResult) )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 ? $result->model
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 : $result;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 $model = [$model] unless ref $model;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 if (my $factory = $this->transform) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 Loader->safe->Require($factory) unless ref $factory;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 my $t = $this->transform->new();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 $model = $t->Transform($model);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 my %params = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 type => $this->contentType,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 charset => 'utf-8',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 body => JSON->new->utf8->pretty->encode($model)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 if(is($result,ViewResult)) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 $params{status} = $result->status if $result->status;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $params{headers} = $result->headers if $result->headers;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 $params{cookies} = $result->cookies if $result->cookies;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 return HttpResponse->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 %params
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 =head1
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 =cut