File Coverage

blib/lib/Exercises/API.pm
Criterion Covered Total %
statement 26 58 44.8
branch 0 4 0.0
condition n/a
subroutine 9 13 69.2
pod 0 1 0.0
total 35 76 46.0


line stmt bran cond sub pod time code
1             package Exercises::API;
2              
3             # ABSTRACT: API Ninja's Exercises API
4              
5 1     1   399084 use v5.38;
  1         4  
6 1     1   6 use strict;
  1         3  
  1         46  
7 1     1   5 use warnings;
  1         6  
  1         77  
8 1     1   950 use Moose;
  1         680766  
  1         5  
9 1     1   6400 use LWP;
  1         43025  
  1         40  
10 1     1   801 use JSON;
  1         9145  
  1         6  
11 1     1   120 use Carp;
  1         1  
  1         50  
12 1     1   1734 use Readonly;
  1         3039  
  1         55  
13              
14 1     1   383 use Exercises::API::Exercise;
  1         7  
  1         405  
15              
16             our $VERSION = '0.001';
17              
18             Readonly my $API_BASE_URL => 'https://api.api-ninjas.com/v1/exercises';
19              
20             has 'ua' => (
21             isa => 'LWP::UserAgent',
22             is => 'ro',
23             lazy_build => 1,
24             );
25              
26             has 'apikey' => (
27             required => 1,
28             isa => 'Maybe[Str]',
29             is => 'ro'
30             );
31              
32 0     0 0   sub exercises( $self, %args ) {
  0            
  0            
  0            
33 0           my $path = $self->_build_path(%args);
34 0           my $exercises_list = $self->_request($path);
35 0           my @exercises;
36              
37 0           for my $exercise (@$exercises_list) {
38 0           push @exercises, Exercises::API::Exercise->new($exercise);
39             }
40              
41 0           return @exercises;
42             }
43              
44 0     0     sub _build_path( $self, %args ) {
  0            
  0            
  0            
45 0           my $uri = URI->new($API_BASE_URL);
46              
47 0 0         $uri->query( $uri->query_form(%args) ) if %args;
48              
49 0           return $uri;
50             }
51              
52 0     0     sub _build_ua($self) {
  0            
  0            
53              
54 0           my $ua = LWP::UserAgent->new;
55 0           $ua->default_header( 'X-Api-Key' => $self->apikey );
56              
57 0           return $ua;
58             }
59              
60 0     0     sub _request( $self, $uri ) {
  0            
  0            
  0            
61 0           my $response = $self->ua->get($uri);
62 0 0         if ( $response->is_success ) {
63 0           return decode_json( $response->decoded_content );
64             }
65             else {
66 0           my $code = $response->code;
67 0           confess "Exercises API status code ($code)\n"
68             . "Error: "
69             . $response->status_line;
70             }
71             }
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Exercises::API - API Ninja's Exercises API
84              
85             =head1 VERSION
86              
87             version 0.001
88              
89             =head1 SYNOPSIS
90              
91             use Exercises::API;
92              
93             # Set API Ninja Exercise API Key
94             my $ea = Exercises::API->new(apikey => $ENV{'AN_EXERCISES_APIKEY'});
95              
96             # A list of exercises
97             my @exercises = $ea->exercises;
98              
99             for my $exercise (@exercises){
100             print "Name: " . $exercise->name . "\n";
101             print "Type: " . $exercise->type . "\n";
102             print "Muscle: " . $exercise->muscle . "\n";
103             print "Equipment: " . $exercise->equipment . "\n";
104             print "Difficulty: " . $exercise->difficulty . "\n";
105             print "Instructions: " . $exercise->instructions . "\n";
106              
107             }
108              
109             # Specifying the parameters
110             my %args = (
111             name => 'press',
112             type => 'strength',
113             muscle => 'chest',
114             difficulty => 'beginner',
115             # offset => 0 (is a premium feature/parameter)
116             );
117              
118             # A list of exercises based on the specified parameters
119             my @exercisesParams = $ea->exercises(%args);
120              
121             for my $exercise (@exercises){
122             print "Name: " . $exercise->name . "\n";
123             print "Type: " . $exercise->type . "\n";
124             print "Muscle: " . $exercise->muscle . "\n";
125             print "Equipment: " . $exercise->equipment . "\n";
126             print "Difficulty: " . $exercise->difficulty . "\n";
127             print "Instructions: " . $exercise->instructions . "\n";
128             }
129              
130             =head1 DESCRIPTION
131              
132             The L<Exercises API|https://www.api-ninjas.com/api/exercises> provides access to a comprehensive list of thousands of exercises targeting every major muscle group.
133              
134             Returns up to 5 exercises that satisfy the given parameters.
135              
136             =head1 API Key (required)
137              
138             You can get an API Key at L<API Ninjas|https://www.api-ninjas.com>.
139              
140             =head1 Parameters
141              
142             =head2 name (optional)
143              
144             Name of exercise. This value can be partial (e.g. press will match Dumbbell Bench Press).
145              
146             =head2 type (optional)
147              
148             Exercise type. Possible values are:
149              
150             cardio
151             olympic_weightlifting
152             plyometrics
153             powerlifting
154             strength
155             stretching
156             strongman
157              
158             =head2 muscle (optional)
159              
160             Muscle group targeted by the exercise. Possible values are:
161              
162             abdominals
163             abductors
164             adductors
165             biceps
166             calves
167             chest
168             forearms
169             glutes
170             hamstrings
171             lats
172             lower_back
173             middle_back
174             neck
175             quadriceps
176             traps
177             triceps
178              
179             =head2 difficulty (optional)
180              
181             Difficulty level of the exercise. Possible values are:
182              
183             beginner
184             intermediate
185             expert
186              
187             =head2 offset (optional) - premium
188              
189             Number of results to offset for pagination. Default is 0.
190              
191             =head1 Installation
192              
193             =head2 cpanm
194              
195             cpanm Exercises::API
196              
197             =head2 Project Directory
198              
199             cpanm --installdeps .
200             perl Makefile.PL
201             make
202             make install
203              
204             =head1 AUTHOR
205              
206             Nobunaga <nobunaga@cpan.org>
207              
208             =head1 COPYRIGHT AND LICENSE
209              
210             This software is Copyright (c) 2024 by Rayhan Alcena.
211              
212             This is free software, licensed under:
213              
214             The MIT (X11) License
215              
216             =cut