File Coverage

blib/lib/Webservice/Judobase/General.pm
Criterion Covered Total %
statement 21 50 42.0
branch 0 10 0.0
condition n/a
subroutine 7 10 70.0
pod 3 3 100.0
total 31 73 42.4


line stmt bran cond sub pod time code
1 1     1   5 use strict;
  1         2  
  1         25  
2 1     1   8 use warnings;
  1         2  
  1         35  
3              
4             package Webservice::Judobase::General;
5             $Webservice::Judobase::General::VERSION = '0.09';
6             # VERSION
7              
8 1     1   4 use Moo;
  1         2  
  1         4  
9 1     1   284 use HTTP::Request;
  1         1  
  1         17  
10 1     1   15 use JSON::Tiny 'decode_json';
  1         2  
  1         47  
11 1     1   5 use LWP::UserAgent;
  1         2  
  1         34  
12              
13 1     1   5 use namespace::clean;
  1         7  
  1         5  
14              
15             has 'ua' => (
16             is => 'ro',
17             required => 1,
18             );
19              
20             has 'url' => (
21             is => 'ro',
22             required => 1,
23             );
24              
25             sub competition {
26 0     0 1   my ( $self, %args ) = @_;
27 0 0         return { error => 'id parameter is required' } unless defined $args{id};
28              
29             my $url
30             = $self->url
31             . '?params[action]=general.get_one'
32             . '&params[module]=competition'
33             . '&params[id]='
34 0           . $args{id};
35              
36 0           my $request = HTTP::Request->new( GET => $url );
37              
38 0           my $response = $self->ua->request($request);
39              
40 0 0         return decode_json $response->content
41             if $response->code == 200;
42              
43 0           return { error => 'Error retreiving competitor info' };
44             }
45              
46             sub competitions {
47 0     0 1   my $self = shift;
48              
49 0           my $url
50             = $self->url
51             . '?params[action]=competition.get_list'
52             . '&params[limit]=9999'
53             . '&params[sort]=-1';
54              
55 0           my $request = HTTP::Request->new( GET => $url );
56              
57 0           my $response = $self->ua->request($request);
58              
59 0 0         return decode_json $response->content
60             if $response->code == 200;
61              
62 0           return { error => 'Error retreiving competitions info' };
63             }
64              
65             sub competitors {
66 0     0 1   my ( $self, %args ) = @_;
67             return { error => 'event_id parameter is required' }
68 0 0         unless defined $args{event_id};
69              
70             my $url
71             = $self->url
72             . '?params[action]=competition.competitors'
73             . '&params[id_competition]='
74 0           . $args{event_id};
75              
76 0           my $request = HTTP::Request->new( GET => $url );
77              
78 0           my $response = $self->ua->request($request);
79              
80 0           my @competitors = ();
81 0 0         if ( $response->code == 200 ) {
82 0           my $info = decode_json $response->content;
83              
84 0           for my $gender ( values %{ $info->{categories} } ) {
  0            
85 0           for my $cat ( values %$gender ) {
86 0           for my $person ( values %{ $cat->{persons} } ) {
  0            
87 0           push @competitors, $person;
88             }
89             }
90             }
91              
92 0           return \@competitors;
93             }
94             else {
95 0           return { error => 'Error retreiving competitors info' };
96             }
97             }
98              
99             1;