HealthGraph.pm

filter: Statementstmt 85.8% 73 / 85 Branchbran 50.0% 9 / 18 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.4% 13 / 17 Podpod 100% 3 / 3 totaltotal 75.3% 101 / 134 CC 13 SCAR 15.5
CC
13
Cov
77%
get
11.2
_build_ua
3.1
uri_for
3.1
CRAP 4.7
« HealthGraph/Response.pm ↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package WebService::HealthGraph;
4 2 $WebService::HealthGraph::VERSION = '0.000004';
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Types::Standard qw( Bool HashRef InstanceOf Int Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Types::URI qw( Uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use URI::FromHash qw( uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use WebService::HealthGraph::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 has auto_pagination => (
15 is => 'ro',
16 isa => Bool,
17 default => 1,
18 );
19  
20 2 has base_url => (
21 is => 'ro',
22 isa => Uri,
23 lazy => 1,
24 default => 'https://api.runkeeper.com',
25 coerce => 1,
26 );
27  
28 2 has debug => (
29 is => 'ro',
30 isa => Bool,
31 default => 0,
32 );
33  
34 2 has token => (
35 is => 'ro',
36 isa => Str,
37 predicate => '_has_token',
38 );
39  
40 2 has ua => (
41 is => 'ro',
42 isa => InstanceOf ['LWP::UserAgent'],
43 lazy => 1,
44 builder => '_build_ua',
45 );
46  
47 2 has url_map => (
48 is => 'ro',
49 isa => HashRef,
50 lazy => 1,
51 builder => '_build_url_map',
52 );
53  
54 2 has user => (
55 is => 'ro',
56 isa => InstanceOf ['WebService::HealthGraph::Response'],
57 lazy => 1,
58 builder => '_build_user',
59 );
60  
61 has user_id => (
62 is => 'ro',
63 isa => Int,
64 lazy => 1,
65 0 ▶ default => sub { shift->user->content->{userID} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 2 );
67  
68 1 ▶sub _build_ua {
Subroutine _build_ua: called CC=3 SCAR=11.4
69 1 my $self = shift;
70 1 my $ua = LWP::UserAgent->new;
71 1 ▶ if ( $self->_has_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_token) 0 1 1
72 0 $ua->default_header( Authorization => 'Bearer ' . $self->token );
73 }
74  
75 1 ▶ return $ua unless $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 0 1 1
76 1 require LWP::ConsoleLogger::Easy;
77 1 LWP::ConsoleLogger::Easy::debug_ua($ua);
78 1 return $ua;
79 }
80  
81 0 ▶sub _build_url_map {
Subroutine _build_url_map: not called CC=1 SCAR=6.9
82 0 my $self = shift;
83 0 my %map = %{ $self->user->content };
84 0 delete $map{userID};
85 0 return \%map;
86 }
87  
88 1 ▶sub _build_user {
Subroutine _build_user: called CC=1 SCAR=0.0
89 1 my $self = shift;
90 1 return $self->get('/user');
91 }
92  
93 1 ▶sub get {
Subroutine get: called CC=9 SCAR=24.2
Pod documented
94 1 my $self = shift;
95 1 my $url = shift;
96 1 my $args = shift;
97 1 ▶ my $headers = $args->{headers} || {};
Condition50%1/2
$args->{'headers'} || {}
l!l
01
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table50%1/2
A
A= $args->{'headers'}
Aresult
11
00
98 1 ▶ my $feed = $args->{feed} || 0;
Condition50%1/2
$args->{'feed'} || 0
l!l
01
MC/DC0%0/1
$args->{'feed'} || 0
$args->{'feed'}
Truth table50%1/2
A
A= $args->{'feed'}
Aresult
11
00
99  
100 1 ▶ $url = URI->new($url) unless ref $url;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $url 1 0 1
101  
102 1 my $path = $url->path;
103  
104 1 $url->scheme( $self->base_url->scheme );
105 1 $url->host( $self->base_url->host );
106  
107 1 my @path_parts = $url->path_segments;
108 1 shift @path_parts; # first part is empty string with an absolute URL
109 1 my $top_level = shift @path_parts;
110  
111 1 my %type = (
112 backgroundActivities => 'BackgroundActivitySet',
113 changeLog => 'ChangeLog',
114 diabetes => 'DiabetesMeasurementSet',
115 fitnessActivities => 'FitnessActivity',
116 generalMeasurements => 'GeneralMeasurementSet',
117 nutrition => 'NutritionSet',
118 profile => 'Profile',
119 records => 'Records',
120 settings => 'Settings',
121 sleep => 'SleepSet',
122 strengthTrainingActivities => 'StrengthTrainingActivity',
123 team => 'Team',
124 user => 'User',
125 weight => 'WeightSet',
126 );
127  
128 1 ▶ unless ( exists $headers->{Accept} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $headers->{"Accept"}) 1 0 1
129 1 my $accept = $type{$top_level};
130  
131 # Weird exception to the rule
132 1 ▶ if ( @path_parts and $top_level eq 'team' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path_parts and $top_level eq "team") 0 1 1
Condition33%1/3
@path_parts and $top_level eq "team"
!ll&&!rl&&r
100
MC/DC0%0/2
@path_parts and $top_level eq "team"
@path_parts $top_level eq "team"
Truth table33%1/3
A and B
A= @path_partsB= $top_level eq "team"
ABresult
0X0
100
111
133 0 $accept = 'Member';
134 }
135  
136 1 ▶ $accept .= 'Feed' if $feed;
Branch: 1 branch
BranchTrueFalseTotal
if $feed 0 1 1
137  
138 $headers->{Accept}
139 1 = sprintf( 'application/vnd.com.runkeeper.%s+json', $accept );
140 }
141  
142 # Fix up URLs with a semicolon delimiter.
143 1 ▶ if ( $url =~ m{;} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /;/) 0 1 1
144 0 $url = $url->as_string;
145 0 $url =~ s{;}{&}g;
146 }
147  
148 1 my $res = $self->ua->get( $url, %{$headers} );
149 return WebService::HealthGraph::Response->new(
150 0 ▶ get => sub { $self->get( shift, $args ) }, raw => $res );
Subroutine __ANON__: not called CC=1 SCAR=1.2
151 }
152  
153 0 ▶sub url_for { return shift->uri_for(@_) }
Subroutine url_for: not called CC=1 SCAR=6.9
Pod documented
154  
155 1 ▶sub uri_for {
Subroutine uri_for: called CC=3 SCAR=11.3
Pod documented
156 1 my $self = shift;
157 1 my $type = shift;
158 1 my $query = shift;
159  
160 1 ▶ die "type $type not found" unless exists $self->url_map->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->url_map->{$type} 0 1 1
161  
162 return uri(
163 1 ▶ path => $self->url_map->{$type},
Branch: 1 branch
BranchTrueFalseTotal
$query ? : 0 1 1
164 $query
165 ? (
166 query => $query,
167 query_separator => '&',
168 )
169 : (),
170 );
171 }
172  
173 2 1;
174  
175 =pod
176  
177 =encoding UTF-8
178  
179 =head1 NAME
180  
181 WebService::HealthGraph - A thin wrapper around the Runkeeper (Health Graph) API
182  
183 =head1 VERSION
184  
185 version 0.000004
186  
187 =head1 SYNOPSIS
188  
189 my $runkeeper = WebService::HealthGraph->new(
190 debug => 1,
191 token => 'foo',
192 );
193  
194 my $user = $runkeeper->user;
195  
196 use Data::Printer;
197 p $user->content;
198  
199 # Fetch a weight feed
200  
201 use DateTime ();
202 use URI::FromHash qw( uri );
203  
204 my $cutoff = DateTime->now->subtract( days => 7 );
205  
206 my $uri = uri(
207 path => '/weight',
208 query => { noEarlierThan => $cutoff->ymd },
209 );
210  
211 my $feed = $runkeeper->get($uri, { feed => 1 });
212 p $feed->content;
213  
214 =head1 DESCRIPTION
215  
216 BETA BETA BETA. The interface is subject to change.
217  
218 This is a very thin wrapper around the Runkeeper (Health Graph) API. At this
219 point it assumes that you already have an OAuth token to connect with. You can
220 use L<Mojolicious::Plugin::Web::Auth::Site::Runkeeper> to create a token. If
221 that doesn't suit you, patches to add OAuth token retrieval to this module will
222 be happily accepted.
223  
224 =head1 CONSTRUCTOR ARGUMENTS
225  
226 =head2 auto_pagination
227  
228 Boolean. If enabled, response objects will continue to fetch new result pages
229 as the iterator requires them. Defaults to true.
230  
231 =head2 base_url
232  
233 The URL of the API. Defaults to L<https://api.runkeeper.com>. This is
234 settable in case you'd need this for mocking.
235  
236 =head2 debug( $bool )
237  
238 Turns on debugging via L<LWP::ConsoleLogger>. Off by default.
239  
240 =head2 token
241  
242 OAuth token. Optional, but you'll need to to get any URLs.
243  
244 =head2 ua
245  
246 A user agent object of the L<LWP::UserAgent> family. If you provide your own,
247 be sure you set the correct default headers required for authentication.
248  
249 =head2 url_map
250  
251 Returns a map of keys to URLs, as provided by the C<user> endpoint. Runkeeper
252 wants you to use these URLs rather than constructing your own.
253  
254 =head2 uri_for
255  
256 Gives you the corresponding url (in the form of an L<URI> object) for any key
257 which exists in C<url_map>. You can optionally pass a HashRef of query params
258 to this method.
259  
260 my $team_uri = $runkeeper->uri_for( 'team', { pageSize => 10 } );
261  
262 my $friends = $runkeeper->get(
263 $runkeeper->uri_for( 'team', { pageSize => 10 } ),
264 { feed => 1 }
265 );
266  
267 =head2 url_for
268  
269 Convenience method which points to C<url_for>. Will be removed in a later
270 release.
271  
272 =head2 user
273  
274 The L<WebService::HealthGraph::Response> object for the C<user> endpoint.
275  
276 =head2 user_id
277  
278 The id of the user as provided by the C<user> endpoint.
279  
280 =head1 METHODS
281  
282 =head2 get( $url, $optional_args )
283  
284 This module will try to do the right thing with the minimum amount of
285 information:
286  
287 my $weight_response = $runkeeper->get( 'weight', { feed => 1 } );
288 if ( $weight_response->success ) {
289 ...
290 }
291  
292 Optionally, you can provide your own Accept (or other) headers:
293  
294 my $record_response = $runkeeper->get(
295 'records',
296 {
297 headers =>
298 { Accept => 'application/vnd.com.runkeeper.Records+json' }
299 );
300  
301 Returns a L<WebService::HealthGraph::Response> object.
302  
303 =head1 CAVEATS
304  
305 Most response content will contain a C<HashRef>, but the C<records> endpoint
306 returns a response with an C<ArrayRef> in the content.
307  
308 =head1 AUTHOR
309  
310 Olaf Alders <olaf@wundercounter.com>
311  
312 =head1 COPYRIGHT AND LICENSE
313  
314 This software is copyright (c) 2016 by Olaf Alders.
315  
316 This is free software; you can redistribute it and/or modify it under
317 the same terms as the Perl 5 programming language system itself.
318  
319 =cut
320  
321 __END__
« HealthGraph/Response.pm ↑ Summary