Foursquare.pm

filter: Statementstmt 79.6% 102 / 128 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 69.0% 29 / 42 Podpod 100% 17 / 17 totaltotal 78.4% 153 / 195 CC 5 SCAR 4.4
CC
5
Cov
79%
set_access_token
3.1
_get
3.0
users
2.0
CRAP 1.6
« Foursquare/Photos.pm ↑ Summary Foursquare/Config.pm »
1 package WWW::Foursquare;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.9906';
8  
9 3 ▶use WWW::Foursquare::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use WWW::Foursquare::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use WWW::Foursquare::Users;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use WWW::Foursquare::Venues;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use WWW::Foursquare::Venuegroups;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use WWW::Foursquare::Checkins;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use WWW::Foursquare::Tips;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use WWW::Foursquare::Lists;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use WWW::Foursquare::Updates;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use WWW::Foursquare::Photos;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use WWW::Foursquare::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use WWW::Foursquare::Specials;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use WWW::Foursquare::Campaigns;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use WWW::Foursquare::Events;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use WWW::Foursquare::Pages;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use WWW::Foursquare::Pageupdates;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 1 my ($class, %params) = @_;
33
34 1 my $self = {};
35 1 bless $self, $class;
36 1 $self->{client_id} = $params{client_id};
37 1 $self->{client_secret} = $params{client_secret};
38 1 $self->{debug} = $params{debug};
39 1 $self->{redirect_uri} = $params{redirect_uri};
40 1 $self->{request} = WWW::Foursquare::Request->new(\%params);
41
42 1 return $self;
43 }
44  
45 1 ▶sub get_auth_url {
Subroutine get_auth_url: called CC=1 SCAR=0.0
Pod documented
46 1 my $self = shift;
47  
48 my $params = {
49 client_id => $self->{client_id},
50 redirect_uri => $self->{redirect_uri},
51 1 response_type => 'code',
52 };
53 1 my $query = _params_to_str( $params );
54 1 my $url = sprintf "%s?%s", $AUTH_CODE_ENDPOINT, $query;
55  
56 1 return $url;
57 }
58  
59 2 ▶sub get_access_token {
Subroutine get_access_token: called CC=1 SCAR=0.0
Pod documented
60 2 my ($self, $code) = @_;
61  
62 my $params = {
63 client_id => $self->{client_id},
64 client_secret => $self->{client_secret},
65 code => $code,
66 grant_type => 'authorization_code',
67 redirect_uri => $self->{redirect_uri},
68 2 };
69  
70 2 my $res = $self->_get($ACCESS_TOKEN_ENDPOINT, $params);
71 1 return $res->{access_token};
72 }
73  
74 1 ▶sub set_access_token {
Subroutine set_access_token: called CC=3 SCAR=11.4
Pod documented
75 1 my ($self, $access_token) = @_;
76  
77 1 ▶ warn "access_token: $access_token" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
78  
79 # update params of request
80 1 $self->{request}->{access_token} = $access_token;
81 1 ▶ $self->{request}->{userless} = $access_token
Branch: 1 branch
BranchTrueFalseTotal
$access_token ? : 1 0 1
82 ? 0
83 : 1;
84 }
85  
86 0 ▶sub users {
Subroutine users: not called CC=1 SCAR=6.9
Pod documented
87 0 my ($self, $user_id) = @_;
88  
89 0 return WWW::Foursquare::Users->new($self->{request}, $user_id);
90 }
91  
92 0 ▶sub venues {
Subroutine venues: not called CC=1 SCAR=6.9
Pod documented
93 0 my ($self, $venue_id) = @_;
94  
95 0 return WWW::Foursquare::Venues->new($self->{request}, $venue_id);
96 }
97  
98 0 ▶sub venuegroups {
Subroutine venuegroups: not called CC=1 SCAR=6.9
Pod documented
99 0 my ($self, $group_id) = @_;
100  
101 0 return WWW::Foursquare::Venuegroups->new($self->{request}, $group_id);
102 }
103  
104 0 ▶sub checkins {
Subroutine checkins: not called CC=1 SCAR=6.9
Pod documented
105 0 my ($self, $checkin_id) = @_;
106  
107 0 return WWW::Foursquare::Checkins->new($self->{request}, $checkin_id);
108 }
109  
110 0 ▶sub tips {
Subroutine tips: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($self, $tip_id) = @_;
112  
113 0 return WWW::Foursquare::Tips->new($self->{request}, $tip_id);
114 }
115  
116 0 ▶sub lists {
Subroutine lists: not called CC=1 SCAR=6.9
Pod documented
117 0 my ($self, $list_id) = @_;
118  
119 0 return WWW::Foursquare::Lists->new($self->{request}, $list_id);
120 }
121  
122 0 ▶sub updates {
Subroutine updates: not called CC=1 SCAR=6.9
Pod documented
123 0 my ($self, $update_id) = @_;
124  
125 0 return WWW::Foursquare::Updates->new($self->{request}, $update_id);
126 }
127  
128 0 ▶sub photos {
Subroutine photos: not called CC=1 SCAR=6.9
Pod documented
129 0 my ($self, $photo_id) = @_;
130  
131 0 return WWW::Foursquare::Photos->new($self->{request}, $photo_id);
132 }
133  
134 0 ▶sub settings {
Subroutine settings: not called CC=1 SCAR=6.9
Pod documented
135 0 my ($self, $setting_id) = @_;
136  
137 0 return WWW::Foursquare::Settings->new($self->{request}, $setting_id);
138 }
139  
140 0 ▶sub campaigns {
Subroutine campaigns: not called CC=1 SCAR=6.9
Pod documented
141 0 my ($self, $campaign_id) = @_;
142  
143 0 return WWW::Foursquare::Campaigns->new($self->{request}, $campaign_id);
144 }
145  
146 0 ▶sub events {
Subroutine events: not called CC=1 SCAR=6.9
Pod documented
147 0 my ($self, $event_id) = @_;
148
149 0 return WWW::Foursquare::Events->new($self->{request}, $event_id);
150 }
151  
152 0 ▶sub pages {
Subroutine pages: not called CC=1 SCAR=6.9
Pod documented
153 0 my ($self, $page_id) = @_;
154  
155 0 return WWW::Foursquare::Pages->new($self->{request}, $page_id);
156 }
157  
158 0 ▶sub pageupdates {
Subroutine pageupdates: not called CC=1 SCAR=6.9
Pod documented
159 0 my ($self, $update_id) = @_;
160  
161 0 return WWW::Foursquare::Pageupdates->new($self->{request}, $update_id);
162 }
163  
164 3 ▶sub _params_to_str {
Subroutine _params_to_str: called CC=1 SCAR=0.0
165 3 my $hash = shift;
166  
167 3 my $str = join '&', map { $_.'='.uri_escape($hash->{$_}) } sort keys %$hash;
168 3 return $str;
169 }
170  
171 2 ▶sub _get {
Subroutine _get: called CC=3 SCAR=11.0
172 2 my ($self, $url, $params) = @_;
173  
174 2 my $query = _params_to_str($params);
175 2 my $result_url = sprintf "%s?%s", $url, $query;
176  
177 2 ▶ warn "$result_url" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
178  
179 2 my $res = $self->{request}->{ua}->get($result_url);
180  
181 2 ▶ return decode_json($res->content()) if $res->code() == 200;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 200 1 1 2
182  
183 # throw exception
184 1 die $res->content();
185 }
186  
187 2 ▶sub _ua {
Subroutine _ua: called CC=1 SCAR=0.0
188 2 my ($self) = @_;
189  
190 2 return $self->{request}->{ua};
191 }
192  
193  
194 3 1;
195  
196 =head1 NAME
197  
198 WWW::Foursquare - is a Perl wrapper for the Foursqauare API.
199  
200 =head1 VERSION
201  
202 This document describes WWW::Foursquare version 0.9906
203  
204 =cut
205  
206 =head1 SYNOPSIS
207  
208 use WWW::Foursquare;
209  
210 # Create fs object
211 my $fs = WWW::Foursquare->new(
212 client_id => 'client_id',
213 client_secret => 'client_secret',
214 redirect_uri => 'redirect_uri',
215 );
216  
217 # Set access_token
218 my $access_token = 'XXXX';
219 $fs->set_token($access_token);
220  
221 # Search users by name
222 my $search_users = eval { $fs->users()->search(name => 'Pavel Vlasov') };
223  
224 if (not $@) {
225
226 # work with result in Perl structure
227 # print Dumper($search_users);
228 }
229 else {
230  
231 # process errors
232 warn $@ if $@;
233 }
234  
235 =cut
236  
237 =head1 DESCRIPTION
238  
239 This wrapper help you send requests to Foursquare API and get result in easy way, OAuth is supported, quick integration with your current project.
240 You can get more information about Foursquare API here: L<https://developer.foursquare.com/>
241  
242 =head1 INTEGRATION
243  
244 How to connect your apps with foursquare via OAuth 2.0.
245  
246 =head2 1. Create foursquare app:
247  
248 https://foursquare.com/developers/apps
249  
250 Get Client ID, Client Secret and Callback url
251  
252 my $fs = WWW::Foursquare->new(
253 client_id => 'client_id',
254 client_secret => 'client_secret',
255 redirect_uri => 'redirect_uri',
256 );
257  
258 =head2 2. Get auth url for your apps:
259  
260 my $auth_url = $fs->get_auth_url();
261 It looks like this: https://foursquare.com/oauth2/authenticate?client_id=client_id&redirect_uri=redirect_url&response_type=code
262  
263 =head2 3. Have a user authorize your app.
264  
265 Implement callback in your server.
266  
267 - You click on auth url to go to autorize page.
268 - If authorization is successful you will redirect to callback url and get code.
269 - Then you use this code for getting access_token for Foursquare API
270  
271 http://your_server.com/callback&code=XXXXXXX
272 my $code = ... # getting code here
273  
274 Allow app to access to your account in foursquare
275  
276 my $access_token = $fs->get_access_token($code);
277  
278 Background magic will send GET request to Foursquare API
279  
280 =head2 4. Set access_token and using Foursquare API
281  
282 $fs->set_access_token($access_token);
283  
284 For more information I would recommend you visit page L<https://developer.foursquare.com/overview/auth>
285  
286 =head1 DEBUG MODE
287  
288 my $fs = WWW::Foursquare->new(
289 debug => 1,
290 );
291  
292 =head1 METHODS
293  
294 =head2 new
295  
296 Creating a new foursquare object.
297  
298 my $fs = WWW::Foursquare->new(
299 client_id => 'client_id',
300 client_secret => 'client_secret',
301 redirect_uri => 'redirect_uri',
302 );
303  
304 =head2 get_auth_url
305  
306 Prepare auth url from Foursquare parameters (cliend_id, redirect_uri)
307  
308 my $auth_url = $fs->get_auth_url();
309  
310 =head2 get_access_token
311  
312 Get code after redirect, and send GET request to fetch access token
313  
314 my $access_token = $fs->get_access_token($code);
315  
316 =head2 set_access_token
317
318 Set access token for foursquare object
319  
320 $fs->set_access_token($code);
321  
322 =head2 users
323  
324 All users methods: https://developer.foursquare.com/docs/users/users
325
326 If you want to use itself method for forsquare API:
327  
328 $fs->users->info(); # get info about users etc
329  
330 =head2 venues
331  
332 All venues methods: https://developer.foursquare.com/docs/venues/venues
333  
334 =head2 venuegroups
335  
336 All venuegroups methods: https://developer.foursquare.com/docs/venuegroups/venuegroups
337  
338 =head2 checkins
339  
340 All checkins methods: https://developer.foursquare.com/docs/checkins/checkins
341  
342 =head2 tips
343  
344 All tips methods: https://developer.foursquare.com/docs/tips/tips
345  
346 =head2 lists
347  
348 All lists methods: https://developer.foursquare.com/docs/lists/lists
349  
350 =head2 updates
351  
352 All updates methods: https://developer.foursquare.com/docs/updates/updates
353  
354 =head2 photos
355  
356 All photos methods: https://developer.foursquare.com/docs/photos/photos
357  
358 =head2 settings
359
360 All settings methods: https://developer.foursquare.com/docs/settings/settings
361  
362 =head2 specials
363
364 All specials methods: https://developer.foursquare.com/docs/specials/specials
365  
366 =head2 campaigns
367  
368 All campaigns methods: https://developer.foursquare.com/docs/campaigns/campaigns
369  
370 =head2 events
371  
372 All events methods: https://developer.foursquare.com/docs/events/events
373  
374 =head2 pages
375  
376 All pages methods: https://developer.foursquare.com/docs/pages/pages
377  
378 =head2 pageupdates
379  
380 All pageupdates methods: https://developer.foursquare.com/docs/pageupdates/pageupdates
381  
382 =head1 EXAMPLES
383  
384 You can see examples how to use WWW::Foursquare.
385 Here: /eg/test.pl
386  
387 =head1 AUTHOR
388  
389 Pavel Vlasov, C<< <fxzuz at cpan.org> >>
390  
391 =head1 BUGS
392  
393 Please report any bugs or feature requests to C<bug-www-foursquare at rt.cpan.org>, or through
394 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Foursquare>. I will be notified, and then you'll
395 automatically be notified of progress on your bug as I make changes.
396  
397 =head1 SUPPORT
398  
399 You can find documentation for this module with the perldoc command.
400  
401 perldoc WWW::Foursquare
402  
403  
404 You can also look for information at:
405  
406 =over 5
407  
408 =item * Github
409  
410 L<http://github.com/fxzuz/WWW-Foursquare/>
411  
412 =item * RT: CPAN's request tracker (report bugs here)
413  
414 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Foursquare>
415  
416 =item * AnnoCPAN: Annotated CPAN documentation
417  
418 L<http://annocpan.org/dist/WWW-Foursquare>
419  
420 =item * CPAN Ratings
421  
422 L<http://cpanratings.perl.org/d/WWW-Foursquare>
423  
424 =item * Search CPAN
425  
426 L<http://search.cpan.org/dist/WWW-Foursquare/>
427  
428 =back
429  
430 =head1 LICENSE AND COPYRIGHT
431  
432 Copyright 2012 Pavel Vlasov.
433  
434 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
435  
436 =cut
« Foursquare/Photos.pm ↑ Summary Foursquare/Config.pm »