| 1 | package Froody::Reflection; | ||||||||||||||||||||||||||||
| 2 | 20 | ▶ | use base qw(Froody::Implementation); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 20 | ▶ | use Froody::Response::XML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 20 | ▶ | use Froody::Response::Terse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 20 | ▶ | use Params::Validate qw( :all ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 20 | our $IMPLEMENTATION_SUPERCLASS = 1; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 12 | ▶ | sub implements { "Froody::API::Reflection" => 'froody.reflection.*' } | ||||||||||||||||||||||||||
|
Subroutine
implements: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 20 | ▶ | use Froody::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 20 | our $logger = get_logger("froody.reflection"); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Froody::Reflection | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head2 Functions | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =over | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =item getMethodInfo | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Returns information for a given froody API method. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub getMethodInfo | ||||||||||||||||||||||||||||
| 38 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getMethodInfo: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 2 | my ($self, $args, $metadata) = @_; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | my $method_name = $args->{method_name}; | |||||||||||||||||||||||||||
| 42 | 2 | my $method = $metadata->{dispatcher}->get_method($method_name); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | return $self->_methodInfo($metadata, $method); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | sub _methodInfo { | ||||||||||||||||||||||||||
|
Subroutine
_methodInfo: called CC=9 SCAR=23.1
| |||||||||||||||||||||||||||||
| 48 | 2 | my ($self, $metadata, $method) = @_; | |||||||||||||||||||||||||||
| 49 | 2 | my $calling_method = $metadata->{dispatcher}->get_method('froody.reflection.getMethodInfo'); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | my $response = { | |||||||||||||||||||||||||||
| 52 | name => $method->full_name, | ||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | $response->{description} = $method->description | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | if $method->description; | ||||||||||||||||||||||||||||
| 57 | 2 | ▶ | $response->{needslogin} = $method->needslogin || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$method->needslogin || 0
MC/DC0%0/1
$method->needslogin || 0
$method->needslogin
Truth table50%1/2
A
A= $method->needslogin
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | my $arg_info; | |||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 2 | my $arguments = $method->arguments; | |||||||||||||||||||||||||||
| 62 | 2 | for my $k (keys %$arguments) { | |||||||||||||||||||||||||||
| 63 | 0 | my $v = $arguments->{$k}; | |||||||||||||||||||||||||||
| 64 | my $argdata = { | ||||||||||||||||||||||||||||
| 65 | name => $k, | ||||||||||||||||||||||||||||
| 66 | -text => $v->{doc}, | ||||||||||||||||||||||||||||
| 67 | optional => $v->{optional}, | ||||||||||||||||||||||||||||
| 68 | 0 | type => join(',',@{$v->{type}}), | |||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||
| 70 | 0 | push @$arg_info, $argdata; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | $response->{arguments} = { argument => $arg_info } if $arg_info && @$arg_info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$arg_info and @$arg_info
MC/DC0%0/2
$arg_info and @$arg_info
$arg_info
@$arg_info
Truth table33%1/3
A and B
A= $arg_infoB= @$arg_info
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | my $method_errors = $method->errors; | |||||||||||||||||||||||||||
| 76 | my $errors = [ map { | ||||||||||||||||||||||||||||
| 77 | 2 | +{ | |||||||||||||||||||||||||||
| 78 | code => $_, | ||||||||||||||||||||||||||||
| 79 | message => $method_errors->{$_}{message}, | ||||||||||||||||||||||||||||
| 80 | -text => $method_errors->{$_}{description} | ||||||||||||||||||||||||||||
| 81 | 0 | } } keys %$method_errors ]; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | $response->{errors} = { error => $errors } if @$errors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | $response->{response} = {} if $method->example_response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | my $rsp = Froody::Response::Terse->new(); | |||||||||||||||||||||||||||
| 87 | 2 | $rsp->content($response); | |||||||||||||||||||||||||||
| 88 | 2 | $rsp->structure( $calling_method ); | |||||||||||||||||||||||||||
| 89 | 2 | $rsp = $rsp->as_xml; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # find the empty <response>...</response> and shove in our | ||||||||||||||||||||||||||||
| 92 | # child nodes. This *must* be encoded in what we are (which is utf-8) | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | if ($method->example_response) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1 | my ($example_element) = $rsp->xml->findnodes("//response"); | |||||||||||||||||||||||||||
| 95 | 1 | $example_element->appendText( _response_to_xml($method)->toString ); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 2 | return $rsp->as_terse; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | ▶ | sub _response_to_xml { | ||||||||||||||||||||||||||
|
Subroutine
_response_to_xml: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | 1 | my $structure = shift; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # convert whatever we have to XML -- example | ||||||||||||||||||||||||||||
| 104 | # responses are always stored in terse form (for now) | ||||||||||||||||||||||||||||
| 105 | 1 | my $example = $structure->example_response->as_xml; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # grab the thingy inside the rsp and return it | ||||||||||||||||||||||||||||
| 108 | 1 | my ($response) = $example->xml->findnodes("/rsp/*"); | |||||||||||||||||||||||||||
| 109 | 1 | return $response->cloneNode(1); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =item getMethods | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Returns a list of methods. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub getMethods | ||||||||||||||||||||||||||||
| 120 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getMethods: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 1 | my ($self, $args, $metadata) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | my $client = $metadata->{dispatcher}; | |||||||||||||||||||||||||||
| 124 | 1 | my $repo = $metadata->{dispatcher}->repository; | |||||||||||||||||||||||||||
| 125 | 6 | my %methods = map { $_ => 1 } | |||||||||||||||||||||||||||
| 126 | 1 | map { $_->full_name } | |||||||||||||||||||||||||||
| 127 | $repo->get_methods; | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | return { | ||||||||||||||||||||||||||||
| 130 | 1 | method => [ sort map { $_ } keys %methods], | |||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =item getErrorTypes | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Returns a list of error types. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub getErrorTypes | ||||||||||||||||||||||||||||
| 141 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getErrorTypes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 1 | my ($self, $args, $metadata) = @_; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | return { | ||||||||||||||||||||||||||||
| 145 | errortype => [ sort | ||||||||||||||||||||||||||||
| 146 | 3 | grep { $_ } | |||||||||||||||||||||||||||
| 147 | 3 | map { $_->full_name } | |||||||||||||||||||||||||||
| 148 | 1 | $metadata->{dispatcher}->repository->get_errortypes ], | |||||||||||||||||||||||||||
| 149 | }; | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item getErrorTypeInfo | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Returns the error type information | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | sub getErrorTypeInfo | ||||||||||||||||||||||||||||
| 159 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getErrorTypeInfo: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 1 | my ($self, $args, $metadata) = @_; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | my $repo = $metadata->{dispatcher}->repository; | |||||||||||||||||||||||||||
| 163 | 1 | my $et = $repo->get_errortype($args->{code}); | |||||||||||||||||||||||||||
| 164 | 1 | return _errortype_hash($et->example_response); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =item getSpecification($args, $metadata) | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Returns a terse representation of all public registered methods and error types | ||||||||||||||||||||||||||||
| 170 | within the repository indicated in $metadata | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub getSpecification { | ||||||||||||||||||||||||||
|
Subroutine
getSpecification: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 0 | my ($self, $args, $metadata) = @_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | my $repo = $metadata->{dispatcher}->repository; | |||||||||||||||||||||||||||
| 178 | 0 | my $methods = [ map { $self->_methodInfo($metadata, $_->[0])->as_terse->content } | |||||||||||||||||||||||||||
| 179 | 0 | sort { $a->[1] cmp $b->[1] } | |||||||||||||||||||||||||||
| 180 | 0 | map { [$_, $_->full_name ] } | |||||||||||||||||||||||||||
| 181 | $repo->get_methods ]; | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | my $errortypes = [ | ||||||||||||||||||||||||||||
| 184 | 0 | map { _errortype_hash($_->[0]->example_response) } | |||||||||||||||||||||||||||
| 185 | 0 | sort { $a->[1] cmp $b->[1] } | |||||||||||||||||||||||||||
| 186 | 0 | grep { $_->[1] } # Skip the default structure | |||||||||||||||||||||||||||
| 187 | 0 | map { [ $_, $_->full_name ] } | |||||||||||||||||||||||||||
| 188 | $repo->get_errortypes | ||||||||||||||||||||||||||||
| 189 | ]; | ||||||||||||||||||||||||||||
| 190 | 0 | my $ret = { | |||||||||||||||||||||||||||
| 191 | methods => { method => $methods }, | ||||||||||||||||||||||||||||
| 192 | errortypes => { errortype => $errortypes }, | ||||||||||||||||||||||||||||
| 193 | }; | ||||||||||||||||||||||||||||
| 194 | 0 | return $ret; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 1 | ▶ | sub _errortype_hash { | ||||||||||||||||||||||||||
|
Subroutine
_errortype_hash: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 198 | 1 | my $example = shift; | |||||||||||||||||||||||||||
| 199 | 1 | my $xml = $example->as_xml->xml; | |||||||||||||||||||||||||||
| 200 | my $ret = { | ||||||||||||||||||||||||||||
| 201 | code => $xml->findvalue('/rsp/err/@code'), | ||||||||||||||||||||||||||||
| 202 | 1 | -text => join('', map { $_->toString } ($xml->findnodes('/rsp/err/*'))) | |||||||||||||||||||||||||||
| 203 | }; | ||||||||||||||||||||||||||||
| 204 | 1 | return $ret; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =back | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head1 BUGS | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | None known. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Please report any bugs you find via the CPAN RT system. | ||||||||||||||||||||||||||||
| 214 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody> | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | Copyright Fotango 2005. All rights reserved. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Please see the main L<Froody> documentation for details of who has worked | ||||||||||||||||||||||||||||
| 221 | on this project. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 224 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | L<Froody> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 20 | 1; | |||||||||||||||||||||||||||