File Coverage

blib/lib/WebService/NFSN/Object.pm
Criterion Covered Total %
statement 45 72 62.5
branch 8 18 44.4
condition n/a
subroutine 10 19 52.6
pod 0 10 0.0
total 63 119 52.9


line stmt bran cond sub pod time code
1             #---------------------------------------------------------------------
2             package WebService::NFSN::Object;
3             #
4             # Copyright 2010 Christopher J. Madsen
5             #
6             # Author: Christopher J. Madsen
7             # Created: 3 Apr 2007
8             #
9             # This program is free software; you can redistribute it and/or modify
10             # it under the same terms as Perl itself.
11             #
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15             # GNU General Public License or the Artistic License for more details.
16             #
17             # ABSTRACT: Base class for NFSN API objects
18             #---------------------------------------------------------------------
19              
20 1     1   718 use 5.006;
  1         5  
21 1     1   6 use Carp;
  1         3  
  1         97  
22 1     1   8 use strict;
  1         3  
  1         25  
23 1     1   6 use warnings;
  1         2  
  1         74  
24 1     1   646 use HTTP::Request::Common qw(GET POST PUT);
  1         3386  
  1         103  
25 1     1   7 use URI 1.00 ();
  1         20  
  1         37  
26 1     1   6 use WebService::NFSN 0.10 qw(_eval_or_die);
  1         19  
  1         471  
27              
28             #=====================================================================
29             # Package Global Variables:
30              
31             our $VERSION = '1.04'; # VERSION
32              
33             #=====================================================================
34             sub get_converter # ($function)
35             {
36 27 100   27 0 86 my $convert = ($_[0] =~ s/:JSON$//
37             ? 'WebService::NFSN::decode_json'
38             : '');
39              
40 27         59 return $convert;
41             } # end get_converter
42              
43             #---------------------------------------------------------------------
44             # Generate the code for an API module:
45              
46             sub _define
47             {
48 5     5   27 my ($class, %p) = @_;
49              
50             #...................................................................
51             # Create the object_type method for classifying objects:
52              
53 5         16 my $code = "package $class;\nsub object_type { '$p{type}' }\n";
54              
55             #...................................................................
56             # Create an accessor method for each property:
57              
58 5         15 foreach my $propType (qw(rw ro wo)) {
59 15         30 my $properties = $p{$propType};
60              
61 15 100       44 next unless $properties;
62              
63 5         8 foreach my $property (@$properties) {
64 15         30 my $convert = get_converter($property);
65              
66 15         39 $code .= <<"END PROPERTY";
67             sub $property
68             {
69             $convert shift->${propType}_property('$property' => \@_);
70             }
71             END PROPERTY
72             } # end foreach $property
73             } # end foreach $propType
74              
75             #...................................................................
76             # Create an object method for each API method:
77              
78 5 100       16 if (my $methods = $p{methods}) {
79 4         19 while (my ($method, $params) = each %$methods) {
80 12         24 my $convert = get_converter($method);
81              
82             # Process method prototype:
83 12         20 my (%accepted, @required);
84 12         25 foreach (@$params) {
85 18 100       59 push @required, $_ unless s/\?$//;
86 18         48 $accepted{$_} = 1;
87             } # end foreach parameter declaration
88              
89             # Store method prototype into package variable:
90 1     1   8 { no strict 'refs'; ## no critic ProhibitNoStrict
  1         2  
  1         923  
  12         19  
91 12         20 @{ sprintf '%s::_%s_prototype', $class, $method }
  12         115  
92             = ($method, \%accepted, \@required) }
93              
94             # Define the method:
95 12         69 $code .= <<"END METHOD";
96             our \@_${method}_prototype;
97             sub $method
98             {
99             $convert shift->POST_request(\@_${method}_prototype, \@_);
100             }
101             END METHOD
102             } # end while each method
103             } # end if methods
104              
105 5         24 _eval_or_die $code;
106             } # end _define
107              
108             #=====================================================================
109             sub new
110             {
111 0     0 0   my ($class, $manager, $id) = @_;
112              
113 0           return bless { manager => $manager,
114             id => $id,
115             }, $class;
116             } # end new
117              
118             #---------------------------------------------------------------------
119             sub GET_request
120             {
121 0     0 0   my ($self, $property) = @_;
122              
123 0           return $self->make_request(GET $self->make_uri($property));
124             } # end GET_request
125              
126             #---------------------------------------------------------------------
127             sub PUT_request
128             {
129 0     0 0   my ($self, $property, $value) = @_;
130              
131 0           return $self->make_request(PUT $self->make_uri($property),
132             Content => $value);
133             } # end PUT_request
134              
135             #---------------------------------------------------------------------
136             sub POST_request
137             {
138 0     0 0   my ($self, $method, $accepted, $required, %param) = @_;
139              
140 0           foreach my $key (@$required) {
141             croak(qq'Missing required "$key" parameter for $method')
142 0 0         unless defined $param{$key};
143             }
144              
145 0           foreach my $key (keys %param) {
146 0 0         carp(qq'"$key" is not a parameter of $method') unless $accepted->{$key};
147             }
148              
149 0           return $self->make_request(POST $self->make_uri($method), \%param);
150             } # end POST_request
151              
152             #---------------------------------------------------------------------
153             sub make_request
154             {
155 0     0 0   my $self = shift @_;
156              
157 0           my $res = $self->{manager}->make_request(@_);
158              
159 0           return $res->content;
160             } # end make_request
161              
162             #---------------------------------------------------------------------
163             sub make_uri
164             {
165 0     0 0   my ($self, $name) = @_;
166              
167             URI->new(join('/', $self->{manager}->root_url, $self->object_type,
168 0           $self->{id}, $name));
169             } # end make_url
170              
171             #---------------------------------------------------------------------
172             sub ro_property
173             {
174 0     0 0   my ($self, $property) = @_;
175              
176 0 0         croak "$property is read-only" if @_ > 2;
177              
178 0           return $self->GET_request($property);
179             } # end ro_property
180              
181             #---------------------------------------------------------------------
182             sub rw_property
183             {
184 0     0 0   my ($self, $property, $value) = @_;
185              
186 0 0         if (@_ > 2) {
187 0           return $self->PUT_request($property, $value);
188             } else {
189 0           return $self->GET_request($property);
190             }
191             } # end rw_property
192              
193             #---------------------------------------------------------------------
194             sub wo_property
195             {
196 0     0 0   my ($self, $property, $value) = @_;
197              
198 0 0         croak "$property is write-only" if @_ < 3;
199              
200 0           return $self->PUT_request($property, $value);
201             } # end wo_property
202              
203             #=====================================================================
204             # Package Return Value:
205              
206             1;
207              
208             __END__