File Coverage

lib/Dancer/Plugin/Swagger/Path.pm
Criterion Covered Total %
statement 54 79 68.3
branch 10 26 38.4
condition 1 10 10.0
subroutine 15 19 78.9
pod 0 5 0.0
total 80 139 57.5


line stmt bran cond sub pod time code
1             package Dancer::Plugin::Swagger::Path;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Internal representation of a swagger path
4             $Dancer::Plugin::Swagger::Path::VERSION = '0.2.0';
5              
6 2     2   7 use strict;
  2         2  
  2         44  
7 2     2   6 use warnings;
  2         2  
  2         37  
8              
9 2     2   822 use Moo;
  2         13492  
  2         24  
10              
11 2     2   2638 use MooseX::MungeHas 'is_ro';
  2         5298  
  2         10  
12              
13 2     2   1051 use Carp;
  2         2  
  2         86  
14 2     2   813 use Hash::Merge;
  2         3336  
  2         78  
15 2     2   661 use Clone 'clone';
  2         3539  
  2         97  
16 2     2   790 use List::AllUtils qw/ first any none /;
  2         20543  
  2         156  
17 2     2   1111 use JSON;
  2         15543  
  2         6  
18 2     2   935 use Class::Load qw/ load_class /;
  2         14928  
  2         1224  
19              
20             has route => ( handles => [ 'pattern' ] );
21              
22             has tags => ( predicate => 1 );
23              
24             has method => sub {
25 8 50   8   647 eval { $_[0]->route->method }
  8         27  
26             or croak "no route or explicit method provided to path";
27             };
28              
29             has path => sub {
30 8     8   653 dancer_pattern_to_swagger_path( $_[0]->route->pattern );
31             };
32              
33             has responses => ( predicate => 1);
34              
35             has description => ( predicate => 1 );
36              
37             has parameters =>
38             lazy => 1,
39             default => sub { [] },
40             predicate => 1,
41             ;
42              
43             # TODO allow to pass a hashref instead of an arrayref
44             sub parameter {
45 0     0 0 0 my( $self, $param, $args ) = @_;
46              
47 0   0     0 $args ||= {};
48 0   0     0 $args->{name} ||= $param;
49              
50 0     0   0 my $p = first { $_->{name} eq $param } @{ $self->parameters };
  0         0  
  0         0  
51            
52 0 0       0 push @{ $self->parameters || [] }, $p = { name => $param }
  0 0       0  
53             unless $p;
54              
55 0         0 %$p = %{Hash::Merge::merge( $p, $args )};
  0         0  
56             }
57              
58             sub dancer_pattern_to_swagger_path {
59 8     8 0 42 my $pattern = shift;
60 8         9 $pattern =~ s#(?<=/):(\w+)(?=/|$)#{$1}#g;
61 8         17 return $pattern;
62             }
63              
64             sub add_to_doc {
65 8     8 0 45 my( $self, $doc ) = @_;
66              
67 8         82 my $path = $self->path;
68 8         87 my $method = $self->method;
69              
70             # already there
71 8 50       83 next if $doc->{paths}{$path}{$method};
72              
73 8   50     38 my $m = $doc->{paths}{$path}{$method} ||= {};
74              
75 8 100       29 $m->{description} = $self->description if $self->has_description;
76 8 100       70 $m->{parameters} = $self->parameters if $self->has_parameters;
77 8 50       378 $m->{tags} = $self->tags if $self->has_tags;
78              
79 8 100       27 if( $self->has_responses ) {
80 1         12 $m->{responses} = clone $self->responses;
81              
82 1         2 for my $r ( values %{$m->{responses}} ) {
  1         3  
83 1         1 delete $r->{template};
84              
85 1 50       5 if( my $example = delete $r->{example} ) {
86 0         0 my $serializer = Dancer::engine('serializer');
87             die "Don't know content type for serializer ", ref $serializer
88 0 0   0   0 if none { $serializer->isa($_) } qw/ Dancer::Serializer::JSON Dancer::Serializer::YAML /;
  0         0  
89 0         0 $r->{examples}{$serializer->content_type} = $example;
90             }
91             }
92             }
93              
94              
95             }
96              
97             sub validate_response {
98 0     0 0 0 my( $self, $code, $data, $strict ) = @_;
99              
100 0         0 my $schema = $self->responses->{$code}{schema};
101              
102 0 0 0     0 die "no schema found for return code $code for ", join ' ' , uc($self->method), $self->path
103             unless $schema or not $strict;
104              
105 0 0       0 return unless $schema;
106              
107 0         0 my $plugin = Dancer::Plugin::Swagger->instance;
108              
109             $schema = {
110             definitions => $plugin->doc->{definitions},
111 0         0 properties => { response => $schema },
112             };
113              
114 0         0 my $result = load_class('JSON::Schema::AsType')->new( schema => $schema)->validate_explain({ response => $data });
115              
116 0 0       0 return unless $result;
117              
118 0         0 die join "\n", map { "* " . $_ } @$result;
  0         0  
119             }
120              
121             sub BUILD {
122 8     8 0 3655 my $self = shift;
123              
124 8         11 for my $param ( eval { @{ $self->route->{_params} } } ) {
  8         5  
  8         140  
125 0           $self->parameter( $param => {
126             in => 'path',
127             required => JSON::true,
128             type => "string",
129             } );
130             }
131             }
132              
133             1;
134              
135             __END__
136              
137             =pod
138              
139             =encoding UTF-8
140              
141             =head1 NAME
142              
143             Dancer::Plugin::Swagger::Path - Internal representation of a swagger path
144              
145             =head1 VERSION
146              
147             version 0.2.0
148              
149             =head1 DESCRIPTION
150              
151             Objects of this class are used by L<Dancer::Plugin::Swagger> to represent
152             a path in the Swagger document.
153              
154             =head1 AUTHOR
155              
156             Yanick Champoux <yanick@cpan.org>
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is copyright (c) 2015 by Yanick Champoux.
161              
162             This is free software; you can redistribute it and/or modify it under
163             the same terms as the Perl 5 programming language system itself.
164              
165             =cut