File Coverage

blib/lib/Lim/RPC.pm
Criterion Covered Total %
statement 41 59 69.4
branch 14 40 35.0
condition 7 27 25.9
subroutine 7 7 100.0
pod 2 2 100.0
total 71 135 52.5


line stmt bran cond sub pod time code
1             package Lim::RPC;
2              
3 7     7   43 use common::sense;
  7         17  
  7         59  
4 7     7   345 use Carp;
  7         14  
  7         534  
5              
6 7     7   43 use Scalar::Util qw(blessed);
  7         57  
  7         489  
7              
8 7     7   40 use Lim ();
  7         13  
  7         157  
9 7     7   2763 use Lim::Error ();
  7         17  
  7         8438  
10              
11             =encoding utf8
12              
13             =head1 NAME
14              
15             Lim::RPC - Utilities for Lim's RPC
16              
17             =head1 VERSION
18              
19             See L for version.
20              
21             =cut
22              
23             our $VERSION = $Lim::VERSION;
24              
25             =head1 SYNOPSIS
26              
27             =over 4
28              
29             use Lim::RPC;
30              
31             =back
32              
33             =head1 NOTE
34              
35             These functions are mainly used internaly, you should not have any reason to
36             call them.
37              
38             =head1 METHODS
39              
40             =over 4
41              
42             =item Lim::RPC::V($q, $def)
43              
44             V is for Verify, it will verify the content of the hash ref C<$q> against the
45             RPC definition in C<$def>. On an error it will L.
46              
47             =cut
48              
49             sub V {
50 2     2 1 99 my ($q, $def) = @_;
51            
52 2 50 33     42 if (defined $q and defined $def) {
53 2 50 33     36 unless (ref($q) eq 'HASH' and ref($def) eq 'HASH') {
54 0         0 confess __PACKAGE__, ': Can not verify data, invalid parameters given';
55             }
56            
57 2         7 my @v = ([$q, $def]);
58 2         11 while (defined (my $v = shift(@v))) {
59 2         5 ($q, $def) = @$v;
60 2         4 my $a;
61              
62 2 50       15 if (ref($q) eq 'ARRAY') {
63 0         0 $a = $q;
64             }
65             else {
66 2         10 $a = [$q];
67             }
68            
69 2         4 foreach $q (@{$a}) {
  2         13  
70 2 50       11 unless (ref($q) eq 'HASH') {
71 0         0 confess __PACKAGE__, ': Can not verify data, invalid data given';
72             }
73            
74             # check required
75 2         8 foreach my $k (keys %$def) {
76 2 50 33     43 if (blessed($def->{$k}) and $def->{$k}->required and !exists $q->{$k}) {
      33        
77 0         0 confess __PACKAGE__, ': required data missing, does not match definition';
78             }
79             }
80            
81             # check data
82 2         10 foreach my $k (keys %$q) {
83 2 50       8 unless (exists $def->{$k}) {
84 0         0 confess __PACKAGE__, ': invalid data, no definition exists';
85             }
86            
87 2 50 33     29 if (blessed($def->{$k}) and !$def->{$k}->comform($q->{$k})) {
88 0         0 confess __PACKAGE__, ': invalid data, validation failed';
89             }
90            
91 2 50 33     31 if (ref($q->{$k}) eq 'HASH' or ref($q->{$k}) eq 'ARRAY') {
92 0 0 0     0 if (ref($def->{$k}) eq 'HASH') {
    0          
93 0         0 push(@v, [$q->{$k}, $def->{$k}]);
94             }
95             elsif (blessed $def->{$k} and $def->{$k}->isa('Lim::RPC::Value::Collection')) {
96 0 0       0 unless ($def->{$k}->swallow) {
97 0         0 push(@v, [$q->{$k}, $def->{$k}->children]);
98             }
99             }
100             else {
101 0         0 confess __PACKAGE__, ': invalid definition, can not validate data';
102             }
103             }
104             }
105             }
106             }
107             }
108 2         7 return;
109             }
110              
111             =item Lim::RPC::R($cb, $data)
112              
113             R is for Result, called when a RPC call finish and convert the given C<$data> to
114             the corresponding protocol.
115              
116             =cut
117              
118             sub R {
119 2     2 1 12 my ($cb, $data) = @_;
120            
121 2 50       16 unless (blessed($cb)) {
122 0         0 confess __PACKAGE__, ': cb not blessed';
123             }
124            
125 2 50       18 if (blessed($data)) {
    50          
126 0 0       0 if ($data->isa('Lim::Error')) {
127 0         0 return $cb->cb->($data);
128             }
129             }
130             elsif (defined $data) {
131 2 50       19 unless (ref($data) eq 'HASH') {
132 0         0 confess __PACKAGE__, ': data not a hash';
133             }
134            
135 2 50 33     13 if ($cb->call_def and exists $cb->call_def->{out}) {
    0          
136 2         8 Lim::RPC::V($data, $cb->call_def->{out});
137             }
138             elsif (%$data) {
139 0         0 confess __PACKAGE__, ': data given without definition';
140             }
141             }
142             else {
143 0 0 0     0 if ($cb->call_def and exists $cb->call_def->{out}) {
144 0         0 Lim::RPC::V({}, $cb->call_def->{out});
145             }
146             }
147            
148 2 50       19 return $cb->cb->(defined $data ? $data : {});
149             }
150              
151             =back
152              
153             =head1 AUTHOR
154              
155             Jerry Lundström, C<< >>
156              
157             =head1 BUGS
158              
159             Please report any bugs or feature requests to L.
160              
161             =head1 SUPPORT
162              
163             You can find documentation for this module with the perldoc command.
164              
165             perldoc Lim::RPC
166              
167             You can also look for information at:
168              
169             =over 4
170              
171             =item * Lim issue tracker (report bugs here)
172              
173             L
174              
175             =back
176              
177             =head1 ACKNOWLEDGEMENTS
178              
179             =head1 LICENSE AND COPYRIGHT
180              
181             Copyright 2012-2013 Jerry Lundström.
182              
183             This program is free software; you can redistribute it and/or modify it
184             under the terms of either: the GNU General Public License as published
185             by the Free Software Foundation; or the Artistic License.
186              
187             See http://dev.perl.org/licenses/ for more information.
188              
189              
190             =cut
191              
192             1; # End of Lim::RPC