Up.pm

filter: Statementstmt 83.0% 83 / 100 Branchbran 40.0% 8 / 20 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 84.0% 21 / 25 Podpod 100% 6 / 6 totaltotal 73.3% 121 / 165 CC 15 SCAR 14.0
CC
15
Cov
73%
_request
20.0
_get
6.0
band
4.2
CRAP 4.1
« Up/Mock.pm ↑ Summary Up/JSON.pm »
1 package WWW::Jawbone::Up;
2  
3 6 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '1.32.4';
8  
9 =encoding utf-8
10  
11 =head1 NAME
12  
13 WWW::Jawbone::Up - Unofficial Jawbone UP API
14  
15 =head1 SYNOPSIS
16  
17 use WWW::Jawbone::Up;
18  
19 my $up = WWW::Jawbone::Up->connect('alan@eatabrick.org', 's3kr3t');
20  
21 my $user = $up->user;
22 my $score = $up->score;
23  
24 say $user->name . ' walked ' . $score->move->distance . 'km today';
25  
26 my $hours = int($score->sleep->asleep / 3600);
27 my $minutes = int($score->sleep->asleep / 60); % 60;
28  
29 say $user->name . " slept for ${hours}h${minutes}m last night";
30  
31 =head1 DESCRIPTION
32  
33 WWW::Jawbone::Up is a perl binding to the unofficial Jawbone UP API. After
34 authenticating you can find interesting bits of data. The version number of
35 this library should reflect the associated version of the Jawbone API but I
36 have no way of knowing if they will be sane with their version numbers so we'll
37 see how that pans out.
38  
39 =head1 METHODS
40  
41 =cut
42  
43 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 ▶use JSON 2.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 6 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 6 ▶use WWW::Jawbone::Up::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 6 ▶use WWW::Jawbone::Up::Score;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 6 ▶use WWW::Jawbone::Up::Tick;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 6 ▶use WWW::Jawbone::Up::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 6 ▶use WWW::Jawbone::Up::Workout;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 6 ▶use constant URI_BASE => 'https://jawbone.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 6 ▶use constant URI_API => URI_BASE . '/nudge/api/v.1.32';
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 0 ▶sub _request {
Subroutine _request: not called CC=4 SCAR=30.0
59 0 my ($self, $method, $uri, $data) = @_;
60  
61 0 ▶ $self->{ua} ||= LWP::UserAgent->new();
Condition0%0/3
$self->{"ua"} ||= "LWP::UserAgent"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ua"} ||= "LWP::UserAgent"->new
$self->{"ua"} "LWP::UserAgent"->new
Truth table0%0/3
A ||= B
A= $self->{"ua"}B= "LWP::UserAgent"->new
ABresult
1X1
011
000
62  
63 0 my $request = HTTP::Request->new($method, $uri);
64 0 ▶ $request->header('x-nudge-token', $self->{token}) if $self->{token};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"token"} 0 0 0
65 0 $request->content($data);
66  
67 0 my $response = $self->{ua}->request($request);
68  
69 0 ▶ croak $response->status_line if $response->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_error 0 0 0
70  
71 0 return decode_json($response->decoded_content);
72 }
73  
74 0 ▶sub __encode {
Subroutine __encode: not called CC=1 SCAR=6.9
75 0 my ($hash) = @_;
76  
77 return join '&',
78 0 map sprintf('%s=%s', uri_escape($_), uri_escape($hash->{$_})),
79 keys %$hash;
80 }
81  
82 0 ▶sub _get {
Subroutine _get: not called CC=2 SCAR=17.9
83 0 my ($self, $uri, $data) = @_;
84  
85 0 my $query_string = __encode($data);
86 0 ▶ $uri .= ($uri =~ /\?/ ? '&' : '?') . $query_string;
Branch: 1 branch
BranchTrueFalseTotal
$uri =~ /\?/ ? : 0 0 0
87  
88 0 return $self->_request(GET => $uri);
89 }
90  
91 0 ▶sub _post {
Subroutine _post: not called CC=1 SCAR=6.9
92 0 my ($self, $uri, $data) = @_;
93  
94 0 my $body = __encode($data);
95  
96 0 return $self->_request(POST => $uri, $body);
97 }
98  
99 =head2 connect($email, $password)
100  
101 Authenticates with the API. Will return undef if there are any errors,
102 possibly also dying or warning about them.
103  
104 =cut
105  
106 7 ▶sub connect {
Subroutine connect: called CC=2 SCAR=6.9
Pod documented
107 7 my ($class, $email, $password) = @_;
108  
109 7 my $self = bless {}, $class;
110  
111 7 my $json = $self->_post(
112 URI_BASE . '/user/signin/login', {
113 service => 'nudge',
114 email => $email,
115 pwd => $password,
116 });
117  
118 7 ▶ if ($json->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"error"}) 1 6 7
119 1 carp $json->{error}{msg};
120 1 return undef;
121 }
122  
123 6 $self->{token} = $json->{token};
124  
125 6 return $self;
126 }
127  
128 =head2 user()
129  
130 Returns a L<WWW::Jawbone::Up::User> object representing the currently
131 authenticated user.
132  
133 =cut
134  
135 1 ▶sub user {
Subroutine user: called CC=2 SCAR=7.0
Pod documented
136 1 my ($self) = @_;
137  
138 1 ▶ unless ($self->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"user"}) 1 0 1
139 1 my $json = $self->_get(URI_API . '/users/@me');
140 1 $self->{user} = WWW::Jawbone::Up::User->new($json->{data});
141 }
142  
143 1 return $self->{user};
144 }
145  
146 =head2 feed($date)
147  
148 Returns an array of L<WWW::Jawbone::Up::Feed> objects for the given C<$date>.
149 If no date is given, the current date is used.
150  
151 =cut
152  
153 1 ▶sub feed {
Subroutine feed: called CC=3 SCAR=11.2
Pod documented
154 1 my ($self, $date, $options) = @_;
155  
156 1 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
01
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
157 1 ▶ $options->{date} = $date if defined $date;
Branch: 1 branch
BranchTrueFalseTotal
if defined $date 1 0 1
158  
159 1 my $json = $self->_get(URI_API . '/users/@me/social', $options);
160  
161 1 return map WWW::Jawbone::Up::Feed->new($_), @{ $json->{data}{feed} };
162 }
163  
164 =head2 score($date)
165  
166 Returns a L<WWW::Jawbone::Up::Score> object for the given C<$date>. If no date
167 is given, the current date is used.
168  
169 =cut
170  
171 1 ▶sub score {
Subroutine score: called CC=2 SCAR=7.0
Pod documented
172 1 my ($self, $date, $options) = @_;
173  
174 1 $options = {};
175 1 ▶ $options->{date} = $date if defined $date;
Branch: 1 branch
BranchTrueFalseTotal
if defined $date 1 0 1
176  
177 1 my $json = $self->_get(URI_API . '/users/@me/score', $options);
178  
179 1 return WWW::Jawbone::Up::Score->new($json->{data});
180 }
181  
182 =head2 band($start, $end)
183  
184 Returns an array of L<WWW::Jawbone::Up::Tick> objects for the given time range.
185 Both C<$start> and C<$end> should be epoch times (seconds since epoch). If no
186 range is given, I think the current day is used, but I haven't really
187 researched that fact too much.
188  
189 =cut
190  
191 1 ▶sub band {
Subroutine band: called CC=4 SCAR=14.3
Pod documented
192 1 my ($self, $start, $end, $options) = @_;
193  
194 1 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
01
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
195 1 ▶ $options->{start_time} = $start if defined $start;
Branch: 1 branch
BranchTrueFalseTotal
if defined $start 1 0 1
196 1 ▶ $options->{end_time} = $end if defined $end;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end 1 0 1
197  
198 1 my $json = $self->_get(URI_API . '/users/@me/band', $options);
199  
200 return map WWW::Jawbone::Up::Tick->new($_->{value}),
201 1 @{ $json->{data}{ticks} };
202 }
203  
204 =head2 workouts($date)
205  
206 Returns an array of L<WWW::Jawbone::Up::Workout> objects for the give C<$date>.
207 If no $date is given, I think some number of recent workouts are given, but I
208 haven't really researched that fact too much.
209  
210 =cut
211  
212 1 ▶sub workouts {
Subroutine workouts: called CC=3 SCAR=11.2
Pod documented
213 1 my ($self, $date, $options) = @_;
214  
215 1 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
01
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
216 1 ▶ $options->{date} = $date if defined $date;
Branch: 1 branch
BranchTrueFalseTotal
if defined $date 1 0 1
217  
218 1 my $json = $self->_get(URI_API . '/users/@me/workouts', $options);
219  
220 1 return map WWW::Jawbone::Up::Workout->new($_), @{ $json->{data}{items} };
221 }
222  
223 6 1;
224  
225 =head1 TODO
226  
227 The documentation for this distribution is utterly lacking. I will fix that
228 sometime soon.
229  
230 =head1 AUTHOR
231  
232 Alan Berndt E<lt>alan@eatabrick.orgE<gt>
233  
234 =head1 COPYRIGHT
235  
236 Copyright 2013 Alan Berndt
237  
238 =head1 LICENSE
239  
240 Permission is hereby granted, free of charge, to any person obtaining a copy of
241 this software and associated documentation files (the "Software"), to deal in
242 the Software without restriction, including without limitation the rights to
243 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
244 of the Software, and to permit persons to whom the Software is furnished to do
245 so, subject to the following conditions:
246  
247 The above copyright notice and this permission notice shall be included in all
248 copies or substantial portions of the Software.
249  
250 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
251 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
252 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
253 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
254 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
255 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
256 SOFTWARE.
257  
258 =head1 SEE ALSO
259  
260 L<WWW::Jawbone::Up::Feed>
261 L<WWW::Jawbone::Up::Score>
262 L<WWW::Jawbone::Up::Tick>
263 L<WWW::Jawbone::Up::User>
264 L<WWW::Jawbone::Up::Workout>
265  
266 =cut
« Up/Mock.pm ↑ Summary Up/JSON.pm »