File Coverage

blib/lib/WebService/TWFY/Request.pm
Criterion Covered Total %
statement 44 54 81.4
branch 8 16 50.0
condition 0 3 0.0
subroutine 9 10 90.0
pod 1 4 25.0
total 62 87 71.2


line stmt bran cond sub pod time code
1             package WebService::TWFY::Request ;
2              
3 2     2   14 use strict ;
  2         4  
  2         59  
4 2     2   10 use warnings ;
  2         4  
  2         42  
5              
6 2     2   10 use Carp ;
  2         3  
  2         175  
7              
8 2     2   1672 use HTTP::Request ;
  2         59560  
  2         79  
9 2     2   18 use URI ;
  2         3  
  2         1258  
10              
11             our $VERSION = 0.07 ;
12             our @ISA = qw( HTTP::Request ) ;
13              
14             =head1 NAME
15              
16             WebService::TWFY::Request - API interface for TheyWorkForYou.com
17              
18             =head1 VERSION
19              
20             Version 0.07
21              
22             =cut
23              
24             =head1 SYNOPSIS
25              
26             use WebService::TWFY::API ;
27            
28             my $rh = { key => 'ABC123' };
29             my $api = WebService::TWFY::API->new( $rh ) ;
30              
31             my $rv = $api->query ( 'getConstituency', { 'postcode' => 'W128JL'
32             'output' => 'xml',
33             } ) ;
34              
35             if ($rv->{is_success}) {
36            
37             my $results = $rv->{results} ;
38             ### do whatever with results
39            
40             }
41              
42             =head1 DESCRIPTION
43              
44             This module encapsulates a single request and its arguments.
45             C is essentially a subscall of C.
46              
47             =head1 SUPPORT
48              
49             Please feel free to send any bug reports and suggestions to my email listed below.
50              
51             For more information and useless facts on my life, you can also check my blog:
52              
53             http://ffffruit.com/
54              
55             =head1 AUTHOR
56              
57             Spiros Denaxas
58             CPAN ID: SDEN
59             s [dot] denaxas [@] gmail [dot]com
60             http://ffffruit.com
61              
62             =head1 SOURCE CODE
63              
64             The source code for his module is now on github L
65              
66             =head1 COPYRIGHT
67              
68             This program is free software; you can redistribute
69             it and/or modify it under the same terms as Perl itself.
70              
71             The full text of the license can be found in the
72             LICENSE file included with this module.
73              
74              
75             =head1 SEE ALSO
76              
77             C, C, L, L
78              
79             =cut
80              
81              
82             sub new {
83 2     2 1 4 my $class = shift ;
84 2         11 my $self = new HTTP::Request ;
85 2         101 my $function = shift ;
86 2         3 my $rh_args = shift ;
87            
88 2         5 $self->{function} = $function ;
89 2         4 $self->{args} = $rh_args ;
90            
91 2         7 $self->method('POST') ;
92 2         18 my $uri = &_get_uri_for_function($function) ;
93            
94 2 100       6 if (not defined $uri) {
95 1         29 croak "Invalid function: $function\nPlease look at the documentation for supported functions." ;
96             }
97            
98 1         2 $self->{uri} = $uri ;
99            
100 1         3 bless $self, $class ;
101 1         3 return $self ;
102             }
103            
104             sub _get_uri_for_function {
105 2     2   3 my $function = shift ;
106 2         3 my $URL = 'http://www.theyworkforyou.com/api/' ;
107            
108 2 50       7 return unless defined $function ;
109            
110 2         27 my $rh_valid_functions = {
111             'convertURL' => 'Convert a parliament.uk URL into a TheyWorkForYou one, if possible',
112             'getConstituency' => 'Searches for a constituency',
113             'getConstituencies' => 'Returns a list of constituencies',
114             'getMP' => 'Returns the main details for an MP',
115             'getMPInfo' => 'Returns extra information for an MP',
116             'getMPs' => 'Returns list of MPs',
117             'getLord' => 'Returns details for a Lord',
118             'getLords' => 'Returns list of Lords',
119             'getGeometry' => 'Returns centre, bounding box of constituency',
120             'getCommittee' => 'Returns members of Select Committee',
121             'getDebates' => 'Retuns Debates',
122             'getWrans' => 'Returns Written Answers',
123             'getWMS' => 'Returns Written Ministerial Statements',
124             'getComments' => 'Returns comments',
125             } ;
126              
127 2 100       10 return unless exists $rh_valid_functions->{$function} ;
128 1         2 my $uri = $URL . $function ;
129            
130 1         5 return $uri ;
131              
132             }
133            
134            
135             sub encode_arguments {
136 1     1 0 2 my $self = shift ;
137            
138 1         8 my $rh_args = $self->{args} ;
139 1         7 my $url = URI->new( $self->{uri}, 'http' ) ;
140            
141 1 50       9882 if (exists $rh_args->{output}) {
142 1         5 &validate_output_argument($rh_args->{output}) ;
143             }
144              
145 0         0 &validate_arguments( $self->{function}, $rh_args ) ;
146            
147            
148 0         0 $url->query_form( %$rh_args ) ;
149 0         0 return $url ;
150            
151             }
152              
153             sub validate_output_argument {
154 1     1 0 3 my $output = shift ;
155            
156 1 50       8 croak "Missing value for output parameter.\n" unless
157             defined $output ;
158            
159 1         8 my $rh_valid_params = {
160             'xml' => 'XML output',
161             'php' => 'Serialized PHP',
162             'js' => 'a JavaScript object',
163             'rabx' => 'RPC over Anything But XML',
164             } ;
165            
166 1 50       24 croak "Invalid output selected: $output.\nPlease consult the documentation for valid output modes\n"
167             unless exists $rh_valid_params->{$output} ;
168            
169             }
170              
171             sub validate_arguments {
172 0     0 0   my ($function, $rh_args) = (@_) ;
173            
174             # make an initial check for missing values
175            
176 0           foreach (keys %$rh_args) {
177 0 0         croak "Missing value for : $_ " unless
178             (defined $rh_args->{$_}) ;
179             }
180            
181             # a list of functions and what parameters
182             # are mandatory for them.
183            
184 0           my $rha_functions_params = {
185             'convertURL' => [ 'url' ],
186             'getConstituency' => [ 'postcode' ],
187             'getConstituencies' => [ ],
188             'getMP' => [ ],
189             'getMPInfo' => [ 'id' ],
190             'getMPs' => [ ],
191             'getLord' => [ 'id' ],
192             'getLords' => [ ],
193             'getGeometry' => [ ],
194             'getCommittee' => [ 'name' ],
195             'getDebates' => [ 'type' ],
196             'getWrans' => [ ],
197             'getWMS' => [ ],
198             'getComments' => [ ],
199             } ;
200            
201 0           my $ra_req_params = $rha_functions_params->{$function} ;
202            
203 0           foreach (@$ra_req_params) {
204 0 0 0       croak "$function requires the '$_' parameter \n" unless exists $rh_args->{$_} and
205             defined $rh_args->{$_} ;
206             } ;
207              
208             }
209            
210            
211            
212            
213            
214            
215              
216              
217             1 ;