File Coverage

blib/lib/WWW/Foursquare.pm
Criterion Covered Total %
statement 101 127 79.5
branch 5 8 62.5
condition n/a
subroutine 29 42 69.0
pod 17 17 100.0
total 152 194 78.3


line stmt bran cond sub pod time code
1             package WWW::Foursquare;
2              
3 2     2   87953 use 5.006;
  2         9  
  2         117  
4 2     2   16 use strict;
  2         18  
  2         92  
5 2     2   15 use warnings FATAL => 'all';
  2         10  
  2         702  
6              
7             our $VERSION = '0.9906';
8              
9 2     2   1891 use WWW::Foursquare::Config;
  2         6  
  2         281  
10 2     2   1196 use WWW::Foursquare::Request;
  2         7  
  2         72  
11              
12 2     2   1442 use WWW::Foursquare::Users;
  2         5  
  2         95  
13 2     2   1149 use WWW::Foursquare::Venues;
  2         6  
  2         59  
14 2     2   1054 use WWW::Foursquare::Venuegroups;
  2         6  
  2         49  
15 2     2   1033 use WWW::Foursquare::Checkins;
  2         5  
  2         53  
16 2     2   943 use WWW::Foursquare::Tips;
  2         6  
  2         56  
17 2     2   1096 use WWW::Foursquare::Lists;
  2         7  
  2         63  
18 2     2   1217 use WWW::Foursquare::Updates;
  2         5  
  2         75  
19 2     2   1128 use WWW::Foursquare::Photos;
  2         6  
  2         58  
20 2     2   1079 use WWW::Foursquare::Settings;
  2         4  
  2         54  
21 2     2   1094 use WWW::Foursquare::Specials;
  2         6  
  2         60  
22 2     2   1100 use WWW::Foursquare::Campaigns;
  2         5  
  2         60  
23 2     2   1108 use WWW::Foursquare::Events;
  2         5  
  2         56  
24 2     2   1335 use WWW::Foursquare::Pages;
  2         5  
  2         59  
25 2     2   1126 use WWW::Foursquare::Pageupdates;
  2         6  
  2         65  
26              
27 2     2   13 use JSON;
  2         4  
  2         18  
28 2     2   316 use LWP;
  2         4  
  2         47  
29 2     2   12 use URI::Escape;
  2         3  
  2         2348  
30              
31             sub new {
32 1     1 1 14 my ($class, %params) = @_;
33            
34 1         2 my $self = {};
35 1         3 bless $self, $class;
36 1         6 $self->{client_id} = $params{client_id};
37 1         2 $self->{client_secret} = $params{client_secret};
38 1         2 $self->{debug} = $params{debug};
39 1         2 $self->{redirect_uri} = $params{redirect_uri};
40 1         11 $self->{request} = WWW::Foursquare::Request->new(\%params);
41            
42 1         7 return $self;
43             }
44              
45             sub get_auth_url {
46 1     1 1 217 my $self = shift;
47              
48 1         8 my $params = {
49             client_id => $self->{client_id},
50             redirect_uri => $self->{redirect_uri},
51             response_type => 'code',
52             };
53 1         6 my $query = _params_to_str( $params );
54 1         10 my $url = sprintf "%s?%s", $AUTH_CODE_ENDPOINT, $query;
55              
56 1         19 return $url;
57             }
58              
59             sub get_access_token {
60 2     2 1 6 my ($self, $code) = @_;
61              
62 2         22 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             };
69              
70 2         10 my $res = $self->_get($ACCESS_TOKEN_ENDPOINT, $params);
71 1         41 return $res->{access_token};
72             }
73              
74             sub set_access_token {
75 1     1 1 383 my ($self, $access_token) = @_;
76              
77 1 50       8 warn "access_token: $access_token" if $self->{debug};
78              
79             # update params of request
80 1         4 $self->{request}->{access_token} = $access_token;
81 1 50       7 $self->{request}->{userless} = $access_token
82             ? 0
83             : 1;
84             }
85              
86             sub users {
87 0     0 1 0 my ($self, $user_id) = @_;
88              
89 0         0 return WWW::Foursquare::Users->new($self->{request}, $user_id);
90             }
91              
92             sub venues {
93 0     0 1 0 my ($self, $venue_id) = @_;
94              
95 0         0 return WWW::Foursquare::Venues->new($self->{request}, $venue_id);
96             }
97              
98             sub venuegroups {
99 0     0 1 0 my ($self, $group_id) = @_;
100              
101 0         0 return WWW::Foursquare::Venuegroups->new($self->{request}, $group_id);
102             }
103              
104             sub checkins {
105 0     0 1 0 my ($self, $checkin_id) = @_;
106              
107 0         0 return WWW::Foursquare::Checkins->new($self->{request}, $checkin_id);
108             }
109              
110             sub tips {
111 0     0 1 0 my ($self, $tip_id) = @_;
112              
113 0         0 return WWW::Foursquare::Tips->new($self->{request}, $tip_id);
114             }
115              
116             sub lists {
117 0     0 1 0 my ($self, $list_id) = @_;
118              
119 0         0 return WWW::Foursquare::Lists->new($self->{request}, $list_id);
120             }
121              
122             sub updates {
123 0     0 1 0 my ($self, $update_id) = @_;
124              
125 0         0 return WWW::Foursquare::Updates->new($self->{request}, $update_id);
126             }
127              
128             sub photos {
129 0     0 1 0 my ($self, $photo_id) = @_;
130              
131 0         0 return WWW::Foursquare::Photos->new($self->{request}, $photo_id);
132             }
133              
134             sub settings {
135 0     0 1 0 my ($self, $setting_id) = @_;
136              
137 0         0 return WWW::Foursquare::Settings->new($self->{request}, $setting_id);
138             }
139              
140             sub campaigns {
141 0     0 1 0 my ($self, $campaign_id) = @_;
142              
143 0         0 return WWW::Foursquare::Campaigns->new($self->{request}, $campaign_id);
144             }
145              
146             sub events {
147 0     0 1 0 my ($self, $event_id) = @_;
148            
149 0         0 return WWW::Foursquare::Events->new($self->{request}, $event_id);
150             }
151              
152             sub pages {
153 0     0 1 0 my ($self, $page_id) = @_;
154              
155 0         0 return WWW::Foursquare::Pages->new($self->{request}, $page_id);
156             }
157              
158             sub pageupdates {
159 0     0 1 0 my ($self, $update_id) = @_;
160              
161 0         0 return WWW::Foursquare::Pageupdates->new($self->{request}, $update_id);
162             }
163              
164             sub _params_to_str {
165 3     3   7 my $hash = shift;
166              
167 3         22 my $str = join '&', map { $_.'='.uri_escape($hash->{$_}) } sort keys %$hash;
  13         339  
168 3         45 return $str;
169             }
170              
171             sub _get {
172 2     2   13 my ($self, $url, $params) = @_;
173              
174 2         7 my $query = _params_to_str($params);
175 2         11 my $result_url = sprintf "%s?%s", $url, $query;
176              
177 2 50       13 warn "$result_url" if $self->{debug};
178              
179 2         13 my $res = $self->{request}->{ua}->get($result_url);
180              
181 2 100       27039 return decode_json($res->content()) if $res->code() == 200;
182              
183             # throw exception
184 1         19 die $res->content();
185             }
186              
187             sub _ua {
188 2     2   536 my ($self) = @_;
189              
190 2         19 return $self->{request}->{ua};
191             }
192              
193              
194             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
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
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<< >>
390              
391             =head1 BUGS
392              
393             Please report any bugs or feature requests to C, or through
394             the web interface at L. 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
411              
412             =item * RT: CPAN's request tracker (report bugs here)
413              
414             L
415              
416             =item * AnnoCPAN: Annotated CPAN documentation
417              
418             L
419              
420             =item * CPAN Ratings
421              
422             L
423              
424             =item * Search CPAN
425              
426             L
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